config_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. package conf
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/zeromicro/go-zero/core/fs"
  7. "github.com/zeromicro/go-zero/core/hash"
  8. )
  9. var dupErr conflictKeyError
  10. func TestLoadConfig_notExists(t *testing.T) {
  11. assert.NotNil(t, Load("not_a_file", nil))
  12. }
  13. func TestLoadConfig_notRecogFile(t *testing.T) {
  14. filename, err := fs.TempFilenameWithText("hello")
  15. assert.Nil(t, err)
  16. defer os.Remove(filename)
  17. assert.NotNil(t, LoadConfig(filename, nil))
  18. }
  19. func TestConfigJson(t *testing.T) {
  20. tests := []string{
  21. ".json",
  22. ".yaml",
  23. ".yml",
  24. }
  25. text := `{
  26. "a": "foo",
  27. "b": 1,
  28. "c": "${FOO}",
  29. "d": "abcd!@#$112"
  30. }`
  31. for _, test := range tests {
  32. test := test
  33. t.Run(test, func(t *testing.T) {
  34. os.Setenv("FOO", "2")
  35. defer os.Unsetenv("FOO")
  36. tmpfile, err := createTempFile(test, text)
  37. assert.Nil(t, err)
  38. defer os.Remove(tmpfile)
  39. var val struct {
  40. A string `json:"a"`
  41. B int `json:"b"`
  42. C string `json:"c"`
  43. D string `json:"d"`
  44. }
  45. MustLoad(tmpfile, &val)
  46. assert.Equal(t, "foo", val.A)
  47. assert.Equal(t, 1, val.B)
  48. assert.Equal(t, "${FOO}", val.C)
  49. assert.Equal(t, "abcd!@#$112", val.D)
  50. })
  51. }
  52. }
  53. func TestLoadFromJsonBytesArray(t *testing.T) {
  54. input := []byte(`{"users": [{"name": "foo"}, {"Name": "bar"}]}`)
  55. var val struct {
  56. Users []struct {
  57. Name string
  58. }
  59. }
  60. assert.NoError(t, LoadConfigFromJsonBytes(input, &val))
  61. var expect []string
  62. for _, user := range val.Users {
  63. expect = append(expect, user.Name)
  64. }
  65. assert.EqualValues(t, []string{"foo", "bar"}, expect)
  66. }
  67. func TestConfigToml(t *testing.T) {
  68. text := `a = "foo"
  69. b = 1
  70. c = "${FOO}"
  71. d = "abcd!@#$112"
  72. `
  73. os.Setenv("FOO", "2")
  74. defer os.Unsetenv("FOO")
  75. tmpfile, err := createTempFile(".toml", text)
  76. assert.Nil(t, err)
  77. defer os.Remove(tmpfile)
  78. var val struct {
  79. A string `json:"a"`
  80. B int `json:"b"`
  81. C string `json:"c"`
  82. D string `json:"d"`
  83. }
  84. MustLoad(tmpfile, &val)
  85. assert.Equal(t, "foo", val.A)
  86. assert.Equal(t, 1, val.B)
  87. assert.Equal(t, "${FOO}", val.C)
  88. assert.Equal(t, "abcd!@#$112", val.D)
  89. }
  90. func TestConfigOptional(t *testing.T) {
  91. text := `a = "foo"
  92. b = 1
  93. c = "FOO"
  94. d = "abcd"
  95. `
  96. tmpfile, err := createTempFile(".toml", text)
  97. assert.Nil(t, err)
  98. defer os.Remove(tmpfile)
  99. var val struct {
  100. A string `json:"a"`
  101. B int `json:"b,optional"`
  102. C string `json:"c,optional=B"`
  103. D string `json:"d,optional=b"`
  104. }
  105. if assert.NoError(t, Load(tmpfile, &val)) {
  106. assert.Equal(t, "foo", val.A)
  107. assert.Equal(t, 1, val.B)
  108. assert.Equal(t, "FOO", val.C)
  109. assert.Equal(t, "abcd", val.D)
  110. }
  111. }
  112. func TestConfigWithLower(t *testing.T) {
  113. text := `a = "foo"
  114. b = 1
  115. `
  116. tmpfile, err := createTempFile(".toml", text)
  117. assert.Nil(t, err)
  118. defer os.Remove(tmpfile)
  119. var val struct {
  120. A string `json:"a"`
  121. b int
  122. }
  123. if assert.NoError(t, Load(tmpfile, &val)) {
  124. assert.Equal(t, "foo", val.A)
  125. assert.Equal(t, 0, val.b)
  126. }
  127. }
  128. func TestConfigJsonCanonical(t *testing.T) {
  129. text := []byte(`{"a": "foo", "B": "bar"}`)
  130. var val1 struct {
  131. A string `json:"a"`
  132. B string `json:"b"`
  133. }
  134. var val2 struct {
  135. A string
  136. B string
  137. }
  138. assert.NoError(t, LoadFromJsonBytes(text, &val1))
  139. assert.Equal(t, "foo", val1.A)
  140. assert.Equal(t, "bar", val1.B)
  141. assert.NoError(t, LoadFromJsonBytes(text, &val2))
  142. assert.Equal(t, "foo", val2.A)
  143. assert.Equal(t, "bar", val2.B)
  144. }
  145. func TestConfigTomlCanonical(t *testing.T) {
  146. text := []byte(`a = "foo"
  147. B = "bar"`)
  148. var val1 struct {
  149. A string `json:"a"`
  150. B string `json:"b"`
  151. }
  152. var val2 struct {
  153. A string
  154. B string
  155. }
  156. assert.NoError(t, LoadFromTomlBytes(text, &val1))
  157. assert.Equal(t, "foo", val1.A)
  158. assert.Equal(t, "bar", val1.B)
  159. assert.NoError(t, LoadFromTomlBytes(text, &val2))
  160. assert.Equal(t, "foo", val2.A)
  161. assert.Equal(t, "bar", val2.B)
  162. }
  163. func TestConfigYamlCanonical(t *testing.T) {
  164. text := []byte(`a: foo
  165. B: bar`)
  166. var val1 struct {
  167. A string `json:"a"`
  168. B string `json:"b"`
  169. }
  170. var val2 struct {
  171. A string
  172. B string
  173. }
  174. assert.NoError(t, LoadConfigFromYamlBytes(text, &val1))
  175. assert.Equal(t, "foo", val1.A)
  176. assert.Equal(t, "bar", val1.B)
  177. assert.NoError(t, LoadFromYamlBytes(text, &val2))
  178. assert.Equal(t, "foo", val2.A)
  179. assert.Equal(t, "bar", val2.B)
  180. }
  181. func TestConfigTomlEnv(t *testing.T) {
  182. text := `a = "foo"
  183. b = 1
  184. c = "${FOO}"
  185. d = "abcd!@#112"
  186. `
  187. os.Setenv("FOO", "2")
  188. defer os.Unsetenv("FOO")
  189. tmpfile, err := createTempFile(".toml", text)
  190. assert.Nil(t, err)
  191. defer os.Remove(tmpfile)
  192. var val struct {
  193. A string `json:"a"`
  194. B int `json:"b"`
  195. C string `json:"c"`
  196. D string `json:"d"`
  197. }
  198. MustLoad(tmpfile, &val, UseEnv())
  199. assert.Equal(t, "foo", val.A)
  200. assert.Equal(t, 1, val.B)
  201. assert.Equal(t, "2", val.C)
  202. assert.Equal(t, "abcd!@#112", val.D)
  203. }
  204. func TestConfigJsonEnv(t *testing.T) {
  205. tests := []string{
  206. ".json",
  207. ".yaml",
  208. ".yml",
  209. }
  210. text := `{
  211. "a": "foo",
  212. "b": 1,
  213. "c": "${FOO}",
  214. "d": "abcd!@#$a12 3"
  215. }`
  216. for _, test := range tests {
  217. test := test
  218. t.Run(test, func(t *testing.T) {
  219. os.Setenv("FOO", "2")
  220. defer os.Unsetenv("FOO")
  221. tmpfile, err := createTempFile(test, text)
  222. assert.Nil(t, err)
  223. defer os.Remove(tmpfile)
  224. var val struct {
  225. A string `json:"a"`
  226. B int `json:"b"`
  227. C string `json:"c"`
  228. D string `json:"d"`
  229. }
  230. MustLoad(tmpfile, &val, UseEnv())
  231. assert.Equal(t, "foo", val.A)
  232. assert.Equal(t, 1, val.B)
  233. assert.Equal(t, "2", val.C)
  234. assert.Equal(t, "abcd!@# 3", val.D)
  235. })
  236. }
  237. }
  238. func TestToCamelCase(t *testing.T) {
  239. tests := []struct {
  240. input string
  241. expect string
  242. }{
  243. {
  244. input: "",
  245. expect: "",
  246. },
  247. {
  248. input: "A",
  249. expect: "a",
  250. },
  251. {
  252. input: "a",
  253. expect: "a",
  254. },
  255. {
  256. input: "hello_world",
  257. expect: "hello_world",
  258. },
  259. {
  260. input: "Hello_world",
  261. expect: "hello_world",
  262. },
  263. {
  264. input: "hello_World",
  265. expect: "hello_world",
  266. },
  267. {
  268. input: "helloWorld",
  269. expect: "helloworld",
  270. },
  271. {
  272. input: "HelloWorld",
  273. expect: "helloworld",
  274. },
  275. {
  276. input: "hello World",
  277. expect: "hello world",
  278. },
  279. {
  280. input: "Hello World",
  281. expect: "hello world",
  282. },
  283. {
  284. input: "Hello World",
  285. expect: "hello world",
  286. },
  287. {
  288. input: "Hello World foo_bar",
  289. expect: "hello world foo_bar",
  290. },
  291. {
  292. input: "Hello World foo_Bar",
  293. expect: "hello world foo_bar",
  294. },
  295. {
  296. input: "Hello World Foo_bar",
  297. expect: "hello world foo_bar",
  298. },
  299. {
  300. input: "Hello World Foo_Bar",
  301. expect: "hello world foo_bar",
  302. },
  303. {
  304. input: "Hello.World Foo_Bar",
  305. expect: "hello.world foo_bar",
  306. },
  307. {
  308. input: "你好 World Foo_Bar",
  309. expect: "你好 world foo_bar",
  310. },
  311. }
  312. for _, test := range tests {
  313. test := test
  314. t.Run(test.input, func(t *testing.T) {
  315. assert.Equal(t, test.expect, toLowerCase(test.input))
  316. })
  317. }
  318. }
  319. func TestLoadFromJsonBytesError(t *testing.T) {
  320. var val struct{}
  321. assert.Error(t, LoadFromJsonBytes([]byte(`hello`), &val))
  322. }
  323. func TestLoadFromTomlBytesError(t *testing.T) {
  324. var val struct{}
  325. assert.Error(t, LoadFromTomlBytes([]byte(`hello`), &val))
  326. }
  327. func TestLoadFromYamlBytesError(t *testing.T) {
  328. var val struct{}
  329. assert.Error(t, LoadFromYamlBytes([]byte(`':hello`), &val))
  330. }
  331. func TestLoadFromYamlBytes(t *testing.T) {
  332. input := []byte(`layer1:
  333. layer2:
  334. layer3: foo`)
  335. var val struct {
  336. Layer1 struct {
  337. Layer2 struct {
  338. Layer3 string
  339. }
  340. }
  341. }
  342. assert.NoError(t, LoadFromYamlBytes(input, &val))
  343. assert.Equal(t, "foo", val.Layer1.Layer2.Layer3)
  344. }
  345. func TestLoadFromYamlBytesTerm(t *testing.T) {
  346. input := []byte(`layer1:
  347. layer2:
  348. tls_conf: foo`)
  349. var val struct {
  350. Layer1 struct {
  351. Layer2 struct {
  352. Layer3 string `json:"tls_conf"`
  353. }
  354. }
  355. }
  356. assert.NoError(t, LoadFromYamlBytes(input, &val))
  357. assert.Equal(t, "foo", val.Layer1.Layer2.Layer3)
  358. }
  359. func TestLoadFromYamlBytesLayers(t *testing.T) {
  360. input := []byte(`layer1:
  361. layer2:
  362. layer3: foo`)
  363. var val struct {
  364. Value string `json:"Layer1.Layer2.Layer3"`
  365. }
  366. assert.NoError(t, LoadFromYamlBytes(input, &val))
  367. assert.Equal(t, "foo", val.Value)
  368. }
  369. func TestLoadFromYamlItemOverlay(t *testing.T) {
  370. type (
  371. Redis struct {
  372. Host string
  373. Port int
  374. }
  375. RedisKey struct {
  376. Redis
  377. Key string
  378. }
  379. Server struct {
  380. Redis RedisKey
  381. }
  382. TestConfig struct {
  383. Server
  384. Redis Redis
  385. }
  386. )
  387. input := []byte(`Redis:
  388. Host: localhost
  389. Port: 6379
  390. Key: test
  391. `)
  392. var c TestConfig
  393. assert.ErrorAs(t, LoadFromYamlBytes(input, &c), &dupErr)
  394. }
  395. func TestLoadFromYamlItemOverlayReverse(t *testing.T) {
  396. type (
  397. Redis struct {
  398. Host string
  399. Port int
  400. }
  401. RedisKey struct {
  402. Redis
  403. Key string
  404. }
  405. Server struct {
  406. Redis Redis
  407. }
  408. TestConfig struct {
  409. Redis RedisKey
  410. Server
  411. }
  412. )
  413. input := []byte(`Redis:
  414. Host: localhost
  415. Port: 6379
  416. Key: test
  417. `)
  418. var c TestConfig
  419. assert.ErrorAs(t, LoadFromYamlBytes(input, &c), &dupErr)
  420. }
  421. func TestLoadFromYamlItemOverlayWithMap(t *testing.T) {
  422. type (
  423. Redis struct {
  424. Host string
  425. Port int
  426. }
  427. RedisKey struct {
  428. Redis
  429. Key string
  430. }
  431. Server struct {
  432. Redis RedisKey
  433. }
  434. TestConfig struct {
  435. Server
  436. Redis map[string]interface{}
  437. }
  438. )
  439. input := []byte(`Redis:
  440. Host: localhost
  441. Port: 6379
  442. Key: test
  443. `)
  444. var c TestConfig
  445. assert.ErrorAs(t, LoadFromYamlBytes(input, &c), &dupErr)
  446. }
  447. func TestUnmarshalJsonBytesMap(t *testing.T) {
  448. input := []byte(`{"foo":{"/mtproto.RPCTos": "bff.bff","bar":"baz"}}`)
  449. var val struct {
  450. Foo map[string]string
  451. }
  452. assert.NoError(t, LoadFromJsonBytes(input, &val))
  453. assert.Equal(t, "bff.bff", val.Foo["/mtproto.RPCTos"])
  454. assert.Equal(t, "baz", val.Foo["bar"])
  455. }
  456. func TestUnmarshalJsonBytesMapWithSliceElements(t *testing.T) {
  457. input := []byte(`{"foo":{"/mtproto.RPCTos": ["bff.bff", "any"],"bar":["baz", "qux"]}}`)
  458. var val struct {
  459. Foo map[string][]string
  460. }
  461. assert.NoError(t, LoadFromJsonBytes(input, &val))
  462. assert.EqualValues(t, []string{"bff.bff", "any"}, val.Foo["/mtproto.RPCTos"])
  463. assert.EqualValues(t, []string{"baz", "qux"}, val.Foo["bar"])
  464. }
  465. func TestUnmarshalJsonBytesMapWithSliceOfStructs(t *testing.T) {
  466. input := []byte(`{"foo":{
  467. "/mtproto.RPCTos": [{"bar": "any"}],
  468. "bar":[{"bar": "qux"}, {"bar": "ever"}]}}`)
  469. var val struct {
  470. Foo map[string][]struct {
  471. Bar string
  472. }
  473. }
  474. assert.NoError(t, LoadFromJsonBytes(input, &val))
  475. assert.Equal(t, 1, len(val.Foo["/mtproto.RPCTos"]))
  476. assert.Equal(t, "any", val.Foo["/mtproto.RPCTos"][0].Bar)
  477. assert.Equal(t, 2, len(val.Foo["bar"]))
  478. assert.Equal(t, "qux", val.Foo["bar"][0].Bar)
  479. assert.Equal(t, "ever", val.Foo["bar"][1].Bar)
  480. }
  481. func TestUnmarshalJsonBytesWithAnonymousField(t *testing.T) {
  482. type (
  483. Int int
  484. InnerConf struct {
  485. Name string
  486. }
  487. Conf struct {
  488. Int
  489. InnerConf
  490. }
  491. )
  492. var (
  493. input = []byte(`{"Name": "hello", "int": 3}`)
  494. c Conf
  495. )
  496. assert.NoError(t, LoadFromJsonBytes(input, &c))
  497. assert.Equal(t, "hello", c.Name)
  498. assert.Equal(t, Int(3), c.Int)
  499. }
  500. func TestUnmarshalJsonBytesWithMapValueOfStruct(t *testing.T) {
  501. type (
  502. Value struct {
  503. Name string
  504. }
  505. Config struct {
  506. Items map[string]Value
  507. }
  508. )
  509. var inputs = [][]byte{
  510. []byte(`{"Items": {"Key":{"Name": "foo"}}}`),
  511. []byte(`{"Items": {"Key":{"Name": "foo"}}}`),
  512. []byte(`{"items": {"key":{"name": "foo"}}}`),
  513. []byte(`{"items": {"key":{"name": "foo"}}}`),
  514. }
  515. for _, input := range inputs {
  516. var c Config
  517. if assert.NoError(t, LoadFromJsonBytes(input, &c)) {
  518. assert.Equal(t, 1, len(c.Items))
  519. for _, v := range c.Items {
  520. assert.Equal(t, "foo", v.Name)
  521. }
  522. }
  523. }
  524. }
  525. func TestUnmarshalJsonBytesWithMapTypeValueOfStruct(t *testing.T) {
  526. type (
  527. Value struct {
  528. Name string
  529. }
  530. Map map[string]Value
  531. Config struct {
  532. Map
  533. }
  534. )
  535. var inputs = [][]byte{
  536. []byte(`{"Map": {"Key":{"Name": "foo"}}}`),
  537. []byte(`{"Map": {"Key":{"Name": "foo"}}}`),
  538. []byte(`{"map": {"key":{"name": "foo"}}}`),
  539. []byte(`{"map": {"key":{"name": "foo"}}}`),
  540. }
  541. for _, input := range inputs {
  542. var c Config
  543. if assert.NoError(t, LoadFromJsonBytes(input, &c)) {
  544. assert.Equal(t, 1, len(c.Map))
  545. for _, v := range c.Map {
  546. assert.Equal(t, "foo", v.Name)
  547. }
  548. }
  549. }
  550. }
  551. func Test_FieldOverwrite(t *testing.T) {
  552. t.Run("normal", func(t *testing.T) {
  553. type Base struct {
  554. Name string
  555. }
  556. type St1 struct {
  557. Base
  558. Name2 string
  559. }
  560. type St2 struct {
  561. Base
  562. Name2 string
  563. }
  564. type St3 struct {
  565. *Base
  566. Name2 string
  567. }
  568. type St4 struct {
  569. *Base
  570. Name2 *string
  571. }
  572. validate := func(val any) {
  573. input := []byte(`{"Name": "hello", "Name2": "world"}`)
  574. assert.NoError(t, LoadFromJsonBytes(input, val))
  575. }
  576. validate(&St1{})
  577. validate(&St2{})
  578. validate(&St3{})
  579. validate(&St4{})
  580. })
  581. t.Run("Inherit Override", func(t *testing.T) {
  582. type Base struct {
  583. Name string
  584. }
  585. type St1 struct {
  586. Base
  587. Name string
  588. }
  589. type St2 struct {
  590. Base
  591. Name int
  592. }
  593. type St3 struct {
  594. *Base
  595. Name int
  596. }
  597. type St4 struct {
  598. *Base
  599. Name *string
  600. }
  601. validate := func(val any) {
  602. input := []byte(`{"Name": "hello"}`)
  603. err := LoadFromJsonBytes(input, val)
  604. assert.ErrorAs(t, err, &dupErr)
  605. assert.Equal(t, newConflictKeyError("name").Error(), err.Error())
  606. }
  607. validate(&St1{})
  608. validate(&St2{})
  609. validate(&St3{})
  610. validate(&St4{})
  611. })
  612. t.Run("Inherit more", func(t *testing.T) {
  613. type Base1 struct {
  614. Name string
  615. }
  616. type St0 struct {
  617. Base1
  618. Name string
  619. }
  620. type St1 struct {
  621. St0
  622. Name string
  623. }
  624. type St2 struct {
  625. St0
  626. Name int
  627. }
  628. type St3 struct {
  629. *St0
  630. Name int
  631. }
  632. type St4 struct {
  633. *St0
  634. Name *int
  635. }
  636. validate := func(val any) {
  637. input := []byte(`{"Name": "hello"}`)
  638. err := LoadFromJsonBytes(input, val)
  639. assert.ErrorAs(t, err, &dupErr)
  640. assert.Equal(t, newConflictKeyError("name").Error(), err.Error())
  641. }
  642. validate(&St0{})
  643. validate(&St1{})
  644. validate(&St2{})
  645. validate(&St3{})
  646. validate(&St4{})
  647. })
  648. }
  649. func TestFieldOverwriteComplicated(t *testing.T) {
  650. t.Run("double maps", func(t *testing.T) {
  651. type (
  652. Base1 struct {
  653. Values map[string]string
  654. }
  655. Base2 struct {
  656. Values map[string]string
  657. }
  658. Config struct {
  659. Base1
  660. Base2
  661. }
  662. )
  663. var c Config
  664. input := []byte(`{"Values": {"Key": "Value"}}`)
  665. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  666. })
  667. t.Run("merge children", func(t *testing.T) {
  668. type (
  669. Inner1 struct {
  670. Name string
  671. }
  672. Inner2 struct {
  673. Age int
  674. }
  675. Base1 struct {
  676. Inner Inner1
  677. }
  678. Base2 struct {
  679. Inner Inner2
  680. }
  681. Config struct {
  682. Base1
  683. Base2
  684. }
  685. )
  686. var c Config
  687. input := []byte(`{"Inner": {"Name": "foo", "Age": 10}}`)
  688. if assert.NoError(t, LoadFromJsonBytes(input, &c)) {
  689. assert.Equal(t, "foo", c.Base1.Inner.Name)
  690. assert.Equal(t, 10, c.Base2.Inner.Age)
  691. }
  692. })
  693. t.Run("overwritten maps", func(t *testing.T) {
  694. type (
  695. Inner struct {
  696. Map map[string]string
  697. }
  698. Config struct {
  699. Map map[string]string
  700. Inner
  701. }
  702. )
  703. var c Config
  704. input := []byte(`{"Inner": {"Map": {"Key": "Value"}}}`)
  705. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  706. })
  707. t.Run("overwritten nested maps", func(t *testing.T) {
  708. type (
  709. Inner struct {
  710. Map map[string]string
  711. }
  712. Middle1 struct {
  713. Map map[string]string
  714. Inner
  715. }
  716. Middle2 struct {
  717. Map map[string]string
  718. Inner
  719. }
  720. Config struct {
  721. Middle1
  722. Middle2
  723. }
  724. )
  725. var c Config
  726. input := []byte(`{"Middle1": {"Inner": {"Map": {"Key": "Value"}}}}`)
  727. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  728. })
  729. t.Run("overwritten outer/inner maps", func(t *testing.T) {
  730. type (
  731. Inner struct {
  732. Map map[string]string
  733. }
  734. Middle struct {
  735. Inner
  736. Map map[string]string
  737. }
  738. Config struct {
  739. Middle
  740. }
  741. )
  742. var c Config
  743. input := []byte(`{"Middle": {"Inner": {"Map": {"Key": "Value"}}}}`)
  744. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  745. })
  746. t.Run("overwritten anonymous maps", func(t *testing.T) {
  747. type (
  748. Inner struct {
  749. Map map[string]string
  750. }
  751. Middle struct {
  752. Inner
  753. Map map[string]string
  754. }
  755. Elem map[string]Middle
  756. Config struct {
  757. Elem
  758. }
  759. )
  760. var c Config
  761. input := []byte(`{"Elem": {"Key": {"Inner": {"Map": {"Key": "Value"}}}}}`)
  762. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  763. })
  764. t.Run("overwritten primitive and map", func(t *testing.T) {
  765. type (
  766. Inner struct {
  767. Value string
  768. }
  769. Elem map[string]Inner
  770. Named struct {
  771. Elem string
  772. }
  773. Config struct {
  774. Named
  775. Elem
  776. }
  777. )
  778. var c Config
  779. input := []byte(`{"Elem": {"Key": {"Value": "Value"}}}`)
  780. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  781. })
  782. t.Run("overwritten map and slice", func(t *testing.T) {
  783. type (
  784. Inner struct {
  785. Value string
  786. }
  787. Elem []Inner
  788. Named struct {
  789. Elem string
  790. }
  791. Config struct {
  792. Named
  793. Elem
  794. }
  795. )
  796. var c Config
  797. input := []byte(`{"Elem": {"Key": {"Value": "Value"}}}`)
  798. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  799. })
  800. t.Run("overwritten map and string", func(t *testing.T) {
  801. type (
  802. Elem string
  803. Named struct {
  804. Elem string
  805. }
  806. Config struct {
  807. Named
  808. Elem
  809. }
  810. )
  811. var c Config
  812. input := []byte(`{"Elem": {"Key": {"Value": "Value"}}}`)
  813. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  814. })
  815. }
  816. func TestLoadNamedFieldOverwritten(t *testing.T) {
  817. t.Run("overwritten named struct", func(t *testing.T) {
  818. type (
  819. Elem string
  820. Named struct {
  821. Elem string
  822. }
  823. Base struct {
  824. Named
  825. Elem
  826. }
  827. Config struct {
  828. Val Base
  829. }
  830. )
  831. var c Config
  832. input := []byte(`{"Val": {"Elem": {"Key": {"Value": "Value"}}}}`)
  833. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  834. })
  835. t.Run("overwritten named []struct", func(t *testing.T) {
  836. type (
  837. Elem string
  838. Named struct {
  839. Elem string
  840. }
  841. Base struct {
  842. Named
  843. Elem
  844. }
  845. Config struct {
  846. Vals []Base
  847. }
  848. )
  849. var c Config
  850. input := []byte(`{"Vals": [{"Elem": {"Key": {"Value": "Value"}}}]}`)
  851. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  852. })
  853. t.Run("overwritten named map[string]struct", func(t *testing.T) {
  854. type (
  855. Elem string
  856. Named struct {
  857. Elem string
  858. }
  859. Base struct {
  860. Named
  861. Elem
  862. }
  863. Config struct {
  864. Vals map[string]Base
  865. }
  866. )
  867. var c Config
  868. input := []byte(`{"Vals": {"Key": {"Elem": {"Key": {"Value": "Value"}}}}}`)
  869. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  870. })
  871. t.Run("overwritten named *struct", func(t *testing.T) {
  872. type (
  873. Elem string
  874. Named struct {
  875. Elem string
  876. }
  877. Base struct {
  878. Named
  879. Elem
  880. }
  881. Config struct {
  882. Vals *Base
  883. }
  884. )
  885. var c Config
  886. input := []byte(`{"Vals": [{"Elem": {"Key": {"Value": "Value"}}}]}`)
  887. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  888. })
  889. t.Run("overwritten named struct", func(t *testing.T) {
  890. type (
  891. Named struct {
  892. Elem string
  893. }
  894. Base struct {
  895. Named
  896. Elem Named
  897. }
  898. Config struct {
  899. Val Base
  900. }
  901. )
  902. var c Config
  903. input := []byte(`{"Val": {"Elem": "Value"}}`)
  904. assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
  905. })
  906. t.Run("overwritten named struct", func(t *testing.T) {
  907. type Config struct {
  908. Val chan int
  909. }
  910. var c Config
  911. input := []byte(`{"Val": 1}`)
  912. assert.Error(t, LoadFromJsonBytes(input, &c))
  913. })
  914. }
  915. func TestLoadLowerMemberShouldNotConflict(t *testing.T) {
  916. type (
  917. Redis struct {
  918. db uint
  919. }
  920. Config struct {
  921. db uint
  922. Redis
  923. }
  924. )
  925. var c Config
  926. assert.NoError(t, LoadFromJsonBytes([]byte(`{}`), &c))
  927. assert.Zero(t, c.db)
  928. assert.Zero(t, c.Redis.db)
  929. }
  930. func TestFillDefaultUnmarshal(t *testing.T) {
  931. t.Run("nil", func(t *testing.T) {
  932. type St struct{}
  933. err := FillDefault(St{})
  934. assert.Error(t, err)
  935. })
  936. t.Run("not nil", func(t *testing.T) {
  937. type St struct{}
  938. err := FillDefault(&St{})
  939. assert.NoError(t, err)
  940. })
  941. t.Run("default", func(t *testing.T) {
  942. type St struct {
  943. A string `json:",default=a"`
  944. B string
  945. }
  946. var st St
  947. err := FillDefault(&st)
  948. assert.NoError(t, err)
  949. assert.Equal(t, st.A, "a")
  950. })
  951. t.Run("env", func(t *testing.T) {
  952. type St struct {
  953. A string `json:",default=a"`
  954. B string
  955. C string `json:",env=TEST_C"`
  956. }
  957. t.Setenv("TEST_C", "c")
  958. var st St
  959. err := FillDefault(&st)
  960. assert.NoError(t, err)
  961. assert.Equal(t, st.A, "a")
  962. assert.Equal(t, st.C, "c")
  963. })
  964. t.Run("has value", func(t *testing.T) {
  965. type St struct {
  966. A string `json:",default=a"`
  967. B string
  968. }
  969. var st = St{
  970. A: "b",
  971. }
  972. err := FillDefault(&st)
  973. assert.Error(t, err)
  974. })
  975. }
  976. func TestConfigWithJsonTag(t *testing.T) {
  977. t.Run("map with value", func(t *testing.T) {
  978. var input = []byte(`[Value]
  979. [Value.first]
  980. Email = "foo"
  981. [Value.second]
  982. Email = "bar"`)
  983. type Value struct {
  984. Email string
  985. }
  986. type Config struct {
  987. ValueMap map[string]Value `json:"Value"`
  988. }
  989. var c Config
  990. if assert.NoError(t, LoadFromTomlBytes(input, &c)) {
  991. assert.Len(t, c.ValueMap, 2)
  992. }
  993. })
  994. t.Run("map with ptr value", func(t *testing.T) {
  995. var input = []byte(`[Value]
  996. [Value.first]
  997. Email = "foo"
  998. [Value.second]
  999. Email = "bar"`)
  1000. type Value struct {
  1001. Email string
  1002. }
  1003. type Config struct {
  1004. ValueMap map[string]*Value `json:"Value"`
  1005. }
  1006. var c Config
  1007. if assert.NoError(t, LoadFromTomlBytes(input, &c)) {
  1008. assert.Len(t, c.ValueMap, 2)
  1009. }
  1010. })
  1011. t.Run("map with optional", func(t *testing.T) {
  1012. var input = []byte(`[Value]
  1013. [Value.first]
  1014. Email = "foo"
  1015. [Value.second]
  1016. Email = "bar"`)
  1017. type Value struct {
  1018. Email string
  1019. }
  1020. type Config struct {
  1021. Value map[string]Value `json:",optional"`
  1022. }
  1023. var c Config
  1024. if assert.NoError(t, LoadFromTomlBytes(input, &c)) {
  1025. assert.Len(t, c.Value, 2)
  1026. }
  1027. })
  1028. t.Run("map with empty tag", func(t *testing.T) {
  1029. var input = []byte(`[Value]
  1030. [Value.first]
  1031. Email = "foo"
  1032. [Value.second]
  1033. Email = "bar"`)
  1034. type Value struct {
  1035. Email string
  1036. }
  1037. type Config struct {
  1038. Value map[string]Value `json:" "`
  1039. }
  1040. var c Config
  1041. if assert.NoError(t, LoadFromTomlBytes(input, &c)) {
  1042. assert.Len(t, c.Value, 2)
  1043. }
  1044. })
  1045. }
  1046. func createTempFile(ext, text string) (string, error) {
  1047. tmpFile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext)
  1048. if err != nil {
  1049. return "", err
  1050. }
  1051. if err := os.WriteFile(tmpFile.Name(), []byte(text), os.ModeTemporary); err != nil {
  1052. return "", err
  1053. }
  1054. filename := tmpFile.Name()
  1055. if err = tmpFile.Close(); err != nil {
  1056. return "", err
  1057. }
  1058. return filename, nil
  1059. }