@@ -8,6 +8,146 @@ import (
88 "github.com/stretchr/testify/require"
99)
1010
11+ func BenchmarkParseFileToStruct (b * testing.B ) {
12+ type config struct {
13+ Host string `koanf:"host"`
14+ Port string `koanf:"port"`
15+ }
16+
17+ _ = os .MkdirAll ("./test" , 0o755 )
18+
19+ _ = os .WriteFile ("./test/file.json" , []byte (`{
20+ "host": "https://my-server.com/api/v1",
21+ "port": "3000"
22+ }` ), 0o755 )
23+
24+ for i := 0 ; i < b .N ; i ++ {
25+ _ , _ = ParseFileToStruct ("./test/file.json" , & config {})
26+ }
27+
28+ _ = os .RemoveAll ("./test" )
29+ }
30+
31+ func BenchmarkParseFileWithEnvToStruct (b * testing.B ) {
32+ type config struct {
33+ URL string `koanf:"url"`
34+ AuthType string `koanf:"auth_type"`
35+ Token string `koanf:"token"`
36+ }
37+
38+ _ = os .MkdirAll ("./test" , 0o755 )
39+
40+ _ = os .WriteFile ("./test/file.json" , []byte (`{
41+ "url": "https://my-server.com/api/v1",
42+ "auth_type": "Bearer",
43+ "token": "{{ MY_CONFIG_TOKEN }}"
44+ }` ), 0o755 )
45+
46+ for i := 0 ; i < b .N ; i ++ {
47+ _ , _ = ParseFileWithEnvToStruct ("./test/file.json" , "MY_CONFIG" , & config {})
48+ }
49+
50+ _ = os .RemoveAll ("./test" )
51+ }
52+
53+ func TestParseFileToStruct (t * testing.T ) {
54+ type config struct {
55+ Host string `koanf:"host"`
56+ Port string `koanf:"port"`
57+ }
58+
59+ _ , err := ParseFileToStruct ("" , & config {})
60+ require .Error (t , err )
61+
62+ _ = os .MkdirAll ("./test" , 0o755 )
63+
64+ _ , err = ParseFileToStruct ("./test/file.unknown" , & config {})
65+ require .Error (t , err )
66+
67+ _ , err = ParseFileToStruct ("https://example.com/file.json" , & config {})
68+ require .Error (t , err )
69+
70+ _ , err = ParseFileToStruct ("example.com/file.json" , & config {})
71+ require .Error (t , err )
72+
73+ _ , err = ParseFileToStruct ("https://github.com/koddr/gosl/blob/main/.github/dependabot.yml" , & config {})
74+ require .Error (t , err )
75+
76+ _ , err = ParseFileToStruct ("./test/not-found-file.json" , & config {})
77+ require .Error (t , err )
78+
79+ _ , err = ParseFileToStruct ("./test" , & config {})
80+ require .Error (t , err )
81+
82+ _ = os .WriteFile ("./test/file.json" , []byte (`{
83+ "host": "https://my-server.com/api/v1",
84+ "port": "3000"
85+ }` ), 0o755 )
86+
87+ cfgJson , err := ParseFileToStruct ("./test/file.json" , & config {})
88+ assert .NoError (t , err )
89+ assert .EqualValues (t , cfgJson .Host , "https://my-server.com/api/v1" )
90+
91+ _ = os .WriteFile ("./test/file.yml" , []byte (`host: https://my-server.com/api/v1
92+ port: '3000'` ), 0o755 )
93+
94+ cfgYaml , err := ParseFileToStruct ("./test/file.yml" , & config {})
95+ assert .NoError (t , err )
96+ assert .EqualValues (t , cfgYaml .Host , "https://my-server.com/api/v1" )
97+
98+ _ = os .WriteFile ("./test/file.toml" , []byte (`host = "https://my-server.com/api/v1"
99+ port = "3000"` ), 0o755 )
100+
101+ cfgToml , err := ParseFileToStruct ("./test/file.toml" , & config {})
102+ assert .NoError (t , err )
103+ assert .EqualValues (t , cfgToml .Host , "https://my-server.com/api/v1" )
104+
105+ _ = os .WriteFile ("./test/file.tf" , []byte (`"host" = "https://my-server.com/api/v1"
106+ "port" = "3000"` ), 0o755 )
107+
108+ cfgHcl , err := ParseFileToStruct ("./test/file.tf" , & config {})
109+ assert .NoError (t , err )
110+ assert .EqualValues (t , cfgHcl .Host , "https://my-server.com/api/v1" )
111+
112+ g := GenericUtility [config , any ]{} // tests for method
113+
114+ _ , err = g .ParseFileToStruct ("./test/file.unknown" , & config {})
115+ require .Error (t , err )
116+
117+ _ , err = g .ParseFileToStruct ("https://example.com/file.json" , & config {})
118+ require .Error (t , err )
119+
120+ _ , err = g .ParseFileToStruct ("example.com/file.json" , & config {})
121+ require .Error (t , err )
122+
123+ _ , err = g .ParseFileToStruct ("https://github.com/koddr/gosl/blob/main/.github/dependabot.yml" , & config {})
124+ require .Error (t , err )
125+
126+ _ , err = g .ParseFileToStruct ("./test/not-found-file.json" , & config {})
127+ require .Error (t , err )
128+
129+ _ , err = g .ParseFileToStruct ("./test" , & config {})
130+ require .Error (t , err )
131+
132+ cfgJson , err = g .ParseFileToStruct ("./test/file.json" , & config {})
133+ assert .NoError (t , err )
134+ assert .EqualValues (t , cfgJson .Host , "https://my-server.com/api/v1" )
135+
136+ cfgYaml , err = g .ParseFileToStruct ("./test/file.yml" , & config {})
137+ assert .NoError (t , err )
138+ assert .EqualValues (t , cfgYaml .Host , "https://my-server.com/api/v1" )
139+
140+ cfgToml , err = g .ParseFileToStruct ("./test/file.toml" , & config {})
141+ assert .NoError (t , err )
142+ assert .EqualValues (t , cfgToml .Host , "https://my-server.com/api/v1" )
143+
144+ cfgHcl , err = g .ParseFileToStruct ("./test/file.tf" , & config {})
145+ assert .NoError (t , err )
146+ assert .EqualValues (t , cfgHcl .Host , "https://my-server.com/api/v1" )
147+
148+ _ = os .RemoveAll ("./test" )
149+ }
150+
11151func TestParseFileWithEnvToStruct (t * testing.T ) {
12152 type config struct {
13153 URL string `koanf:"url"`
@@ -72,6 +212,9 @@ token = "{{ MY_CONFIG_TOKEN }}"`), 0o755)
72212 assert .NoError (t , err )
73213 assert .EqualValues (t , cfgHcl .URL , "https://my-server.com/api/v1" )
74214
215+ _ , err = ParseFileWithEnvToStruct ("./test/file.tf" , "" , & config {})
216+ require .Error (t , err )
217+
75218 g := GenericUtility [config , any ]{} // tests for method
76219
77220 _ , err = g .ParseFileWithEnvToStruct ("./test/file.unknown" , "" , & config {})
@@ -108,5 +251,8 @@ token = "{{ MY_CONFIG_TOKEN }}"`), 0o755)
108251 assert .NoError (t , err )
109252 assert .EqualValues (t , cfgHcl .URL , "https://my-server.com/api/v1" )
110253
254+ _ , err = g .ParseFileWithEnvToStruct ("./test/file.tf" , "" , & config {})
255+ require .Error (t , err )
256+
111257 _ = os .RemoveAll ("./test" )
112258}
0 commit comments