mapstructure_examples_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package mhayaMapStructure
  2. import (
  3. "fmt"
  4. )
  5. func ExampleDecode() {
  6. type Person struct {
  7. Name string
  8. Age int
  9. Emails []string
  10. Extra map[string]string
  11. }
  12. // This input can come from anywhere, but typically comes from
  13. // something like decoding JSON where we're not quite sure of the
  14. // struct initially.
  15. input := map[string]interface{}{
  16. "name": "Mitchell",
  17. "age": 91,
  18. "emails": []string{"one", "two", "three"},
  19. "extra": map[string]string{
  20. "twitter": "mitchellh",
  21. },
  22. }
  23. var result Person
  24. err := Decode(input, &result)
  25. if err != nil {
  26. panic(err)
  27. }
  28. fmt.Printf("%#v", result)
  29. // Output:
  30. // mapstructure.Person{Name:"Mitchell", Age:91, Emails:[]string{"one", "two", "three"}, Extra:map[string]string{"twitter":"mitchellh"}}
  31. }
  32. func ExampleDecode_errors() {
  33. type Person struct {
  34. Name string
  35. Age int
  36. Emails []string
  37. Extra map[string]string
  38. }
  39. // This input can come from anywhere, but typically comes from
  40. // something like decoding JSON where we're not quite sure of the
  41. // struct initially.
  42. input := map[string]interface{}{
  43. "name": 123,
  44. "age": "bad value",
  45. "emails": []int{1, 2, 3},
  46. }
  47. var result Person
  48. err := Decode(input, &result)
  49. if err == nil {
  50. panic("should have an error")
  51. }
  52. fmt.Println(err.Error())
  53. // Output:
  54. // 5 error(s) decoding:
  55. //
  56. // * 'Age' expected type 'int', got unconvertible type 'string', value: 'bad value'
  57. // * 'Emails[0]' expected type 'string', got unconvertible type 'int', value: '1'
  58. // * 'Emails[1]' expected type 'string', got unconvertible type 'int', value: '2'
  59. // * 'Emails[2]' expected type 'string', got unconvertible type 'int', value: '3'
  60. // * 'Name' expected type 'string', got unconvertible type 'int', value: '123'
  61. }
  62. func ExampleDecode_metadata() {
  63. type Person struct {
  64. Name string
  65. Age int
  66. }
  67. // This input can come from anywhere, but typically comes from
  68. // something like decoding JSON where we're not quite sure of the
  69. // struct initially.
  70. input := map[string]interface{}{
  71. "name": "Mitchell",
  72. "age": 91,
  73. "email": "foo@bar.com",
  74. }
  75. // For metadata, we make a more advanced DecoderConfig so we can
  76. // more finely configure the decoder that is used. In this case, we
  77. // just tell the decoder we want to track metadata.
  78. var md Metadata
  79. var result Person
  80. config := &DecoderConfig{
  81. Metadata: &md,
  82. Result: &result,
  83. }
  84. decoder, err := NewDecoder(config)
  85. if err != nil {
  86. panic(err)
  87. }
  88. if err := decoder.Decode(input); err != nil {
  89. panic(err)
  90. }
  91. fmt.Printf("Unused keys: %#v", md.Unused)
  92. // Output:
  93. // Unused keys: []string{"email"}
  94. }
  95. func ExampleDecode_weaklyTypedInput() {
  96. type Person struct {
  97. Name string
  98. Age int
  99. Emails []string
  100. }
  101. // This input can come from anywhere, but typically comes from
  102. // something like decoding JSON, generated by a weakly typed language
  103. // such as PHP.
  104. input := map[string]interface{}{
  105. "name": 123, // number => string
  106. "age": "42", // string => number
  107. "emails": map[string]interface{}{}, // empty map => empty array
  108. }
  109. var result Person
  110. config := &DecoderConfig{
  111. WeaklyTypedInput: true,
  112. Result: &result,
  113. }
  114. decoder, err := NewDecoder(config)
  115. if err != nil {
  116. panic(err)
  117. }
  118. err = decoder.Decode(input)
  119. if err != nil {
  120. panic(err)
  121. }
  122. fmt.Printf("%#v", result)
  123. // Output: mapstructure.Person{Name:"123", Age:42, Emails:[]string{}}
  124. }
  125. func ExampleDecode_tags() {
  126. // Note that the mapstructure tags defined in the struct type
  127. // can indicate which fields the values are mapped to.
  128. type Person struct {
  129. Name string `mapstructure:"person_name"`
  130. Age int `mapstructure:"person_age"`
  131. }
  132. input := map[string]interface{}{
  133. "person_name": "Mitchell",
  134. "person_age": 91,
  135. }
  136. var result Person
  137. err := Decode(input, &result)
  138. if err != nil {
  139. panic(err)
  140. }
  141. fmt.Printf("%#v", result)
  142. // Output:
  143. // mapstructure.Person{Name:"Mitchell", Age:91}
  144. }
  145. func ExampleDecode_embeddedStruct() {
  146. // Squashing multiple embedded structs is allowed using the squash tag.
  147. // This is demonstrated by creating a composite struct of multiple types
  148. // and decoding into it. In this case, a person can carry with it both
  149. // a Family and a Location, as well as their own FirstName.
  150. type Family struct {
  151. LastName string
  152. }
  153. type Location struct {
  154. City string
  155. }
  156. type Person struct {
  157. Family `mapstructure:",squash"`
  158. Location `mapstructure:",squash"`
  159. FirstName string
  160. }
  161. input := map[string]interface{}{
  162. "FirstName": "Mitchell",
  163. "LastName": "Hashimoto",
  164. "City": "San Francisco",
  165. }
  166. var result Person
  167. err := Decode(input, &result)
  168. if err != nil {
  169. panic(err)
  170. }
  171. fmt.Printf("%s %s, %s", result.FirstName, result.LastName, result.City)
  172. // Output:
  173. // Mitchell Hashimoto, San Francisco
  174. }
  175. func ExampleDecode_remainingData() {
  176. // Note that the mapstructure tags defined in the struct type
  177. // can indicate which fields the values are mapped to.
  178. type Person struct {
  179. Name string
  180. Age int
  181. Other map[string]interface{} `mapstructure:",remain"`
  182. }
  183. input := map[string]interface{}{
  184. "name": "Mitchell",
  185. "age": 91,
  186. "email": "mitchell@example.com",
  187. }
  188. var result Person
  189. err := Decode(input, &result)
  190. if err != nil {
  191. panic(err)
  192. }
  193. fmt.Printf("%#v", result)
  194. // Output:
  195. // mapstructure.Person{Name:"Mitchell", Age:91, Other:map[string]interface {}{"email":"mitchell@example.com"}}
  196. }
  197. func ExampleDecode_omitempty() {
  198. // NewActor omitempty annotation to avoid map keys for empty values
  199. type Family struct {
  200. LastName string
  201. }
  202. type Location struct {
  203. City string
  204. }
  205. type Person struct {
  206. *Family `mapstructure:",omitempty"`
  207. *Location `mapstructure:",omitempty"`
  208. Age int
  209. FirstName string
  210. }
  211. result := &map[string]interface{}{}
  212. input := Person{FirstName: "Somebody"}
  213. err := Decode(input, &result)
  214. if err != nil {
  215. panic(err)
  216. }
  217. fmt.Printf("%+v", result)
  218. // Output:
  219. // &map[Age:0 FirstName:Somebody]
  220. }