Skip to content

Commit 53fe8c9

Browse files
authored
enhancement: improve the equality check (#7)
1 parent a333b36 commit 53fe8c9

9 files changed

Lines changed: 723 additions & 729 deletions

any.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ func (ev EnvAny) GetCustom(getFunc GetEnvFunc) (any, error) {
9696
func (ev EnvAny) Equal(target EnvAny) bool {
9797
isSameValue := (ev.Value == nil && target.Value == nil) ||
9898
(ev.Value != nil && target.Value != nil && reflect.DeepEqual(ev.Value, target.Value))
99+
if !isSameValue {
100+
return false
101+
}
99102

100-
isSameEnv := (ev.Variable == nil && target.Variable == nil) ||
103+
return (ev.Variable == nil && target.Variable == nil) ||
101104
(ev.Variable != nil && target.Variable != nil && *ev.Variable == *target.Variable)
102-
103-
return isSameValue && isSameEnv
104105
}

any_equal_test.go

Lines changed: 0 additions & 150 deletions
This file was deleted.

any_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,148 @@ func TestEnvAny_GetCustom(t *testing.T) {
165165
})
166166
}
167167
}
168+
169+
func TestEnvAny_Equal(t *testing.T) {
170+
testCases := []struct {
171+
Name string
172+
Input EnvAny
173+
Target EnvAny
174+
Expected bool
175+
}{
176+
{
177+
Name: "both_nil",
178+
Input: EnvAny{},
179+
Target: EnvAny{},
180+
Expected: true,
181+
},
182+
{
183+
Name: "same_string_values",
184+
Input: NewEnvAnyValue("hello"),
185+
Target: NewEnvAnyValue("hello"),
186+
Expected: true,
187+
},
188+
{
189+
Name: "different_string_values",
190+
Input: NewEnvAnyValue("hello"),
191+
Target: NewEnvAnyValue("world"),
192+
Expected: false,
193+
},
194+
{
195+
Name: "same_int_values",
196+
Input: NewEnvAnyValue(42),
197+
Target: NewEnvAnyValue(42),
198+
Expected: true,
199+
},
200+
{
201+
Name: "different_int_values",
202+
Input: NewEnvAnyValue(42),
203+
Target: NewEnvAnyValue(100),
204+
Expected: false,
205+
},
206+
{
207+
Name: "same_float_values",
208+
Input: NewEnvAnyValue(3.14),
209+
Target: NewEnvAnyValue(3.14),
210+
Expected: true,
211+
},
212+
{
213+
Name: "different_float_values",
214+
Input: NewEnvAnyValue(3.14),
215+
Target: NewEnvAnyValue(2.718),
216+
Expected: false,
217+
},
218+
{
219+
Name: "same_bool_values",
220+
Input: NewEnvAnyValue(true),
221+
Target: NewEnvAnyValue(true),
222+
Expected: true,
223+
},
224+
{
225+
Name: "different_bool_values",
226+
Input: NewEnvAnyValue(true),
227+
Target: NewEnvAnyValue(false),
228+
Expected: false,
229+
},
230+
{
231+
Name: "same_map_values",
232+
Input: NewEnvAnyValue(map[string]any{"key": "value"}),
233+
Target: NewEnvAnyValue(map[string]any{"key": "value"}),
234+
Expected: true,
235+
},
236+
{
237+
Name: "different_map_values",
238+
Input: NewEnvAnyValue(map[string]any{"key1": "value1"}),
239+
Target: NewEnvAnyValue(map[string]any{"key2": "value2"}),
240+
Expected: false,
241+
},
242+
{
243+
Name: "same_slice_values",
244+
Input: NewEnvAnyValue([]any{1, 2, 3}),
245+
Target: NewEnvAnyValue([]any{1, 2, 3}),
246+
Expected: true,
247+
},
248+
{
249+
Name: "different_slice_values",
250+
Input: NewEnvAnyValue([]any{1, 2, 3}),
251+
Target: NewEnvAnyValue([]any{4, 5, 6}),
252+
Expected: false,
253+
},
254+
{
255+
Name: "same_variable_names",
256+
Input: NewEnvAnyVariable("MY_VAR"),
257+
Target: NewEnvAnyVariable("MY_VAR"),
258+
Expected: true,
259+
},
260+
{
261+
Name: "different_variable_names",
262+
Input: NewEnvAnyVariable("VAR1"),
263+
Target: NewEnvAnyVariable("VAR2"),
264+
Expected: false,
265+
},
266+
{
267+
Name: "same_value_and_variable",
268+
Input: NewEnvAny("MY_VAR", "default"),
269+
Target: NewEnvAny("MY_VAR", "default"),
270+
Expected: true,
271+
},
272+
{
273+
Name: "same_variable_different_value",
274+
Input: NewEnvAny("MY_VAR", "value1"),
275+
Target: NewEnvAny("MY_VAR", "value2"),
276+
Expected: false,
277+
},
278+
{
279+
Name: "different_variable_same_value",
280+
Input: NewEnvAny("VAR1", "value"),
281+
Target: NewEnvAny("VAR2", "value"),
282+
Expected: false,
283+
},
284+
{
285+
Name: "value_vs_variable",
286+
Input: NewEnvAnyValue("hello"),
287+
Target: NewEnvAnyVariable("MY_VAR"),
288+
Expected: false,
289+
},
290+
{
291+
Name: "different_types",
292+
Input: NewEnvAnyValue("42"),
293+
Target: NewEnvAnyValue(42),
294+
Expected: false,
295+
},
296+
{
297+
Name: "nil_value_vs_non_nil",
298+
Input: NewEnvAnyVariable("MY_VAR"),
299+
Target: NewEnvAnyValue("hello"),
300+
Expected: false,
301+
},
302+
}
303+
304+
for _, tc := range testCases {
305+
t.Run(tc.Name, func(t *testing.T) {
306+
result := tc.Input.Equal(tc.Target)
307+
if result != tc.Expected {
308+
t.Errorf("Expected %v, got %v", tc.Expected, result)
309+
}
310+
})
311+
}
312+
}

