Debian Codegraph @

providers/structs/structs_test.go

raw
1 package structs
2
3 import (
4 "reflect"
5 "testing"
6 )
7
8 type parentStruct struct {
9 Name string `koanf:"name"`
10 ID int `koanf:"id"`
11 Child1 childStruct `koanf:"child1"`
12 }
13 type childStruct struct {
14 Name string `koanf:"name"`
15 Type string `koanf:"type"`
16 Empty map[string]string `koanf:"empty"`
17 Grandchild1 grandchildStruct `koanf:"grandchild1"`
18 }
19 type grandchildStruct struct {
20 Ids []int `koanf:"ids"`
21 On bool `koanf:"on"`
22 }
23 type testStruct struct {
24 Type string `koanf:"type"`
25 Empty map[string]string `koanf:"empty"`
26 Parent1 parentStruct `koanf:"parent1"`
27 }
28
29 type testStructWithDelim struct {
30 Endpoint string `koanf:"conf_endpoint"`
31 Username string `koanf:"conf_creds.username"`
32 Password string `koanf:"conf_creds.password"`
33 }
34
35 func TestStructs_Read(t *testing.T) {
36 type fields struct {
37 s any
38 tag string
39 delim string
40 }
41 tests := []struct {
42 name string
43 fields fields
44 want map[string]any
45 wantErr bool
46 }{
47 {
48 name: "read",
49 fields: fields{
50 s: testStruct{
51 Type: "json",
52 Empty: make(map[string]string),
53 Parent1: parentStruct{
54 Name: "parent1",
55 ID: 1234,
56 Child1: childStruct{
57 Name: "child1",
58 Type: "json",
59 Empty: make(map[string]string),
60 Grandchild1: grandchildStruct{
61 Ids: []int{1, 2, 3},
62 On: true,
63 },
64 },
65 },
66 },
67 tag: "koanf",
68 },
69 want: map[string]any{
70 "type": "json",
71 "empty": map[string]string{},
72 "parent1": map[string]any{
73 "child1": map[string]any{
74 "empty": map[string]string{},
75 "type": "json",
76 "name": "child1",
77 "grandchild1": map[string]any{
78 "on": true,
79 "ids": []int{1, 2, 3},
80 },
81 },
82 "name": "parent1",
83 "id": 1234,
84 },
85 },
86 wantErr: false,
87 },
88 {
89 name: "read delim struct",
90 fields: fields{
91 s: testStructWithDelim{
92 Endpoint: "test_endpoint",
93 Username: "test_username",
94 Password: "test_password",
95 },
96 tag: "koanf",
97 delim: ".",
98 },
99 want: map[string]any{
100 "conf_creds": map[string]any{
101 "password": "test_password",
102 "username": "test_username",
103 },
104 "conf_endpoint": "test_endpoint",
105 },
106 wantErr: false,
107 },
108 }
109 for _, tt := range tests {
110 t.Run(tt.name, func(t *testing.T) {
111 s := &Structs{
112 s: tt.fields.s,
113 tag: tt.fields.tag,
114 delim: tt.fields.delim,
115 }
116 got, err := s.Read()
117 if (err != nil) != tt.wantErr {
118 t.Errorf("Structs.Read() error = %v, wantErr %v", err, tt.wantErr)
119 return
120 }
121 if !reflect.DeepEqual(got, tt.want) {
122 t.Errorf("Structs.Read() = \n%#v\nwant \n%#v\n", got, tt.want)
123 }
124 })
125 }
126 }