decode_test.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  1. //
  2. // Copyright (c) 2011-2019 Canonical Ltd
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package yaml_test
  16. import (
  17. "bytes"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "math"
  22. "reflect"
  23. "strings"
  24. "time"
  25. . "gopkg.in/check.v1"
  26. "gopkg.in/yaml.v3"
  27. )
  28. var unmarshalIntTest = 123
  29. var unmarshalTests = []struct {
  30. data string
  31. value interface{}
  32. }{
  33. {
  34. "",
  35. (*struct{})(nil),
  36. },
  37. {
  38. "{}", &struct{}{},
  39. }, {
  40. "v: hi",
  41. map[string]string{"v": "hi"},
  42. }, {
  43. "v: hi", map[string]interface{}{"v": "hi"},
  44. }, {
  45. "v: true",
  46. map[string]string{"v": "true"},
  47. }, {
  48. "v: true",
  49. map[string]interface{}{"v": true},
  50. }, {
  51. "v: 10",
  52. map[string]interface{}{"v": 10},
  53. }, {
  54. "v: 0b10",
  55. map[string]interface{}{"v": 2},
  56. }, {
  57. "v: 0xA",
  58. map[string]interface{}{"v": 10},
  59. }, {
  60. "v: 4294967296",
  61. map[string]int64{"v": 4294967296},
  62. }, {
  63. "v: 0.1",
  64. map[string]interface{}{"v": 0.1},
  65. }, {
  66. "v: .1",
  67. map[string]interface{}{"v": 0.1},
  68. }, {
  69. "v: .Inf",
  70. map[string]interface{}{"v": math.Inf(+1)},
  71. }, {
  72. "v: -.Inf",
  73. map[string]interface{}{"v": math.Inf(-1)},
  74. }, {
  75. "v: -10",
  76. map[string]interface{}{"v": -10},
  77. }, {
  78. "v: -.1",
  79. map[string]interface{}{"v": -0.1},
  80. },
  81. // Simple values.
  82. {
  83. "123",
  84. &unmarshalIntTest,
  85. },
  86. // Floats from spec
  87. {
  88. "canonical: 6.8523e+5",
  89. map[string]interface{}{"canonical": 6.8523e+5},
  90. }, {
  91. "expo: 685.230_15e+03",
  92. map[string]interface{}{"expo": 685.23015e+03},
  93. }, {
  94. "fixed: 685_230.15",
  95. map[string]interface{}{"fixed": 685230.15},
  96. }, {
  97. "neginf: -.inf",
  98. map[string]interface{}{"neginf": math.Inf(-1)},
  99. }, {
  100. "fixed: 685_230.15",
  101. map[string]float64{"fixed": 685230.15},
  102. },
  103. //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported
  104. //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails.
  105. // Bools are per 1.2 spec.
  106. {
  107. "canonical: true",
  108. map[string]interface{}{"canonical": true},
  109. }, {
  110. "canonical: false",
  111. map[string]interface{}{"canonical": false},
  112. }, {
  113. "bool: True",
  114. map[string]interface{}{"bool": true},
  115. }, {
  116. "bool: False",
  117. map[string]interface{}{"bool": false},
  118. }, {
  119. "bool: TRUE",
  120. map[string]interface{}{"bool": true},
  121. }, {
  122. "bool: FALSE",
  123. map[string]interface{}{"bool": false},
  124. },
  125. // For backwards compatibility with 1.1, decoding old strings into typed values still works.
  126. {
  127. "option: on",
  128. map[string]bool{"option": true},
  129. }, {
  130. "option: y",
  131. map[string]bool{"option": true},
  132. }, {
  133. "option: Off",
  134. map[string]bool{"option": false},
  135. }, {
  136. "option: No",
  137. map[string]bool{"option": false},
  138. }, {
  139. "option: other",
  140. map[string]bool{},
  141. },
  142. // Ints from spec
  143. {
  144. "canonical: 685230",
  145. map[string]interface{}{"canonical": 685230},
  146. }, {
  147. "decimal: +685_230",
  148. map[string]interface{}{"decimal": 685230},
  149. }, {
  150. "octal: 02472256",
  151. map[string]interface{}{"octal": 685230},
  152. }, {
  153. "octal: -02472256",
  154. map[string]interface{}{"octal": -685230},
  155. }, {
  156. "octal: 0o2472256",
  157. map[string]interface{}{"octal": 685230},
  158. }, {
  159. "octal: -0o2472256",
  160. map[string]interface{}{"octal": -685230},
  161. }, {
  162. "hexa: 0x_0A_74_AE",
  163. map[string]interface{}{"hexa": 685230},
  164. }, {
  165. "bin: 0b1010_0111_0100_1010_1110",
  166. map[string]interface{}{"bin": 685230},
  167. }, {
  168. "bin: -0b101010",
  169. map[string]interface{}{"bin": -42},
  170. }, {
  171. "bin: -0b1000000000000000000000000000000000000000000000000000000000000000",
  172. map[string]interface{}{"bin": -9223372036854775808},
  173. }, {
  174. "decimal: +685_230",
  175. map[string]int{"decimal": 685230},
  176. },
  177. //{"sexa: 190:20:30", map[string]interface{}{"sexa": 0}}, // Unsupported
  178. // Nulls from spec
  179. {
  180. "empty:",
  181. map[string]interface{}{"empty": nil},
  182. }, {
  183. "canonical: ~",
  184. map[string]interface{}{"canonical": nil},
  185. }, {
  186. "english: null",
  187. map[string]interface{}{"english": nil},
  188. }, {
  189. "~: null key",
  190. map[interface{}]string{nil: "null key"},
  191. }, {
  192. "empty:",
  193. map[string]*bool{"empty": nil},
  194. },
  195. // Flow sequence
  196. {
  197. "seq: [A,B]",
  198. map[string]interface{}{"seq": []interface{}{"A", "B"}},
  199. }, {
  200. "seq: [A,B,C,]",
  201. map[string][]string{"seq": []string{"A", "B", "C"}},
  202. }, {
  203. "seq: [A,1,C]",
  204. map[string][]string{"seq": []string{"A", "1", "C"}},
  205. }, {
  206. "seq: [A,1,C]",
  207. map[string][]int{"seq": []int{1}},
  208. }, {
  209. "seq: [A,1,C]",
  210. map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
  211. },
  212. // Block sequence
  213. {
  214. "seq:\n - A\n - B",
  215. map[string]interface{}{"seq": []interface{}{"A", "B"}},
  216. }, {
  217. "seq:\n - A\n - B\n - C",
  218. map[string][]string{"seq": []string{"A", "B", "C"}},
  219. }, {
  220. "seq:\n - A\n - 1\n - C",
  221. map[string][]string{"seq": []string{"A", "1", "C"}},
  222. }, {
  223. "seq:\n - A\n - 1\n - C",
  224. map[string][]int{"seq": []int{1}},
  225. }, {
  226. "seq:\n - A\n - 1\n - C",
  227. map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
  228. },
  229. // Literal block scalar
  230. {
  231. "scalar: | # Comment\n\n literal\n\n \ttext\n\n",
  232. map[string]string{"scalar": "\nliteral\n\n\ttext\n"},
  233. },
  234. // Folded block scalar
  235. {
  236. "scalar: > # Comment\n\n folded\n line\n \n next\n line\n * one\n * two\n\n last\n line\n\n",
  237. map[string]string{"scalar": "\nfolded line\nnext line\n * one\n * two\n\nlast line\n"},
  238. },
  239. // Map inside interface with no type hints.
  240. {
  241. "a: {b: c}",
  242. map[interface{}]interface{}{"a": map[string]interface{}{"b": "c"}},
  243. },
  244. // Non-string map inside interface with no type hints.
  245. {
  246. "a: {b: c, 1: d}",
  247. map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": "c", 1: "d"}},
  248. },
  249. // Structs and type conversions.
  250. {
  251. "hello: world",
  252. &struct{ Hello string }{"world"},
  253. }, {
  254. "a: {b: c}",
  255. &struct{ A struct{ B string } }{struct{ B string }{"c"}},
  256. }, {
  257. "a: {b: c}",
  258. &struct{ A *struct{ B string } }{&struct{ B string }{"c"}},
  259. }, {
  260. "a: 'null'",
  261. &struct{ A *unmarshalerType }{&unmarshalerType{"null"}},
  262. }, {
  263. "a: {b: c}",
  264. &struct{ A map[string]string }{map[string]string{"b": "c"}},
  265. }, {
  266. "a: {b: c}",
  267. &struct{ A *map[string]string }{&map[string]string{"b": "c"}},
  268. }, {
  269. "a:",
  270. &struct{ A map[string]string }{},
  271. }, {
  272. "a: 1",
  273. &struct{ A int }{1},
  274. }, {
  275. "a: 1",
  276. &struct{ A float64 }{1},
  277. }, {
  278. "a: 1.0",
  279. &struct{ A int }{1},
  280. }, {
  281. "a: 1.0",
  282. &struct{ A uint }{1},
  283. }, {
  284. "a: [1, 2]",
  285. &struct{ A []int }{[]int{1, 2}},
  286. }, {
  287. "a: [1, 2]",
  288. &struct{ A [2]int }{[2]int{1, 2}},
  289. }, {
  290. "a: 1",
  291. &struct{ B int }{0},
  292. }, {
  293. "a: 1",
  294. &struct {
  295. B int "a"
  296. }{1},
  297. }, {
  298. // Some limited backwards compatibility with the 1.1 spec.
  299. "a: YES",
  300. &struct{ A bool }{true},
  301. },
  302. // Some cross type conversions
  303. {
  304. "v: 42",
  305. map[string]uint{"v": 42},
  306. }, {
  307. "v: -42",
  308. map[string]uint{},
  309. }, {
  310. "v: 4294967296",
  311. map[string]uint64{"v": 4294967296},
  312. }, {
  313. "v: -4294967296",
  314. map[string]uint64{},
  315. },
  316. // int
  317. {
  318. "int_max: 2147483647",
  319. map[string]int{"int_max": math.MaxInt32},
  320. },
  321. {
  322. "int_min: -2147483648",
  323. map[string]int{"int_min": math.MinInt32},
  324. },
  325. {
  326. "int_overflow: 9223372036854775808", // math.MaxInt64 + 1
  327. map[string]int{},
  328. },
  329. // int64
  330. {
  331. "int64_max: 9223372036854775807",
  332. map[string]int64{"int64_max": math.MaxInt64},
  333. },
  334. {
  335. "int64_max_base2: 0b111111111111111111111111111111111111111111111111111111111111111",
  336. map[string]int64{"int64_max_base2": math.MaxInt64},
  337. },
  338. {
  339. "int64_min: -9223372036854775808",
  340. map[string]int64{"int64_min": math.MinInt64},
  341. },
  342. {
  343. "int64_neg_base2: -0b111111111111111111111111111111111111111111111111111111111111111",
  344. map[string]int64{"int64_neg_base2": -math.MaxInt64},
  345. },
  346. {
  347. "int64_overflow: 9223372036854775808", // math.MaxInt64 + 1
  348. map[string]int64{},
  349. },
  350. // uint
  351. {
  352. "uint_min: 0",
  353. map[string]uint{"uint_min": 0},
  354. },
  355. {
  356. "uint_max: 4294967295",
  357. map[string]uint{"uint_max": math.MaxUint32},
  358. },
  359. {
  360. "uint_underflow: -1",
  361. map[string]uint{},
  362. },
  363. // uint64
  364. {
  365. "uint64_min: 0",
  366. map[string]uint{"uint64_min": 0},
  367. },
  368. {
  369. "uint64_max: 18446744073709551615",
  370. map[string]uint64{"uint64_max": math.MaxUint64},
  371. },
  372. {
  373. "uint64_max_base2: 0b1111111111111111111111111111111111111111111111111111111111111111",
  374. map[string]uint64{"uint64_max_base2": math.MaxUint64},
  375. },
  376. {
  377. "uint64_maxint64: 9223372036854775807",
  378. map[string]uint64{"uint64_maxint64": math.MaxInt64},
  379. },
  380. {
  381. "uint64_underflow: -1",
  382. map[string]uint64{},
  383. },
  384. // float32
  385. {
  386. "float32_max: 3.40282346638528859811704183484516925440e+38",
  387. map[string]float32{"float32_max": math.MaxFloat32},
  388. },
  389. {
  390. "float32_nonzero: 1.401298464324817070923729583289916131280e-45",
  391. map[string]float32{"float32_nonzero": math.SmallestNonzeroFloat32},
  392. },
  393. {
  394. "float32_maxuint64: 18446744073709551615",
  395. map[string]float32{"float32_maxuint64": float32(math.MaxUint64)},
  396. },
  397. {
  398. "float32_maxuint64+1: 18446744073709551616",
  399. map[string]float32{"float32_maxuint64+1": float32(math.MaxUint64 + 1)},
  400. },
  401. // float64
  402. {
  403. "float64_max: 1.797693134862315708145274237317043567981e+308",
  404. map[string]float64{"float64_max": math.MaxFloat64},
  405. },
  406. {
  407. "float64_nonzero: 4.940656458412465441765687928682213723651e-324",
  408. map[string]float64{"float64_nonzero": math.SmallestNonzeroFloat64},
  409. },
  410. {
  411. "float64_maxuint64: 18446744073709551615",
  412. map[string]float64{"float64_maxuint64": float64(math.MaxUint64)},
  413. },
  414. {
  415. "float64_maxuint64+1: 18446744073709551616",
  416. map[string]float64{"float64_maxuint64+1": float64(math.MaxUint64 + 1)},
  417. },
  418. // Overflow cases.
  419. {
  420. "v: 4294967297",
  421. map[string]int32{},
  422. }, {
  423. "v: 128",
  424. map[string]int8{},
  425. },
  426. // Quoted values.
  427. {
  428. "'1': '\"2\"'",
  429. map[interface{}]interface{}{"1": "\"2\""},
  430. }, {
  431. "v:\n- A\n- 'B\n\n C'\n",
  432. map[string][]string{"v": []string{"A", "B\nC"}},
  433. },
  434. // Explicit tags.
  435. {
  436. "v: !!float '1.1'",
  437. map[string]interface{}{"v": 1.1},
  438. }, {
  439. "v: !!float 0",
  440. map[string]interface{}{"v": float64(0)},
  441. }, {
  442. "v: !!float -1",
  443. map[string]interface{}{"v": float64(-1)},
  444. }, {
  445. "v: !!null ''",
  446. map[string]interface{}{"v": nil},
  447. }, {
  448. "%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'",
  449. map[string]interface{}{"v": 1},
  450. },
  451. // Non-specific tag (Issue #75)
  452. {
  453. "v: ! test",
  454. map[string]interface{}{"v": "test"},
  455. },
  456. // Anchors and aliases.
  457. {
  458. "a: &x 1\nb: &y 2\nc: *x\nd: *y\n",
  459. &struct{ A, B, C, D int }{1, 2, 1, 2},
  460. }, {
  461. "a: &a {c: 1}\nb: *a",
  462. &struct {
  463. A, B struct {
  464. C int
  465. }
  466. }{struct{ C int }{1}, struct{ C int }{1}},
  467. }, {
  468. "a: &a [1, 2]\nb: *a",
  469. &struct{ B []int }{[]int{1, 2}},
  470. },
  471. // Bug #1133337
  472. {
  473. "foo: ''",
  474. map[string]*string{"foo": new(string)},
  475. }, {
  476. "foo: null",
  477. map[string]*string{"foo": nil},
  478. }, {
  479. "foo: null",
  480. map[string]string{"foo": ""},
  481. }, {
  482. "foo: null",
  483. map[string]interface{}{"foo": nil},
  484. },
  485. // Support for ~
  486. {
  487. "foo: ~",
  488. map[string]*string{"foo": nil},
  489. }, {
  490. "foo: ~",
  491. map[string]string{"foo": ""},
  492. }, {
  493. "foo: ~",
  494. map[string]interface{}{"foo": nil},
  495. },
  496. // Ignored field
  497. {
  498. "a: 1\nb: 2\n",
  499. &struct {
  500. A int
  501. B int "-"
  502. }{1, 0},
  503. },
  504. // Bug #1191981
  505. {
  506. "" +
  507. "%YAML 1.1\n" +
  508. "--- !!str\n" +
  509. `"Generic line break (no glyph)\n\` + "\n" +
  510. ` Generic line break (glyphed)\n\` + "\n" +
  511. ` Line separator\u2028\` + "\n" +
  512. ` Paragraph separator\u2029"` + "\n",
  513. "" +
  514. "Generic line break (no glyph)\n" +
  515. "Generic line break (glyphed)\n" +
  516. "Line separator\u2028Paragraph separator\u2029",
  517. },
  518. // Struct inlining
  519. {
  520. "a: 1\nb: 2\nc: 3\n",
  521. &struct {
  522. A int
  523. C inlineB `yaml:",inline"`
  524. }{1, inlineB{2, inlineC{3}}},
  525. },
  526. // Struct inlining as a pointer.
  527. {
  528. "a: 1\nb: 2\nc: 3\n",
  529. &struct {
  530. A int
  531. C *inlineB `yaml:",inline"`
  532. }{1, &inlineB{2, inlineC{3}}},
  533. }, {
  534. "a: 1\n",
  535. &struct {
  536. A int
  537. C *inlineB `yaml:",inline"`
  538. }{1, nil},
  539. }, {
  540. "a: 1\nc: 3\nd: 4\n",
  541. &struct {
  542. A int
  543. C *inlineD `yaml:",inline"`
  544. }{1, &inlineD{&inlineC{3}, 4}},
  545. },
  546. // Map inlining
  547. {
  548. "a: 1\nb: 2\nc: 3\n",
  549. &struct {
  550. A int
  551. C map[string]int `yaml:",inline"`
  552. }{1, map[string]int{"b": 2, "c": 3}},
  553. },
  554. // bug 1243827
  555. {
  556. "a: -b_c",
  557. map[string]interface{}{"a": "-b_c"},
  558. },
  559. {
  560. "a: +b_c",
  561. map[string]interface{}{"a": "+b_c"},
  562. },
  563. {
  564. "a: 50cent_of_dollar",
  565. map[string]interface{}{"a": "50cent_of_dollar"},
  566. },
  567. // issue #295 (allow scalars with colons in flow mappings and sequences)
  568. {
  569. "a: {b: https://github.com/go-yaml/yaml}",
  570. map[string]interface{}{"a": map[string]interface{}{
  571. "b": "https://github.com/go-yaml/yaml",
  572. }},
  573. },
  574. {
  575. "a: [https://github.com/go-yaml/yaml]",
  576. map[string]interface{}{"a": []interface{}{"https://github.com/go-yaml/yaml"}},
  577. },
  578. // Duration
  579. {
  580. "a: 3s",
  581. map[string]time.Duration{"a": 3 * time.Second},
  582. },
  583. // Issue #24.
  584. {
  585. "a: <foo>",
  586. map[string]string{"a": "<foo>"},
  587. },
  588. // Base 60 floats are obsolete and unsupported.
  589. {
  590. "a: 1:1\n",
  591. map[string]string{"a": "1:1"},
  592. },
  593. // Binary data.
  594. {
  595. "a: !!binary gIGC\n",
  596. map[string]string{"a": "\x80\x81\x82"},
  597. }, {
  598. "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
  599. map[string]string{"a": strings.Repeat("\x90", 54)},
  600. }, {
  601. "a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n",
  602. map[string]string{"a": strings.Repeat("\x00", 52)},
  603. },
  604. // Issue #39.
  605. {
  606. "a:\n b:\n c: d\n",
  607. map[string]struct{ B interface{} }{"a": {map[string]interface{}{"c": "d"}}},
  608. },
  609. // Custom map type.
  610. {
  611. "a: {b: c}",
  612. M{"a": M{"b": "c"}},
  613. },
  614. // Support encoding.TextUnmarshaler.
  615. {
  616. "a: 1.2.3.4\n",
  617. map[string]textUnmarshaler{"a": textUnmarshaler{S: "1.2.3.4"}},
  618. },
  619. {
  620. "a: 2015-02-24T18:19:39Z\n",
  621. map[string]textUnmarshaler{"a": textUnmarshaler{"2015-02-24T18:19:39Z"}},
  622. },
  623. // Timestamps
  624. {
  625. // Date only.
  626. "a: 2015-01-01\n",
  627. map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  628. },
  629. {
  630. // RFC3339
  631. "a: 2015-02-24T18:19:39.12Z\n",
  632. map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, .12e9, time.UTC)},
  633. },
  634. {
  635. // RFC3339 with short dates.
  636. "a: 2015-2-3T3:4:5Z",
  637. map[string]time.Time{"a": time.Date(2015, 2, 3, 3, 4, 5, 0, time.UTC)},
  638. },
  639. {
  640. // ISO8601 lower case t
  641. "a: 2015-02-24t18:19:39Z\n",
  642. map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
  643. },
  644. {
  645. // space separate, no time zone
  646. "a: 2015-02-24 18:19:39\n",
  647. map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
  648. },
  649. // Some cases not currently handled. Uncomment these when
  650. // the code is fixed.
  651. // {
  652. // // space separated with time zone
  653. // "a: 2001-12-14 21:59:43.10 -5",
  654. // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
  655. // },
  656. // {
  657. // // arbitrary whitespace between fields
  658. // "a: 2001-12-14 \t\t \t21:59:43.10 \t Z",
  659. // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
  660. // },
  661. {
  662. // explicit string tag
  663. "a: !!str 2015-01-01",
  664. map[string]interface{}{"a": "2015-01-01"},
  665. },
  666. {
  667. // explicit timestamp tag on quoted string
  668. "a: !!timestamp \"2015-01-01\"",
  669. map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  670. },
  671. {
  672. // explicit timestamp tag on unquoted string
  673. "a: !!timestamp 2015-01-01",
  674. map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  675. },
  676. {
  677. // quoted string that's a valid timestamp
  678. "a: \"2015-01-01\"",
  679. map[string]interface{}{"a": "2015-01-01"},
  680. },
  681. {
  682. // explicit timestamp tag into interface.
  683. "a: !!timestamp \"2015-01-01\"",
  684. map[string]interface{}{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  685. },
  686. {
  687. // implicit timestamp tag into interface.
  688. "a: 2015-01-01",
  689. map[string]interface{}{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  690. },
  691. // Encode empty lists as zero-length slices.
  692. {
  693. "a: []",
  694. &struct{ A []int }{[]int{}},
  695. },
  696. // UTF-16-LE
  697. {
  698. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n\x00",
  699. M{"ñoño": "very yes"},
  700. },
  701. // UTF-16-LE with surrogate.
  702. {
  703. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \x00=\xd8\xd4\xdf\n\x00",
  704. M{"ñoño": "very yes 🟔"},
  705. },
  706. // UTF-16-BE
  707. {
  708. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n",
  709. M{"ñoño": "very yes"},
  710. },
  711. // UTF-16-BE with surrogate.
  712. {
  713. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \xd8=\xdf\xd4\x00\n",
  714. M{"ñoño": "very yes 🟔"},
  715. },
  716. // This *is* in fact a float number, per the spec. #171 was a mistake.
  717. {
  718. "a: 123456e1\n",
  719. M{"a": 123456e1},
  720. }, {
  721. "a: 123456E1\n",
  722. M{"a": 123456e1},
  723. },
  724. // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes
  725. {
  726. "First occurrence: &anchor Foo\nSecond occurrence: *anchor\nOverride anchor: &anchor Bar\nReuse anchor: *anchor\n",
  727. map[string]interface{}{
  728. "First occurrence": "Foo",
  729. "Second occurrence": "Foo",
  730. "Override anchor": "Bar",
  731. "Reuse anchor": "Bar",
  732. },
  733. },
  734. // Single document with garbage following it.
  735. {
  736. "---\nhello\n...\n}not yaml",
  737. "hello",
  738. },
  739. // Comment scan exhausting the input buffer (issue #469).
  740. {
  741. "true\n#" + strings.Repeat(" ", 512*3),
  742. "true",
  743. }, {
  744. "true #" + strings.Repeat(" ", 512*3),
  745. "true",
  746. },
  747. // CRLF
  748. {
  749. "a: b\r\nc:\r\n- d\r\n- e\r\n",
  750. map[string]interface{}{
  751. "a": "b",
  752. "c": []interface{}{"d", "e"},
  753. },
  754. },
  755. }
  756. type M map[string]interface{}
  757. type inlineB struct {
  758. B int
  759. inlineC `yaml:",inline"`
  760. }
  761. type inlineC struct {
  762. C int
  763. }
  764. type inlineD struct {
  765. C *inlineC `yaml:",inline"`
  766. D int
  767. }
  768. func (s *S) TestUnmarshal(c *C) {
  769. for i, item := range unmarshalTests {
  770. c.Logf("test %d: %q", i, item.data)
  771. t := reflect.ValueOf(item.value).Type()
  772. value := reflect.New(t)
  773. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  774. if _, ok := err.(*yaml.TypeError); !ok {
  775. c.Assert(err, IsNil)
  776. }
  777. c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err))
  778. }
  779. }
  780. func (s *S) TestUnmarshalFullTimestamp(c *C) {
  781. // Full timestamp in same format as encoded. This is confirmed to be
  782. // properly decoded by Python as a timestamp as well.
  783. var str = "2015-02-24T18:19:39.123456789-03:00"
  784. var t interface{}
  785. err := yaml.Unmarshal([]byte(str), &t)
  786. c.Assert(err, IsNil)
  787. c.Assert(t, Equals, time.Date(2015, 2, 24, 18, 19, 39, 123456789, t.(time.Time).Location()))
  788. c.Assert(t.(time.Time).In(time.UTC), Equals, time.Date(2015, 2, 24, 21, 19, 39, 123456789, time.UTC))
  789. }
  790. func (s *S) TestDecoderSingleDocument(c *C) {
  791. // Test that Decoder.Decode works as expected on
  792. // all the unmarshal tests.
  793. for i, item := range unmarshalTests {
  794. c.Logf("test %d: %q", i, item.data)
  795. if item.data == "" {
  796. // Behaviour differs when there's no YAML.
  797. continue
  798. }
  799. t := reflect.ValueOf(item.value).Type()
  800. value := reflect.New(t)
  801. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(value.Interface())
  802. if _, ok := err.(*yaml.TypeError); !ok {
  803. c.Assert(err, IsNil)
  804. }
  805. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  806. }
  807. }
  808. var decoderTests = []struct {
  809. data string
  810. values []interface{}
  811. }{{
  812. "",
  813. nil,
  814. }, {
  815. "a: b",
  816. []interface{}{
  817. map[string]interface{}{"a": "b"},
  818. },
  819. }, {
  820. "---\na: b\n...\n",
  821. []interface{}{
  822. map[string]interface{}{"a": "b"},
  823. },
  824. }, {
  825. "---\n'hello'\n...\n---\ngoodbye\n...\n",
  826. []interface{}{
  827. "hello",
  828. "goodbye",
  829. },
  830. }}
  831. func (s *S) TestDecoder(c *C) {
  832. for i, item := range decoderTests {
  833. c.Logf("test %d: %q", i, item.data)
  834. var values []interface{}
  835. dec := yaml.NewDecoder(strings.NewReader(item.data))
  836. for {
  837. var value interface{}
  838. err := dec.Decode(&value)
  839. if err == io.EOF {
  840. break
  841. }
  842. c.Assert(err, IsNil)
  843. values = append(values, value)
  844. }
  845. c.Assert(values, DeepEquals, item.values)
  846. }
  847. }
  848. type errReader struct{}
  849. func (errReader) Read([]byte) (int, error) {
  850. return 0, errors.New("some read error")
  851. }
  852. func (s *S) TestDecoderReadError(c *C) {
  853. err := yaml.NewDecoder(errReader{}).Decode(&struct{}{})
  854. c.Assert(err, ErrorMatches, `yaml: input error: some read error`)
  855. }
  856. func (s *S) TestUnmarshalNaN(c *C) {
  857. value := map[string]interface{}{}
  858. err := yaml.Unmarshal([]byte("notanum: .NaN"), &value)
  859. c.Assert(err, IsNil)
  860. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  861. }
  862. func (s *S) TestUnmarshalDurationInt(c *C) {
  863. // Don't accept plain ints as durations as it's unclear (issue #200).
  864. var d time.Duration
  865. err := yaml.Unmarshal([]byte("123"), &d)
  866. c.Assert(err, ErrorMatches, "(?s).* line 1: cannot unmarshal !!int `123` into time.Duration")
  867. }
  868. var unmarshalErrorTests = []struct {
  869. data, error string
  870. }{
  871. {"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"},
  872. {"v: [A,", "yaml: line 1: did not find expected node content"},
  873. {"v:\n- [A,", "yaml: line 2: did not find expected node content"},
  874. {"a:\n- b: *,", "yaml: line 2: did not find expected alphabetic or numeric character"},
  875. {"a: *b\n", "yaml: unknown anchor 'b' referenced"},
  876. {"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
  877. {"value: -", "yaml: block sequence entries are not allowed in this context"},
  878. {"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
  879. {"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
  880. {"{{.}}", `yaml: invalid map key: map\[string]interface \{\}\{".":interface \{\}\(nil\)\}`},
  881. {"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`},
  882. {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
  883. {"a:\n 1:\nb\n 2:", ".*could not find expected ':'"},
  884. {"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"},
  885. {"#\n-\n{", "yaml: line 3: could not find expected ':'"}, // Issue #665
  886. {"0: [:!00 \xef", "yaml: incomplete UTF-8 octet sequence"}, // Issue #666
  887. {
  888. "a: &a [00,00,00,00,00,00,00,00,00]\n" +
  889. "b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" +
  890. "c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" +
  891. "d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" +
  892. "e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" +
  893. "f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" +
  894. "g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" +
  895. "h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" +
  896. "i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n",
  897. "yaml: document contains excessive aliasing",
  898. },
  899. }
  900. func (s *S) TestUnmarshalErrors(c *C) {
  901. for i, item := range unmarshalErrorTests {
  902. c.Logf("test %d: %q", i, item.data)
  903. var value interface{}
  904. err := yaml.Unmarshal([]byte(item.data), &value)
  905. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  906. }
  907. }
  908. func (s *S) TestDecoderErrors(c *C) {
  909. for _, item := range unmarshalErrorTests {
  910. var value interface{}
  911. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(&value)
  912. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  913. }
  914. }
  915. var unmarshalerTests = []struct {
  916. data, tag string
  917. value interface{}
  918. }{
  919. {"_: {hi: there}", "!!map", map[string]interface{}{"hi": "there"}},
  920. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  921. {"_: 10", "!!int", 10},
  922. {"_: null", "!!null", nil},
  923. {`_: BAR!`, "!!str", "BAR!"},
  924. {`_: "BAR!"`, "!!str", "BAR!"},
  925. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  926. {`_: ""`, "!!str", ""},
  927. }
  928. var unmarshalerResult = map[int]error{}
  929. type unmarshalerType struct {
  930. value interface{}
  931. }
  932. func (o *unmarshalerType) UnmarshalYAML(value *yaml.Node) error {
  933. if err := value.Decode(&o.value); err != nil {
  934. return err
  935. }
  936. if i, ok := o.value.(int); ok {
  937. if result, ok := unmarshalerResult[i]; ok {
  938. return result
  939. }
  940. }
  941. return nil
  942. }
  943. type unmarshalerPointer struct {
  944. Field *unmarshalerType "_"
  945. }
  946. type unmarshalerValue struct {
  947. Field unmarshalerType "_"
  948. }
  949. type unmarshalerInlined struct {
  950. Field *unmarshalerType "_"
  951. Inlined unmarshalerType `yaml:",inline"`
  952. }
  953. type unmarshalerInlinedTwice struct {
  954. InlinedTwice unmarshalerInlined `yaml:",inline"`
  955. }
  956. type obsoleteUnmarshalerType struct {
  957. value interface{}
  958. }
  959. func (o *obsoleteUnmarshalerType) UnmarshalYAML(unmarshal func(v interface{}) error) error {
  960. if err := unmarshal(&o.value); err != nil {
  961. return err
  962. }
  963. if i, ok := o.value.(int); ok {
  964. if result, ok := unmarshalerResult[i]; ok {
  965. return result
  966. }
  967. }
  968. return nil
  969. }
  970. type obsoleteUnmarshalerPointer struct {
  971. Field *obsoleteUnmarshalerType "_"
  972. }
  973. type obsoleteUnmarshalerValue struct {
  974. Field obsoleteUnmarshalerType "_"
  975. }
  976. func (s *S) TestUnmarshalerPointerField(c *C) {
  977. for _, item := range unmarshalerTests {
  978. obj := &unmarshalerPointer{}
  979. err := yaml.Unmarshal([]byte(item.data), obj)
  980. c.Assert(err, IsNil)
  981. if item.value == nil {
  982. c.Assert(obj.Field, IsNil)
  983. } else {
  984. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  985. c.Assert(obj.Field.value, DeepEquals, item.value)
  986. }
  987. }
  988. for _, item := range unmarshalerTests {
  989. obj := &obsoleteUnmarshalerPointer{}
  990. err := yaml.Unmarshal([]byte(item.data), obj)
  991. c.Assert(err, IsNil)
  992. if item.value == nil {
  993. c.Assert(obj.Field, IsNil)
  994. } else {
  995. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  996. c.Assert(obj.Field.value, DeepEquals, item.value)
  997. }
  998. }
  999. }
  1000. func (s *S) TestUnmarshalerValueField(c *C) {
  1001. for _, item := range unmarshalerTests {
  1002. obj := &obsoleteUnmarshalerValue{}
  1003. err := yaml.Unmarshal([]byte(item.data), obj)
  1004. c.Assert(err, IsNil)
  1005. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  1006. c.Assert(obj.Field.value, DeepEquals, item.value)
  1007. }
  1008. }
  1009. func (s *S) TestUnmarshalerInlinedField(c *C) {
  1010. obj := &unmarshalerInlined{}
  1011. err := yaml.Unmarshal([]byte("_: a\ninlined: b\n"), obj)
  1012. c.Assert(err, IsNil)
  1013. c.Assert(obj.Field, DeepEquals, &unmarshalerType{"a"})
  1014. c.Assert(obj.Inlined, DeepEquals, unmarshalerType{map[string]interface{}{"_": "a", "inlined": "b"}})
  1015. twc := &unmarshalerInlinedTwice{}
  1016. err = yaml.Unmarshal([]byte("_: a\ninlined: b\n"), twc)
  1017. c.Assert(err, IsNil)
  1018. c.Assert(twc.InlinedTwice.Field, DeepEquals, &unmarshalerType{"a"})
  1019. c.Assert(twc.InlinedTwice.Inlined, DeepEquals, unmarshalerType{map[string]interface{}{"_": "a", "inlined": "b"}})
  1020. }
  1021. func (s *S) TestUnmarshalerWholeDocument(c *C) {
  1022. obj := &obsoleteUnmarshalerType{}
  1023. err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
  1024. c.Assert(err, IsNil)
  1025. value, ok := obj.value.(map[string]interface{})
  1026. c.Assert(ok, Equals, true, Commentf("value: %#v", obj.value))
  1027. c.Assert(value["_"], DeepEquals, unmarshalerTests[0].value)
  1028. }
  1029. func (s *S) TestUnmarshalerTypeError(c *C) {
  1030. unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
  1031. unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
  1032. defer func() {
  1033. delete(unmarshalerResult, 2)
  1034. delete(unmarshalerResult, 4)
  1035. }()
  1036. type T struct {
  1037. Before int
  1038. After int
  1039. M map[string]*unmarshalerType
  1040. }
  1041. var v T
  1042. data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
  1043. err := yaml.Unmarshal([]byte(data), &v)
  1044. c.Assert(err, ErrorMatches, ""+
  1045. "yaml: unmarshal errors:\n"+
  1046. " line 1: cannot unmarshal !!str `A` into int\n"+
  1047. " foo\n"+
  1048. " bar\n"+
  1049. " line 1: cannot unmarshal !!str `B` into int")
  1050. c.Assert(v.M["abc"], NotNil)
  1051. c.Assert(v.M["def"], IsNil)
  1052. c.Assert(v.M["ghi"], NotNil)
  1053. c.Assert(v.M["jkl"], IsNil)
  1054. c.Assert(v.M["abc"].value, Equals, 1)
  1055. c.Assert(v.M["ghi"].value, Equals, 3)
  1056. }
  1057. func (s *S) TestObsoleteUnmarshalerTypeError(c *C) {
  1058. unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
  1059. unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
  1060. defer func() {
  1061. delete(unmarshalerResult, 2)
  1062. delete(unmarshalerResult, 4)
  1063. }()
  1064. type T struct {
  1065. Before int
  1066. After int
  1067. M map[string]*obsoleteUnmarshalerType
  1068. }
  1069. var v T
  1070. data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
  1071. err := yaml.Unmarshal([]byte(data), &v)
  1072. c.Assert(err, ErrorMatches, ""+
  1073. "yaml: unmarshal errors:\n"+
  1074. " line 1: cannot unmarshal !!str `A` into int\n"+
  1075. " foo\n"+
  1076. " bar\n"+
  1077. " line 1: cannot unmarshal !!str `B` into int")
  1078. c.Assert(v.M["abc"], NotNil)
  1079. c.Assert(v.M["def"], IsNil)
  1080. c.Assert(v.M["ghi"], NotNil)
  1081. c.Assert(v.M["jkl"], IsNil)
  1082. c.Assert(v.M["abc"].value, Equals, 1)
  1083. c.Assert(v.M["ghi"].value, Equals, 3)
  1084. }
  1085. type proxyTypeError struct{}
  1086. func (v *proxyTypeError) UnmarshalYAML(node *yaml.Node) error {
  1087. var s string
  1088. var a int32
  1089. var b int64
  1090. if err := node.Decode(&s); err != nil {
  1091. panic(err)
  1092. }
  1093. if s == "a" {
  1094. if err := node.Decode(&b); err == nil {
  1095. panic("should have failed")
  1096. }
  1097. return node.Decode(&a)
  1098. }
  1099. if err := node.Decode(&a); err == nil {
  1100. panic("should have failed")
  1101. }
  1102. return node.Decode(&b)
  1103. }
  1104. func (s *S) TestUnmarshalerTypeErrorProxying(c *C) {
  1105. type T struct {
  1106. Before int
  1107. After int
  1108. M map[string]*proxyTypeError
  1109. }
  1110. var v T
  1111. data := `{before: A, m: {abc: a, def: b}, after: B}`
  1112. err := yaml.Unmarshal([]byte(data), &v)
  1113. c.Assert(err, ErrorMatches, ""+
  1114. "yaml: unmarshal errors:\n"+
  1115. " line 1: cannot unmarshal !!str `A` into int\n"+
  1116. " line 1: cannot unmarshal !!str `a` into int32\n"+
  1117. " line 1: cannot unmarshal !!str `b` into int64\n"+
  1118. " line 1: cannot unmarshal !!str `B` into int")
  1119. }
  1120. type obsoleteProxyTypeError struct{}
  1121. func (v *obsoleteProxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error {
  1122. var s string
  1123. var a int32
  1124. var b int64
  1125. if err := unmarshal(&s); err != nil {
  1126. panic(err)
  1127. }
  1128. if s == "a" {
  1129. if err := unmarshal(&b); err == nil {
  1130. panic("should have failed")
  1131. }
  1132. return unmarshal(&a)
  1133. }
  1134. if err := unmarshal(&a); err == nil {
  1135. panic("should have failed")
  1136. }
  1137. return unmarshal(&b)
  1138. }
  1139. func (s *S) TestObsoleteUnmarshalerTypeErrorProxying(c *C) {
  1140. type T struct {
  1141. Before int
  1142. After int
  1143. M map[string]*obsoleteProxyTypeError
  1144. }
  1145. var v T
  1146. data := `{before: A, m: {abc: a, def: b}, after: B}`
  1147. err := yaml.Unmarshal([]byte(data), &v)
  1148. c.Assert(err, ErrorMatches, ""+
  1149. "yaml: unmarshal errors:\n"+
  1150. " line 1: cannot unmarshal !!str `A` into int\n"+
  1151. " line 1: cannot unmarshal !!str `a` into int32\n"+
  1152. " line 1: cannot unmarshal !!str `b` into int64\n"+
  1153. " line 1: cannot unmarshal !!str `B` into int")
  1154. }
  1155. var failingErr = errors.New("failingErr")
  1156. type failingUnmarshaler struct{}
  1157. func (ft *failingUnmarshaler) UnmarshalYAML(node *yaml.Node) error {
  1158. return failingErr
  1159. }
  1160. func (s *S) TestUnmarshalerError(c *C) {
  1161. err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{})
  1162. c.Assert(err, Equals, failingErr)
  1163. }
  1164. type obsoleteFailingUnmarshaler struct{}
  1165. func (ft *obsoleteFailingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  1166. return failingErr
  1167. }
  1168. func (s *S) TestObsoleteUnmarshalerError(c *C) {
  1169. err := yaml.Unmarshal([]byte("a: b"), &obsoleteFailingUnmarshaler{})
  1170. c.Assert(err, Equals, failingErr)
  1171. }
  1172. type sliceUnmarshaler []int
  1173. func (su *sliceUnmarshaler) UnmarshalYAML(node *yaml.Node) error {
  1174. var slice []int
  1175. err := node.Decode(&slice)
  1176. if err == nil {
  1177. *su = slice
  1178. return nil
  1179. }
  1180. var intVal int
  1181. err = node.Decode(&intVal)
  1182. if err == nil {
  1183. *su = []int{intVal}
  1184. return nil
  1185. }
  1186. return err
  1187. }
  1188. func (s *S) TestUnmarshalerRetry(c *C) {
  1189. var su sliceUnmarshaler
  1190. err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
  1191. c.Assert(err, IsNil)
  1192. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1, 2, 3}))
  1193. err = yaml.Unmarshal([]byte("1"), &su)
  1194. c.Assert(err, IsNil)
  1195. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1}))
  1196. }
  1197. type obsoleteSliceUnmarshaler []int
  1198. func (su *obsoleteSliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  1199. var slice []int
  1200. err := unmarshal(&slice)
  1201. if err == nil {
  1202. *su = slice
  1203. return nil
  1204. }
  1205. var intVal int
  1206. err = unmarshal(&intVal)
  1207. if err == nil {
  1208. *su = []int{intVal}
  1209. return nil
  1210. }
  1211. return err
  1212. }
  1213. func (s *S) TestObsoleteUnmarshalerRetry(c *C) {
  1214. var su obsoleteSliceUnmarshaler
  1215. err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
  1216. c.Assert(err, IsNil)
  1217. c.Assert(su, DeepEquals, obsoleteSliceUnmarshaler([]int{1, 2, 3}))
  1218. err = yaml.Unmarshal([]byte("1"), &su)
  1219. c.Assert(err, IsNil)
  1220. c.Assert(su, DeepEquals, obsoleteSliceUnmarshaler([]int{1}))
  1221. }
  1222. // From http://yaml.org/type/merge.html
  1223. var mergeTests = `
  1224. anchors:
  1225. list:
  1226. - &CENTER { "x": 1, "y": 2 }
  1227. - &LEFT { "x": 0, "y": 2 }
  1228. - &BIG { "r": 10 }
  1229. - &SMALL { "r": 1 }
  1230. # All the following maps are equal:
  1231. plain:
  1232. # Explicit keys
  1233. "x": 1
  1234. "y": 2
  1235. "r": 10
  1236. label: center/big
  1237. mergeOne:
  1238. # Merge one map
  1239. << : *CENTER
  1240. "r": 10
  1241. label: center/big
  1242. mergeMultiple:
  1243. # Merge multiple maps
  1244. << : [ *CENTER, *BIG ]
  1245. label: center/big
  1246. override:
  1247. # Override
  1248. << : [ *BIG, *LEFT, *SMALL ]
  1249. "x": 1
  1250. label: center/big
  1251. shortTag:
  1252. # Explicit short merge tag
  1253. !!merge "<<" : [ *CENTER, *BIG ]
  1254. label: center/big
  1255. longTag:
  1256. # Explicit merge long tag
  1257. !<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ]
  1258. label: center/big
  1259. inlineMap:
  1260. # Inlined map
  1261. << : {"x": 1, "y": 2, "r": 10}
  1262. label: center/big
  1263. inlineSequenceMap:
  1264. # Inlined map in sequence
  1265. << : [ *CENTER, {"r": 10} ]
  1266. label: center/big
  1267. `
  1268. func (s *S) TestMerge(c *C) {
  1269. var want = map[string]interface{}{
  1270. "x": 1,
  1271. "y": 2,
  1272. "r": 10,
  1273. "label": "center/big",
  1274. }
  1275. wantStringMap := make(map[string]interface{})
  1276. for k, v := range want {
  1277. wantStringMap[fmt.Sprintf("%v", k)] = v
  1278. }
  1279. var m map[interface{}]interface{}
  1280. err := yaml.Unmarshal([]byte(mergeTests), &m)
  1281. c.Assert(err, IsNil)
  1282. for name, test := range m {
  1283. if name == "anchors" {
  1284. continue
  1285. }
  1286. if name == "plain" {
  1287. c.Assert(test, DeepEquals, wantStringMap, Commentf("test %q failed", name))
  1288. continue
  1289. }
  1290. c.Assert(test, DeepEquals, want, Commentf("test %q failed", name))
  1291. }
  1292. }
  1293. func (s *S) TestMergeStruct(c *C) {
  1294. type Data struct {
  1295. X, Y, R int
  1296. Label string
  1297. }
  1298. want := Data{1, 2, 10, "center/big"}
  1299. var m map[string]Data
  1300. err := yaml.Unmarshal([]byte(mergeTests), &m)
  1301. c.Assert(err, IsNil)
  1302. for name, test := range m {
  1303. if name == "anchors" {
  1304. continue
  1305. }
  1306. c.Assert(test, Equals, want, Commentf("test %q failed", name))
  1307. }
  1308. }
  1309. var mergeTestsNested = `
  1310. mergeouter1: &mergeouter1
  1311. d: 40
  1312. e: 50
  1313. mergeouter2: &mergeouter2
  1314. e: 5
  1315. f: 6
  1316. g: 70
  1317. mergeinner1: &mergeinner1
  1318. <<: *mergeouter1
  1319. inner:
  1320. a: 1
  1321. b: 2
  1322. mergeinner2: &mergeinner2
  1323. <<: *mergeouter2
  1324. inner:
  1325. a: -1
  1326. b: -2
  1327. outer:
  1328. <<: [*mergeinner1, *mergeinner2]
  1329. f: 60
  1330. inner:
  1331. a: 10
  1332. `
  1333. func (s *S) TestMergeNestedStruct(c *C) {
  1334. // Issue #818: Merging used to just unmarshal twice on the target
  1335. // value, which worked for maps as these were replaced by the new map,
  1336. // but not on struct values as these are preserved. This resulted in
  1337. // the nested data from the merged map to be mixed up with the data
  1338. // from the map being merged into.
  1339. //
  1340. // This test also prevents two potential bugs from showing up:
  1341. //
  1342. // 1) A simple implementation might just zero out the nested value
  1343. // before unmarshaling the second time, but this would clobber previous
  1344. // data that is usually respected ({C: 30} below).
  1345. //
  1346. // 2) A simple implementation might attempt to handle the key skipping
  1347. // directly by iterating over the merging map without recursion, but
  1348. // there are more complex cases that require recursion.
  1349. //
  1350. // Quick summary of the fields:
  1351. //
  1352. // - A must come from outer and not overriden
  1353. // - B must not be set as its in the ignored merge
  1354. // - C should still be set as it's preset in the value
  1355. // - D should be set from the recursive merge
  1356. // - E should be set from the first recursive merge, ignored on the second
  1357. // - F should be set in the inlined map from outer, ignored later
  1358. // - G should be set in the inlined map from the second recursive merge
  1359. //
  1360. type Inner struct {
  1361. A, B, C int
  1362. }
  1363. type Outer struct {
  1364. D, E int
  1365. Inner Inner
  1366. Inline map[string]int `yaml:",inline"`
  1367. }
  1368. type Data struct {
  1369. Outer Outer
  1370. }
  1371. test := Data{Outer{0, 0, Inner{C: 30}, nil}}
  1372. want := Data{Outer{40, 50, Inner{A: 10, C: 30}, map[string]int{"f": 60, "g": 70}}}
  1373. err := yaml.Unmarshal([]byte(mergeTestsNested), &test)
  1374. c.Assert(err, IsNil)
  1375. c.Assert(test, DeepEquals, want)
  1376. // Repeat test with a map.
  1377. var testm map[string]interface{}
  1378. var wantm = map[string]interface {} {
  1379. "f": 60,
  1380. "inner": map[string]interface{}{
  1381. "a": 10,
  1382. },
  1383. "d": 40,
  1384. "e": 50,
  1385. "g": 70,
  1386. }
  1387. err = yaml.Unmarshal([]byte(mergeTestsNested), &testm)
  1388. c.Assert(err, IsNil)
  1389. c.Assert(testm["outer"], DeepEquals, wantm)
  1390. }
  1391. var unmarshalNullTests = []struct {
  1392. input string
  1393. pristine, expected func() interface{}
  1394. }{{
  1395. "null",
  1396. func() interface{} { var v interface{}; v = "v"; return &v },
  1397. func() interface{} { var v interface{}; v = nil; return &v },
  1398. }, {
  1399. "null",
  1400. func() interface{} { var s = "s"; return &s },
  1401. func() interface{} { var s = "s"; return &s },
  1402. }, {
  1403. "null",
  1404. func() interface{} { var s = "s"; sptr := &s; return &sptr },
  1405. func() interface{} { var sptr *string; return &sptr },
  1406. }, {
  1407. "null",
  1408. func() interface{} { var i = 1; return &i },
  1409. func() interface{} { var i = 1; return &i },
  1410. }, {
  1411. "null",
  1412. func() interface{} { var i = 1; iptr := &i; return &iptr },
  1413. func() interface{} { var iptr *int; return &iptr },
  1414. }, {
  1415. "null",
  1416. func() interface{} { var m = map[string]int{"s": 1}; return &m },
  1417. func() interface{} { var m map[string]int; return &m },
  1418. }, {
  1419. "null",
  1420. func() interface{} { var m = map[string]int{"s": 1}; return m },
  1421. func() interface{} { var m = map[string]int{"s": 1}; return m },
  1422. }, {
  1423. "s2: null\ns3: null",
  1424. func() interface{} { var m = map[string]int{"s1": 1, "s2": 2}; return m },
  1425. func() interface{} { var m = map[string]int{"s1": 1, "s2": 2, "s3": 0}; return m },
  1426. }, {
  1427. "s2: null\ns3: null",
  1428. func() interface{} { var m = map[string]interface{}{"s1": 1, "s2": 2}; return m },
  1429. func() interface{} { var m = map[string]interface{}{"s1": 1, "s2": nil, "s3": nil}; return m },
  1430. }}
  1431. func (s *S) TestUnmarshalNull(c *C) {
  1432. for _, test := range unmarshalNullTests {
  1433. pristine := test.pristine()
  1434. expected := test.expected()
  1435. err := yaml.Unmarshal([]byte(test.input), pristine)
  1436. c.Assert(err, IsNil)
  1437. c.Assert(pristine, DeepEquals, expected)
  1438. }
  1439. }
  1440. func (s *S) TestUnmarshalPreservesData(c *C) {
  1441. var v struct {
  1442. A, B int
  1443. C int `yaml:"-"`
  1444. }
  1445. v.A = 42
  1446. v.C = 88
  1447. err := yaml.Unmarshal([]byte("---"), &v)
  1448. c.Assert(err, IsNil)
  1449. c.Assert(v.A, Equals, 42)
  1450. c.Assert(v.B, Equals, 0)
  1451. c.Assert(v.C, Equals, 88)
  1452. err = yaml.Unmarshal([]byte("b: 21\nc: 99"), &v)
  1453. c.Assert(err, IsNil)
  1454. c.Assert(v.A, Equals, 42)
  1455. c.Assert(v.B, Equals, 21)
  1456. c.Assert(v.C, Equals, 88)
  1457. }
  1458. func (s *S) TestUnmarshalSliceOnPreset(c *C) {
  1459. // Issue #48.
  1460. v := struct{ A []int }{[]int{1}}
  1461. yaml.Unmarshal([]byte("a: [2]"), &v)
  1462. c.Assert(v.A, DeepEquals, []int{2})
  1463. }
  1464. var unmarshalStrictTests = []struct {
  1465. known bool
  1466. unique bool
  1467. data string
  1468. value interface{}
  1469. error string
  1470. }{{
  1471. known: true,
  1472. data: "a: 1\nc: 2\n",
  1473. value: struct{ A, B int }{A: 1},
  1474. error: `yaml: unmarshal errors:\n line 2: field c not found in type struct { A int; B int }`,
  1475. }, {
  1476. unique: true,
  1477. data: "a: 1\nb: 2\na: 3\n",
  1478. value: struct{ A, B int }{A: 3, B: 2},
  1479. error: `yaml: unmarshal errors:\n line 3: mapping key "a" already defined at line 1`,
  1480. }, {
  1481. unique: true,
  1482. data: "c: 3\na: 1\nb: 2\nc: 4\n",
  1483. value: struct {
  1484. A int
  1485. inlineB `yaml:",inline"`
  1486. }{
  1487. A: 1,
  1488. inlineB: inlineB{
  1489. B: 2,
  1490. inlineC: inlineC{
  1491. C: 4,
  1492. },
  1493. },
  1494. },
  1495. error: `yaml: unmarshal errors:\n line 4: mapping key "c" already defined at line 1`,
  1496. }, {
  1497. unique: true,
  1498. data: "c: 0\na: 1\nb: 2\nc: 1\n",
  1499. value: struct {
  1500. A int
  1501. inlineB `yaml:",inline"`
  1502. }{
  1503. A: 1,
  1504. inlineB: inlineB{
  1505. B: 2,
  1506. inlineC: inlineC{
  1507. C: 1,
  1508. },
  1509. },
  1510. },
  1511. error: `yaml: unmarshal errors:\n line 4: mapping key "c" already defined at line 1`,
  1512. }, {
  1513. unique: true,
  1514. data: "c: 1\na: 1\nb: 2\nc: 3\n",
  1515. value: struct {
  1516. A int
  1517. M map[string]interface{} `yaml:",inline"`
  1518. }{
  1519. A: 1,
  1520. M: map[string]interface{}{
  1521. "b": 2,
  1522. "c": 3,
  1523. },
  1524. },
  1525. error: `yaml: unmarshal errors:\n line 4: mapping key "c" already defined at line 1`,
  1526. }, {
  1527. unique: true,
  1528. data: "a: 1\n9: 2\nnull: 3\n9: 4",
  1529. value: map[interface{}]interface{}{
  1530. "a": 1,
  1531. nil: 3,
  1532. 9: 4,
  1533. },
  1534. error: `yaml: unmarshal errors:\n line 4: mapping key "9" already defined at line 2`,
  1535. }}
  1536. func (s *S) TestUnmarshalKnownFields(c *C) {
  1537. for i, item := range unmarshalStrictTests {
  1538. c.Logf("test %d: %q", i, item.data)
  1539. // First test that normal Unmarshal unmarshals to the expected value.
  1540. if !item.unique {
  1541. t := reflect.ValueOf(item.value).Type()
  1542. value := reflect.New(t)
  1543. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  1544. c.Assert(err, Equals, nil)
  1545. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  1546. }
  1547. // Then test that it fails on the same thing with KnownFields on.
  1548. t := reflect.ValueOf(item.value).Type()
  1549. value := reflect.New(t)
  1550. dec := yaml.NewDecoder(bytes.NewBuffer([]byte(item.data)))
  1551. dec.KnownFields(item.known)
  1552. err := dec.Decode(value.Interface())
  1553. c.Assert(err, ErrorMatches, item.error)
  1554. }
  1555. }
  1556. type textUnmarshaler struct {
  1557. S string
  1558. }
  1559. func (t *textUnmarshaler) UnmarshalText(s []byte) error {
  1560. t.S = string(s)
  1561. return nil
  1562. }
  1563. func (s *S) TestFuzzCrashers(c *C) {
  1564. cases := []string{
  1565. // runtime error: index out of range
  1566. "\"\\0\\\r\n",
  1567. // should not happen
  1568. " 0: [\n] 0",
  1569. "? ? \"\n\" 0",
  1570. " - {\n000}0",
  1571. "0:\n 0: [0\n] 0",
  1572. " - \"\n000\"0",
  1573. " - \"\n000\"\"",
  1574. "0:\n - {\n000}0",
  1575. "0:\n - \"\n000\"0",
  1576. "0:\n - \"\n000\"\"",
  1577. // runtime error: index out of range
  1578. " \ufeff\n",
  1579. "? \ufeff\n",
  1580. "? \ufeff:\n",
  1581. "0: \ufeff\n",
  1582. "? \ufeff: \ufeff\n",
  1583. }
  1584. for _, data := range cases {
  1585. var v interface{}
  1586. _ = yaml.Unmarshal([]byte(data), &v)
  1587. }
  1588. }
  1589. //var data []byte
  1590. //func init() {
  1591. // var err error
  1592. // data, err = ioutil.ReadFile("/tmp/file.yaml")
  1593. // if err != nil {
  1594. // panic(err)
  1595. // }
  1596. //}
  1597. //
  1598. //func (s *S) BenchmarkUnmarshal(c *C) {
  1599. // var err error
  1600. // for i := 0; i < c.N; i++ {
  1601. // var v map[string]interface{}
  1602. // err = yaml.Unmarshal(data, &v)
  1603. // }
  1604. // if err != nil {
  1605. // panic(err)
  1606. // }
  1607. //}
  1608. //
  1609. //func (s *S) BenchmarkMarshal(c *C) {
  1610. // var v map[string]interface{}
  1611. // yaml.Unmarshal(data, &v)
  1612. // c.ResetTimer()
  1613. // for i := 0; i < c.N; i++ {
  1614. // yaml.Marshal(&v)
  1615. // }
  1616. //}