You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
4.2 KiB

  1. ## TOML parser and encoder for Go with reflection
  2. TOML stands for Tom's Obvious, Minimal Language. This Go package provides a
  3. reflection interface similar to Go's standard library `json` and `xml`
  4. packages. This package also supports the `encoding.TextUnmarshaler` and
  5. `encoding.TextMarshaler` interfaces so that you can define custom data
  6. representations. (There is an example of this below.)
  7. Spec: https://github.com/toml-lang/toml
  8. Compatible with TOML version
  9. [v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
  10. Documentation: https://godoc.org/github.com/BurntSushi/toml
  11. Installation:
  12. ```bash
  13. go get github.com/BurntSushi/toml
  14. ```
  15. Try the toml validator:
  16. ```bash
  17. go get github.com/BurntSushi/toml/cmd/tomlv
  18. tomlv some-toml-file.toml
  19. ```
  20. [![Build Status](https://travis-ci.org/BurntSushi/toml.svg?branch=master)](https://travis-ci.org/BurntSushi/toml) [![GoDoc](https://godoc.org/github.com/BurntSushi/toml?status.svg)](https://godoc.org/github.com/BurntSushi/toml)
  21. ### Testing
  22. This package passes all tests in
  23. [toml-test](https://github.com/BurntSushi/toml-test) for both the decoder
  24. and the encoder.
  25. ### Examples
  26. This package works similarly to how the Go standard library handles `XML`
  27. and `JSON`. Namely, data is loaded into Go values via reflection.
  28. For the simplest example, consider some TOML file as just a list of keys
  29. and values:
  30. ```toml
  31. Age = 25
  32. Cats = [ "Cauchy", "Plato" ]
  33. Pi = 3.14
  34. Perfection = [ 6, 28, 496, 8128 ]
  35. DOB = 1987-07-05T05:45:00Z
  36. ```
  37. Which could be defined in Go as:
  38. ```go
  39. type Config struct {
  40. Age int
  41. Cats []string
  42. Pi float64
  43. Perfection []int
  44. DOB time.Time // requires `import time`
  45. }
  46. ```
  47. And then decoded with:
  48. ```go
  49. var conf Config
  50. if _, err := toml.Decode(tomlData, &conf); err != nil {
  51. // handle error
  52. }
  53. ```
  54. You can also use struct tags if your struct field name doesn't map to a TOML
  55. key value directly:
  56. ```toml
  57. some_key_NAME = "wat"
  58. ```
  59. ```go
  60. type TOML struct {
  61. ObscureKey string `toml:"some_key_NAME"`
  62. }
  63. ```
  64. ### Using the `encoding.TextUnmarshaler` interface
  65. Here's an example that automatically parses duration strings into
  66. `time.Duration` values:
  67. ```toml
  68. [[song]]
  69. name = "Thunder Road"
  70. duration = "4m49s"
  71. [[song]]
  72. name = "Stairway to Heaven"
  73. duration = "8m03s"
  74. ```
  75. Which can be decoded with:
  76. ```go
  77. type song struct {
  78. Name string
  79. Duration duration
  80. }
  81. type songs struct {
  82. Song []song
  83. }
  84. var favorites songs
  85. if _, err := toml.Decode(blob, &favorites); err != nil {
  86. log.Fatal(err)
  87. }
  88. for _, s := range favorites.Song {
  89. fmt.Printf("%s (%s)\n", s.Name, s.Duration)
  90. }
  91. ```
  92. And you'll also need a `duration` type that satisfies the
  93. `encoding.TextUnmarshaler` interface:
  94. ```go
  95. type duration struct {
  96. time.Duration
  97. }
  98. func (d *duration) UnmarshalText(text []byte) error {
  99. var err error
  100. d.Duration, err = time.ParseDuration(string(text))
  101. return err
  102. }
  103. ```
  104. ### More complex usage
  105. Here's an example of how to load the example from the official spec page:
  106. ```toml
  107. # This is a TOML document. Boom.
  108. title = "TOML Example"
  109. [owner]
  110. name = "Tom Preston-Werner"
  111. organization = "GitHub"
  112. bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
  113. dob = 1979-05-27T07:32:00Z # First class dates? Why not?
  114. [database]
  115. server = "192.168.1.1"
  116. ports = [ 8001, 8001, 8002 ]
  117. connection_max = 5000
  118. enabled = true
  119. [servers]
  120. # You can indent as you please. Tabs or spaces. TOML don't care.
  121. [servers.alpha]
  122. ip = "10.0.0.1"
  123. dc = "eqdc10"
  124. [servers.beta]
  125. ip = "10.0.0.2"
  126. dc = "eqdc10"
  127. [clients]
  128. data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
  129. # Line breaks are OK when inside arrays
  130. hosts = [
  131. "alpha",
  132. "omega"
  133. ]
  134. ```
  135. And the corresponding Go types are:
  136. ```go
  137. type tomlConfig struct {
  138. Title string
  139. Owner ownerInfo
  140. DB database `toml:"database"`
  141. Servers map[string]server
  142. Clients clients
  143. }
  144. type ownerInfo struct {
  145. Name string
  146. Org string `toml:"organization"`
  147. Bio string
  148. DOB time.Time
  149. }
  150. type database struct {
  151. Server string
  152. Ports []int
  153. ConnMax int `toml:"connection_max"`
  154. Enabled bool
  155. }
  156. type server struct {
  157. IP string
  158. DC string
  159. }
  160. type clients struct {
  161. Data [][]interface{}
  162. Hosts []string
  163. }
  164. ```
  165. Note that a case insensitive match will be tried if an exact match can't be
  166. found.
  167. A working example of the above can be found in `_examples/example.{go,toml}`.