encode.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. package toml
  2. import (
  3. "bufio"
  4. "encoding"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "math"
  9. "reflect"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/BurntSushi/toml/internal"
  15. )
  16. type tomlEncodeError struct{ error }
  17. var (
  18. errArrayNilElement = errors.New("toml: cannot encode array with nil element")
  19. errNonString = errors.New("toml: cannot encode a map with non-string key type")
  20. errAnonNonStruct = errors.New("toml: cannot encode an anonymous field that is not a struct")
  21. errNoKey = errors.New("toml: top-level values must be Go maps or structs")
  22. errAnything = errors.New("") // used in testing
  23. )
  24. var quotedReplacer = strings.NewReplacer(
  25. "\"", "\\\"",
  26. "\\", "\\\\",
  27. "\x00", `\u0000`,
  28. "\x01", `\u0001`,
  29. "\x02", `\u0002`,
  30. "\x03", `\u0003`,
  31. "\x04", `\u0004`,
  32. "\x05", `\u0005`,
  33. "\x06", `\u0006`,
  34. "\x07", `\u0007`,
  35. "\b", `\b`,
  36. "\t", `\t`,
  37. "\n", `\n`,
  38. "\x0b", `\u000b`,
  39. "\f", `\f`,
  40. "\r", `\r`,
  41. "\x0e", `\u000e`,
  42. "\x0f", `\u000f`,
  43. "\x10", `\u0010`,
  44. "\x11", `\u0011`,
  45. "\x12", `\u0012`,
  46. "\x13", `\u0013`,
  47. "\x14", `\u0014`,
  48. "\x15", `\u0015`,
  49. "\x16", `\u0016`,
  50. "\x17", `\u0017`,
  51. "\x18", `\u0018`,
  52. "\x19", `\u0019`,
  53. "\x1a", `\u001a`,
  54. "\x1b", `\u001b`,
  55. "\x1c", `\u001c`,
  56. "\x1d", `\u001d`,
  57. "\x1e", `\u001e`,
  58. "\x1f", `\u001f`,
  59. "\x7f", `\u007f`,
  60. )
  61. // Encoder encodes a Go to a TOML document.
  62. //
  63. // The mapping between Go values and TOML values should be precisely the same as
  64. // for the Decode* functions. Similarly, the TextMarshaler interface is
  65. // supported by encoding the resulting bytes as strings. If you want to write
  66. // arbitrary binary data then you will need to use something like base64 since
  67. // TOML does not have any binary types.
  68. //
  69. // When encoding TOML hashes (Go maps or structs), keys without any sub-hashes
  70. // are encoded first.
  71. //
  72. // Go maps will be sorted alphabetically by key for deterministic output.
  73. //
  74. // Encoding Go values without a corresponding TOML representation will return an
  75. // error. Examples of this includes maps with non-string keys, slices with nil
  76. // elements, embedded non-struct types, and nested slices containing maps or
  77. // structs. (e.g. [][]map[string]string is not allowed but []map[string]string
  78. // is okay, as is []map[string][]string).
  79. //
  80. // NOTE: Only exported keys are encoded due to the use of reflection. Unexported
  81. // keys are silently discarded.
  82. type Encoder struct {
  83. // The string to use for a single indentation level. The default is two
  84. // spaces.
  85. Indent string
  86. // hasWritten is whether we have written any output to w yet.
  87. hasWritten bool
  88. w *bufio.Writer
  89. }
  90. // NewEncoder create a new Encoder.
  91. func NewEncoder(w io.Writer) *Encoder {
  92. return &Encoder{
  93. w: bufio.NewWriter(w),
  94. Indent: " ",
  95. }
  96. }
  97. // Encode writes a TOML representation of the Go value to the Encoder's writer.
  98. //
  99. // An error is returned if the value given cannot be encoded to a valid TOML
  100. // document.
  101. func (enc *Encoder) Encode(v interface{}) error {
  102. rv := eindirect(reflect.ValueOf(v))
  103. if err := enc.safeEncode(Key([]string{}), rv); err != nil {
  104. return err
  105. }
  106. return enc.w.Flush()
  107. }
  108. func (enc *Encoder) safeEncode(key Key, rv reflect.Value) (err error) {
  109. defer func() {
  110. if r := recover(); r != nil {
  111. if terr, ok := r.(tomlEncodeError); ok {
  112. err = terr.error
  113. return
  114. }
  115. panic(r)
  116. }
  117. }()
  118. enc.encode(key, rv)
  119. return nil
  120. }
  121. func (enc *Encoder) encode(key Key, rv reflect.Value) {
  122. // Special case. Time needs to be in ISO8601 format.
  123. // Special case. If we can marshal the type to text, then we used that.
  124. // Basically, this prevents the encoder for handling these types as
  125. // generic structs (or whatever the underlying type of a TextMarshaler is).
  126. switch t := rv.Interface().(type) {
  127. case time.Time, encoding.TextMarshaler:
  128. enc.writeKeyValue(key, rv, false)
  129. return
  130. // TODO: #76 would make this superfluous after implemented.
  131. case Primitive:
  132. enc.encode(key, reflect.ValueOf(t.undecoded))
  133. return
  134. }
  135. k := rv.Kind()
  136. switch k {
  137. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
  138. reflect.Int64,
  139. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
  140. reflect.Uint64,
  141. reflect.Float32, reflect.Float64, reflect.String, reflect.Bool:
  142. enc.writeKeyValue(key, rv, false)
  143. case reflect.Array, reflect.Slice:
  144. if typeEqual(tomlArrayHash, tomlTypeOfGo(rv)) {
  145. enc.eArrayOfTables(key, rv)
  146. } else {
  147. enc.writeKeyValue(key, rv, false)
  148. }
  149. case reflect.Interface:
  150. if rv.IsNil() {
  151. return
  152. }
  153. enc.encode(key, rv.Elem())
  154. case reflect.Map:
  155. if rv.IsNil() {
  156. return
  157. }
  158. enc.eTable(key, rv)
  159. case reflect.Ptr:
  160. if rv.IsNil() {
  161. return
  162. }
  163. enc.encode(key, rv.Elem())
  164. case reflect.Struct:
  165. enc.eTable(key, rv)
  166. default:
  167. encPanic(fmt.Errorf("unsupported type for key '%s': %s", key, k))
  168. }
  169. }
  170. // eElement encodes any value that can be an array element.
  171. func (enc *Encoder) eElement(rv reflect.Value) {
  172. switch v := rv.Interface().(type) {
  173. case time.Time: // Using TextMarshaler adds extra quotes, which we don't want.
  174. format := time.RFC3339Nano
  175. switch v.Location() {
  176. case internal.LocalDatetime:
  177. format = "2006-01-02T15:04:05.999999999"
  178. case internal.LocalDate:
  179. format = "2006-01-02"
  180. case internal.LocalTime:
  181. format = "15:04:05.999999999"
  182. }
  183. switch v.Location() {
  184. default:
  185. enc.wf(v.Format(format))
  186. case internal.LocalDatetime, internal.LocalDate, internal.LocalTime:
  187. enc.wf(v.In(time.UTC).Format(format))
  188. }
  189. return
  190. case encoding.TextMarshaler:
  191. // Use text marshaler if it's available for this value.
  192. if s, err := v.MarshalText(); err != nil {
  193. encPanic(err)
  194. } else {
  195. enc.writeQuoted(string(s))
  196. }
  197. return
  198. }
  199. switch rv.Kind() {
  200. case reflect.String:
  201. enc.writeQuoted(rv.String())
  202. case reflect.Bool:
  203. enc.wf(strconv.FormatBool(rv.Bool()))
  204. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  205. enc.wf(strconv.FormatInt(rv.Int(), 10))
  206. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  207. enc.wf(strconv.FormatUint(rv.Uint(), 10))
  208. case reflect.Float32:
  209. f := rv.Float()
  210. if math.IsNaN(f) {
  211. enc.wf("nan")
  212. } else if math.IsInf(f, 0) {
  213. enc.wf("%cinf", map[bool]byte{true: '-', false: '+'}[math.Signbit(f)])
  214. } else {
  215. enc.wf(floatAddDecimal(strconv.FormatFloat(f, 'f', -1, 32)))
  216. }
  217. case reflect.Float64:
  218. f := rv.Float()
  219. if math.IsNaN(f) {
  220. enc.wf("nan")
  221. } else if math.IsInf(f, 0) {
  222. enc.wf("%cinf", map[bool]byte{true: '-', false: '+'}[math.Signbit(f)])
  223. } else {
  224. enc.wf(floatAddDecimal(strconv.FormatFloat(f, 'f', -1, 64)))
  225. }
  226. case reflect.Array, reflect.Slice:
  227. enc.eArrayOrSliceElement(rv)
  228. case reflect.Struct:
  229. enc.eStruct(nil, rv, true)
  230. case reflect.Map:
  231. enc.eMap(nil, rv, true)
  232. case reflect.Interface:
  233. enc.eElement(rv.Elem())
  234. default:
  235. encPanic(fmt.Errorf("unexpected primitive type: %T", rv.Interface()))
  236. }
  237. }
  238. // By the TOML spec, all floats must have a decimal with at least one number on
  239. // either side.
  240. func floatAddDecimal(fstr string) string {
  241. if !strings.Contains(fstr, ".") {
  242. return fstr + ".0"
  243. }
  244. return fstr
  245. }
  246. func (enc *Encoder) writeQuoted(s string) {
  247. enc.wf("\"%s\"", quotedReplacer.Replace(s))
  248. }
  249. func (enc *Encoder) eArrayOrSliceElement(rv reflect.Value) {
  250. length := rv.Len()
  251. enc.wf("[")
  252. for i := 0; i < length; i++ {
  253. elem := rv.Index(i)
  254. enc.eElement(elem)
  255. if i != length-1 {
  256. enc.wf(", ")
  257. }
  258. }
  259. enc.wf("]")
  260. }
  261. func (enc *Encoder) eArrayOfTables(key Key, rv reflect.Value) {
  262. if len(key) == 0 {
  263. encPanic(errNoKey)
  264. }
  265. for i := 0; i < rv.Len(); i++ {
  266. trv := rv.Index(i)
  267. if isNil(trv) {
  268. continue
  269. }
  270. enc.newline()
  271. enc.wf("%s[[%s]]", enc.indentStr(key), key.maybeQuotedAll())
  272. enc.newline()
  273. enc.eMapOrStruct(key, trv, false)
  274. }
  275. }
  276. func (enc *Encoder) eTable(key Key, rv reflect.Value) {
  277. if len(key) == 1 {
  278. // Output an extra newline between top-level tables.
  279. // (The newline isn't written if nothing else has been written though.)
  280. enc.newline()
  281. }
  282. if len(key) > 0 {
  283. enc.wf("%s[%s]", enc.indentStr(key), key.maybeQuotedAll())
  284. enc.newline()
  285. }
  286. enc.eMapOrStruct(key, rv, false)
  287. }
  288. func (enc *Encoder) eMapOrStruct(key Key, rv reflect.Value, inline bool) {
  289. switch rv := eindirect(rv); rv.Kind() {
  290. case reflect.Map:
  291. enc.eMap(key, rv, inline)
  292. case reflect.Struct:
  293. enc.eStruct(key, rv, inline)
  294. default:
  295. // Should never happen?
  296. panic("eTable: unhandled reflect.Value Kind: " + rv.Kind().String())
  297. }
  298. }
  299. func (enc *Encoder) eMap(key Key, rv reflect.Value, inline bool) {
  300. rt := rv.Type()
  301. if rt.Key().Kind() != reflect.String {
  302. encPanic(errNonString)
  303. }
  304. // Sort keys so that we have deterministic output. And write keys directly
  305. // underneath this key first, before writing sub-structs or sub-maps.
  306. var mapKeysDirect, mapKeysSub []string
  307. for _, mapKey := range rv.MapKeys() {
  308. k := mapKey.String()
  309. if typeIsHash(tomlTypeOfGo(rv.MapIndex(mapKey))) {
  310. mapKeysSub = append(mapKeysSub, k)
  311. } else {
  312. mapKeysDirect = append(mapKeysDirect, k)
  313. }
  314. }
  315. var writeMapKeys = func(mapKeys []string, trailC bool) {
  316. sort.Strings(mapKeys)
  317. for i, mapKey := range mapKeys {
  318. val := rv.MapIndex(reflect.ValueOf(mapKey))
  319. if isNil(val) {
  320. continue
  321. }
  322. if inline {
  323. enc.writeKeyValue(Key{mapKey}, val, true)
  324. if trailC || i != len(mapKeys)-1 {
  325. enc.wf(", ")
  326. }
  327. } else {
  328. enc.encode(key.add(mapKey), val)
  329. }
  330. }
  331. }
  332. if inline {
  333. enc.wf("{")
  334. }
  335. writeMapKeys(mapKeysDirect, len(mapKeysSub) > 0)
  336. writeMapKeys(mapKeysSub, false)
  337. if inline {
  338. enc.wf("}")
  339. }
  340. }
  341. func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
  342. // Write keys for fields directly under this key first, because if we write
  343. // a field that creates a new table then all keys under it will be in that
  344. // table (not the one we're writing here).
  345. //
  346. // Fields is a [][]int: for fieldsDirect this always has one entry (the
  347. // struct index). For fieldsSub it contains two entries: the parent field
  348. // index from tv, and the field indexes for the fields of the sub.
  349. var (
  350. rt = rv.Type()
  351. fieldsDirect, fieldsSub [][]int
  352. addFields func(rt reflect.Type, rv reflect.Value, start []int)
  353. )
  354. addFields = func(rt reflect.Type, rv reflect.Value, start []int) {
  355. for i := 0; i < rt.NumField(); i++ {
  356. f := rt.Field(i)
  357. if f.PkgPath != "" && !f.Anonymous { /// Skip unexported fields.
  358. continue
  359. }
  360. frv := rv.Field(i)
  361. // Treat anonymous struct fields with tag names as though they are
  362. // not anonymous, like encoding/json does.
  363. //
  364. // Non-struct anonymous fields use the normal encoding logic.
  365. if f.Anonymous {
  366. t := f.Type
  367. switch t.Kind() {
  368. case reflect.Struct:
  369. if getOptions(f.Tag).name == "" {
  370. addFields(t, frv, append(start, f.Index...))
  371. continue
  372. }
  373. case reflect.Ptr:
  374. if t.Elem().Kind() == reflect.Struct && getOptions(f.Tag).name == "" {
  375. if !frv.IsNil() {
  376. addFields(t.Elem(), frv.Elem(), append(start, f.Index...))
  377. }
  378. continue
  379. }
  380. }
  381. }
  382. if typeIsHash(tomlTypeOfGo(frv)) {
  383. fieldsSub = append(fieldsSub, append(start, f.Index...))
  384. } else {
  385. fieldsDirect = append(fieldsDirect, append(start, f.Index...))
  386. }
  387. }
  388. }
  389. addFields(rt, rv, nil)
  390. writeFields := func(fields [][]int) {
  391. for _, fieldIndex := range fields {
  392. fieldType := rt.FieldByIndex(fieldIndex)
  393. fieldVal := rv.FieldByIndex(fieldIndex)
  394. if isNil(fieldVal) { /// Don't write anything for nil fields.
  395. continue
  396. }
  397. opts := getOptions(fieldType.Tag)
  398. if opts.skip {
  399. continue
  400. }
  401. keyName := fieldType.Name
  402. if opts.name != "" {
  403. keyName = opts.name
  404. }
  405. if opts.omitempty && isEmpty(fieldVal) {
  406. continue
  407. }
  408. if opts.omitzero && isZero(fieldVal) {
  409. continue
  410. }
  411. if inline {
  412. enc.writeKeyValue(Key{keyName}, fieldVal, true)
  413. if fieldIndex[0] != len(fields)-1 {
  414. enc.wf(", ")
  415. }
  416. } else {
  417. enc.encode(key.add(keyName), fieldVal)
  418. }
  419. }
  420. }
  421. if inline {
  422. enc.wf("{")
  423. }
  424. writeFields(fieldsDirect)
  425. writeFields(fieldsSub)
  426. if inline {
  427. enc.wf("}")
  428. }
  429. }
  430. // tomlTypeName returns the TOML type name of the Go value's type. It is
  431. // used to determine whether the types of array elements are mixed (which is
  432. // forbidden). If the Go value is nil, then it is illegal for it to be an array
  433. // element, and valueIsNil is returned as true.
  434. // Returns the TOML type of a Go value. The type may be `nil`, which means
  435. // no concrete TOML type could be found.
  436. func tomlTypeOfGo(rv reflect.Value) tomlType {
  437. if isNil(rv) || !rv.IsValid() {
  438. return nil
  439. }
  440. switch rv.Kind() {
  441. case reflect.Bool:
  442. return tomlBool
  443. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
  444. reflect.Int64,
  445. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
  446. reflect.Uint64:
  447. return tomlInteger
  448. case reflect.Float32, reflect.Float64:
  449. return tomlFloat
  450. case reflect.Array, reflect.Slice:
  451. if typeEqual(tomlHash, tomlArrayType(rv)) {
  452. return tomlArrayHash
  453. }
  454. return tomlArray
  455. case reflect.Ptr, reflect.Interface:
  456. return tomlTypeOfGo(rv.Elem())
  457. case reflect.String:
  458. return tomlString
  459. case reflect.Map:
  460. return tomlHash
  461. case reflect.Struct:
  462. switch rv.Interface().(type) {
  463. case time.Time:
  464. return tomlDatetime
  465. case encoding.TextMarshaler:
  466. return tomlString
  467. default:
  468. // Someone used a pointer receiver: we can make it work for pointer
  469. // values.
  470. if rv.CanAddr() {
  471. _, ok := rv.Addr().Interface().(encoding.TextMarshaler)
  472. if ok {
  473. return tomlString
  474. }
  475. }
  476. return tomlHash
  477. }
  478. default:
  479. _, ok := rv.Interface().(encoding.TextMarshaler)
  480. if ok {
  481. return tomlString
  482. }
  483. encPanic(errors.New("unsupported type: " + rv.Kind().String()))
  484. panic("") // Need *some* return value
  485. }
  486. }
  487. // tomlArrayType returns the element type of a TOML array. The type returned
  488. // may be nil if it cannot be determined (e.g., a nil slice or a zero length
  489. // slize). This function may also panic if it finds a type that cannot be
  490. // expressed in TOML (such as nil elements, heterogeneous arrays or directly
  491. // nested arrays of tables).
  492. func tomlArrayType(rv reflect.Value) tomlType {
  493. if isNil(rv) || !rv.IsValid() || rv.Len() == 0 {
  494. return nil
  495. }
  496. /// Don't allow nil.
  497. rvlen := rv.Len()
  498. for i := 1; i < rvlen; i++ {
  499. if tomlTypeOfGo(rv.Index(i)) == nil {
  500. encPanic(errArrayNilElement)
  501. }
  502. }
  503. firstType := tomlTypeOfGo(rv.Index(0))
  504. if firstType == nil {
  505. encPanic(errArrayNilElement)
  506. }
  507. return firstType
  508. }
  509. type tagOptions struct {
  510. skip bool // "-"
  511. name string
  512. omitempty bool
  513. omitzero bool
  514. }
  515. func getOptions(tag reflect.StructTag) tagOptions {
  516. t := tag.Get("toml")
  517. if t == "-" {
  518. return tagOptions{skip: true}
  519. }
  520. var opts tagOptions
  521. parts := strings.Split(t, ",")
  522. opts.name = parts[0]
  523. for _, s := range parts[1:] {
  524. switch s {
  525. case "omitempty":
  526. opts.omitempty = true
  527. case "omitzero":
  528. opts.omitzero = true
  529. }
  530. }
  531. return opts
  532. }
  533. func isZero(rv reflect.Value) bool {
  534. switch rv.Kind() {
  535. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  536. return rv.Int() == 0
  537. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  538. return rv.Uint() == 0
  539. case reflect.Float32, reflect.Float64:
  540. return rv.Float() == 0.0
  541. }
  542. return false
  543. }
  544. func isEmpty(rv reflect.Value) bool {
  545. switch rv.Kind() {
  546. case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
  547. return rv.Len() == 0
  548. case reflect.Bool:
  549. return !rv.Bool()
  550. }
  551. return false
  552. }
  553. func (enc *Encoder) newline() {
  554. if enc.hasWritten {
  555. enc.wf("\n")
  556. }
  557. }
  558. // Write a key/value pair:
  559. //
  560. // key = <any value>
  561. //
  562. // If inline is true it won't add a newline at the end.
  563. func (enc *Encoder) writeKeyValue(key Key, val reflect.Value, inline bool) {
  564. if len(key) == 0 {
  565. encPanic(errNoKey)
  566. }
  567. enc.wf("%s%s = ", enc.indentStr(key), key.maybeQuoted(len(key)-1))
  568. enc.eElement(val)
  569. if !inline {
  570. enc.newline()
  571. }
  572. }
  573. func (enc *Encoder) wf(format string, v ...interface{}) {
  574. if _, err := fmt.Fprintf(enc.w, format, v...); err != nil {
  575. encPanic(err)
  576. }
  577. enc.hasWritten = true
  578. }
  579. func (enc *Encoder) indentStr(key Key) string {
  580. return strings.Repeat(enc.Indent, len(key)-1)
  581. }
  582. func encPanic(err error) {
  583. panic(tomlEncodeError{err})
  584. }
  585. func eindirect(v reflect.Value) reflect.Value {
  586. switch v.Kind() {
  587. case reflect.Ptr, reflect.Interface:
  588. return eindirect(v.Elem())
  589. default:
  590. return v
  591. }
  592. }
  593. func isNil(rv reflect.Value) bool {
  594. switch rv.Kind() {
  595. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  596. return rv.IsNil()
  597. default:
  598. return false
  599. }
  600. }