environment.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ func (ev EnvString) IsZero() bool {
6666
ev.Value == nil
6767
}
6868

69+
// Equal checks if this instance equals the target value.
70+
func (ev EnvString) Equal(target EnvString) bool {
71+
isSameValue := (ev.Value == nil && target.Value == nil) ||
72+
(ev.Value != nil && target.Value != nil && *ev.Value == *target.Value)
73+
if !isSameValue {
74+
return false
75+
}
76+
77+
return (ev.Variable == nil && target.Variable == nil) ||
78+
(ev.Variable != nil && target.Variable != nil && *ev.Variable == *target.Variable)
79+
}
80+
6981
// Get gets literal value or from system environment.
7082
func (ev EnvString) Get() (string, error) {
7183
if ev.IsZero() {
@@ -127,17 +139,6 @@ func (ev EnvString) GetCustom(getFunc GetEnvFunc) (string, error) {
127139
return "", getEnvVariableValueRequiredError(ev.Variable)
128140
}
129141

130-
// Equal checks if this instance equals the target value.
131-
func (ev EnvString) Equal(target EnvString) bool {
132-
isSameValue := (ev.Value == nil && target.Value == nil) ||
133-
(ev.Value != nil && target.Value != nil && *ev.Value == *target.Value)
134-
135-
isSameEnv := (ev.Variable == nil && target.Variable == nil) ||
136-
(ev.Variable != nil && target.Variable != nil && *ev.Variable == *target.Variable)
137-
138-
return isSameValue && isSameEnv
139-
}
140-
141142
// EnvInt represents either a literal integer or an environment reference.
142143
type EnvInt struct {
143144
Value *int64 `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
@@ -176,11 +177,12 @@ func (ev EnvInt) IsZero() bool {
176177
func (ev EnvInt) Equal(target EnvInt) bool {
177178
isSameValue := (ev.Value == nil && target.Value == nil) ||
178179
(ev.Value != nil && target.Value != nil && *ev.Value == *target.Value)
180+
if !isSameValue {
181+
return false
182+
}
179183

180-
isSameEnv := (ev.Variable == nil && target.Variable == nil) ||
184+
return (ev.Variable == nil && target.Variable == nil) ||
181185
(ev.Variable != nil && target.Variable != nil && *ev.Variable == *target.Variable)
182-
183-
return isSameValue && isSameEnv
184186
}
185187

186188
// UnmarshalJSON implements json.Unmarshaler.
@@ -300,11 +302,12 @@ func (ev EnvBool) IsZero() bool {
300302
func (ev EnvBool) Equal(target EnvBool) bool {
301303
isSameValue := (ev.Value == nil && target.Value == nil) ||
302304
(ev.Value != nil && target.Value != nil && *ev.Value == *target.Value)
305+
if !isSameValue {
306+
return false
307+
}
303308

304-
isSameEnv := (ev.Variable == nil && target.Variable == nil) ||
309+
return (ev.Variable == nil && target.Variable == nil) ||
305310
(ev.Variable != nil && target.Variable != nil && *ev.Variable == *target.Variable)
306-
307-
return isSameValue && isSameEnv
308311
}
309312

310313
// UnmarshalJSON implements json.Unmarshaler.
@@ -424,11 +427,12 @@ func (ev EnvFloat) IsZero() bool {
424427
func (ev EnvFloat) Equal(target EnvFloat) bool {
425428
isSameValue := (ev.Value == nil && target.Value == nil) ||
426429
(ev.Value != nil && target.Value != nil && *ev.Value == *target.Value)
430+
if !isSameValue {
431+
return false
432+
}
427433

428-
isSameEnv := (ev.Variable == nil && target.Variable == nil) ||
434+
return (ev.Variable == nil && target.Variable == nil) ||
429435
(ev.Variable != nil && target.Variable != nil && *ev.Variable == *target.Variable)
430-
431-
return isSameValue && isSameEnv
432436
}
433437

434438
// UnmarshalJSON implements json.Unmarshaler.

0 commit comments

Comments
 (0)