Skip to content

Commit 4c30469

Browse files
authored
feat: add IsZero method (#3)
1 parent f0b1103 commit 4c30469

5 files changed

Lines changed: 86 additions & 18 deletions

File tree

any.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ func (ev *EnvAny) UnmarshalJSON(b []byte) error {
5555
return nil
5656
}
5757

58+
// IsZero checks if the instance is empty.
59+
func (ev EnvAny) IsZero() bool {
60+
return (ev.Variable == nil || *ev.Variable == "") &&
61+
ev.Value == nil
62+
}
63+
5864
// Get gets literal value or from system environment.
5965
func (ev EnvAny) Get() (any, error) {
6066
if ev.Variable != nil {

any_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ func TestEnvAny(t *testing.T) {
4545
for i, tc := range testCases {
4646
t.Run(fmt.Sprint(i), func(t *testing.T) {
4747
result, err := tc.Input.Get()
48+
4849
if tc.ErrorMsg != "" {
4950
assertErrorContains(t, err, tc.ErrorMsg)
5051
} else {
5152
assertNilError(t, err)
5253
assertDeepEqual(t, result, tc.Expected)
5354
}
55+
56+
assertDeepEqual(t, tc.Input.IsZero(), tc.Expected == nil)
5457
})
5558
}
5659

environment.go

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ import (
99
"strconv"
1010
)
1111

12-
var (
13-
errEnvironmentValueRequired = errors.New("require either value or env")
14-
// ErrEnvironmentVariableRequired the error happens when the name of environment variable is empty.
15-
ErrEnvironmentVariableRequired = errors.New("the environment variable name is empty")
16-
// ErrEnvironmentVariableValueRequired the error happens when the value from environment variable is empty.
17-
ErrEnvironmentVariableValueRequired = errors.New("the environment variable value is empty")
18-
)
19-
2012
// EnvString represents either a literal string or an environment reference.
2113
type EnvString struct {
2214
Value *string `json:"value,omitempty" jsonschema:"anyof_required=value" mapstructure:"value" yaml:"value,omitempty"`
@@ -66,6 +58,12 @@ func (ev *EnvString) UnmarshalJSON(b []byte) error {
6658
return nil
6759
}
6860

61+
// IsZero checks if the instance is empty.
62+
func (ev EnvString) IsZero() bool {
63+
return (ev.Variable == nil || *ev.Variable == "") &&
64+
ev.Value == nil
65+
}
66+
6967
// Get gets literal value or from system environment.
7068
func (ev EnvString) Get() (string, error) {
7169
err := validateEnvironmentValue(ev.Value, ev.Variable)
@@ -138,6 +136,12 @@ func NewEnvIntVariable(name string) EnvInt {
138136
}
139137
}
140138

139+
// IsZero checks if the instance is empty.
140+
func (ev EnvInt) IsZero() bool {
141+
return (ev.Variable == nil || *ev.Variable == "") &&
142+
ev.Value == nil
143+
}
144+
141145
// UnmarshalJSON implements json.Unmarshaler.
142146
func (ev *EnvInt) UnmarshalJSON(b []byte) error {
143147
type Plain EnvInt
@@ -222,6 +226,12 @@ func NewEnvBoolVariable(name string) EnvBool {
222226
}
223227
}
224228

229+
// IsZero checks if the instance is empty.
230+
func (ev EnvBool) IsZero() bool {
231+
return (ev.Variable == nil || *ev.Variable == "") &&
232+
ev.Value == nil
233+
}
234+
225235
// UnmarshalJSON implements json.Unmarshaler.
226236
func (ev *EnvBool) UnmarshalJSON(b []byte) error {
227237
type Plain EnvBool
@@ -306,6 +316,12 @@ func NewEnvFloatVariable(name string) EnvFloat {
306316
}
307317
}
308318

319+
// IsZero checks if the instance is empty.
320+
func (ev EnvFloat) IsZero() bool {
321+
return (ev.Variable == nil || *ev.Variable == "") &&
322+
ev.Value == nil
323+
}
324+
309325
// UnmarshalJSON implements json.Unmarshaler.
310326
func (ev *EnvFloat) UnmarshalJSON(b []byte) error {
311327
type Plain EnvFloat
@@ -390,6 +406,12 @@ func NewEnvMapStringVariable(name string) EnvMapString {
390406
}
391407
}
392408

409+
// IsZero checks if the instance is empty.
410+
func (ev EnvMapString) IsZero() bool {
411+
return (ev.Variable == nil || *ev.Variable == "") &&
412+
ev.Value == nil
413+
}
414+
393415
// UnmarshalJSON implements json.Unmarshaler.
394416
func (ev *EnvMapString) UnmarshalJSON(b []byte) error {
395417
type Plain EnvMapString
@@ -456,6 +478,12 @@ func NewEnvMapIntVariable(name string) EnvMapInt {
456478
}
457479
}
458480

481+
// IsZero checks if the instance is empty.
482+
func (ev EnvMapInt) IsZero() bool {
483+
return (ev.Variable == nil || *ev.Variable == "") &&
484+
ev.Value == nil
485+
}
486+
459487
// UnmarshalJSON implements json.Unmarshaler.
460488
func (ev *EnvMapInt) UnmarshalJSON(b []byte) error {
461489
type Plain EnvMapInt
@@ -522,6 +550,12 @@ func NewEnvMapFloatVariable(name string) EnvMapFloat {
522550
}
523551
}
524552

553+
// IsZero checks if the instance is empty.
554+
func (ev EnvMapFloat) IsZero() bool {
555+
return (ev.Variable == nil || *ev.Variable == "") &&
556+
ev.Value == nil
557+
}
558+
525559
// UnmarshalJSON implements json.Unmarshaler.
526560
func (ev *EnvMapFloat) UnmarshalJSON(b []byte) error {
527561
type Plain EnvMapFloat
@@ -588,6 +622,12 @@ func NewEnvMapBoolVariable(name string) EnvMapBool {
588622
}
589623
}
590624

625+
// IsZero checks if the instance is empty.
626+
func (ev EnvMapBool) IsZero() bool {
627+
return (ev.Variable == nil || *ev.Variable == "") &&
628+
ev.Value == nil
629+
}
630+
591631
// UnmarshalJSON implements json.Unmarshaler.
592632
func (ev *EnvMapBool) UnmarshalJSON(b []byte) error {
593633
type Plain EnvMapBool
@@ -625,11 +665,3 @@ func (ev EnvMapBool) Get() (map[string]bool, error) {
625665

626666
return ev.Value, nil
627667
}
628-
629-
func getEnvVariableValueRequiredError(envName *string) error {
630-
if envName != nil {
631-
return fmt.Errorf("%s: %w", *envName, ErrEnvironmentVariableValueRequired)
632-
}
633-
634-
return ErrEnvironmentVariableValueRequired
635-
}

environment_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func TestEnvString(t *testing.T) {
4747
} else {
4848
assertNilError(t, err)
4949
assertDeepEqual(t, result, tc.Expected)
50+
assertDeepEqual(t, tc.Input.IsZero(), false)
5051
}
5152
})
5253
}
@@ -118,6 +119,7 @@ func TestEnvBool(t *testing.T) {
118119
newValue, err := tc.Input.GetOrDefault(true)
119120
assertNilError(t, err)
120121
assertDeepEqual(t, newValue, tc.Expected)
122+
assertDeepEqual(t, tc.Input.IsZero(), false)
121123
}
122124
})
123125
}
@@ -134,6 +136,8 @@ func TestEnvBool(t *testing.T) {
134136
result, err := NewEnvBoolVariable("SOME_TRUE").GetOrDefault(true)
135137
assertNilError(t, err)
136138
assertDeepEqual(t, true, result)
139+
140+
assertDeepEqual(t, EnvBool{}.IsZero(), true)
137141
})
138142
}
139143

@@ -182,13 +186,16 @@ func TestEnvInt(t *testing.T) {
182186
assertNilError(t, err)
183187
assertDeepEqual(t, newValue, int64(100))
184188
}
189+
185190
} else {
186191
assertNilError(t, err)
187192
assertDeepEqual(t, result, tc.Expected)
188193

189194
newValue, err := tc.Input.GetOrDefault(100)
190195
assertNilError(t, err)
191196
assertDeepEqual(t, newValue, tc.Expected)
197+
198+
assertDeepEqual(t, tc.Input.IsZero(), false)
192199
}
193200
})
194201
}
@@ -254,6 +261,7 @@ func TestEnvFloat(t *testing.T) {
254261
newValue, err := tc.Input.GetOrDefault(100)
255262
assertNilError(t, err)
256263
assertDeepEqual(t, newValue, tc.Expected)
264+
assertDeepEqual(t, tc.Input.IsZero(), false)
257265
}
258266
})
259267
}
@@ -313,6 +321,7 @@ func TestEnvMapBool(t *testing.T) {
313321
} else {
314322
assertNilError(t, err)
315323
assertDeepEqual(t, result, tc.Expected)
324+
assertDeepEqual(t, tc.Input.IsZero(), tc.Expected == nil)
316325
}
317326
})
318327
}
@@ -375,6 +384,7 @@ func TestEnvMapInt(t *testing.T) {
375384
} else {
376385
assertNilError(t, err)
377386
assertDeepEqual(t, result, tc.Expected)
387+
assertDeepEqual(t, tc.Input.IsZero(), tc.Expected == nil)
378388
}
379389
})
380390
}
@@ -437,6 +447,7 @@ func TestEnvMapFloat(t *testing.T) {
437447
} else {
438448
assertNilError(t, err)
439449
assertDeepEqual(t, result, tc.Expected)
450+
assertDeepEqual(t, tc.Input.IsZero(), tc.Expected == nil)
440451
}
441452
})
442453
}
@@ -500,6 +511,7 @@ func TestEnvMapString(t *testing.T) {
500511
} else {
501512
assertNilError(t, err)
502513
assertDeepEqual(t, result, tc.Expected)
514+
assertDeepEqual(t, tc.Input.IsZero(), tc.Expected == nil)
503515
}
504516
})
505517
}

utils.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ const (
1111
keyValueLength = 2
1212
)
1313

14-
// ErrParseStringFailed is the error when failed to parse a string to another type.
15-
var ErrParseStringFailed = errors.New("ParseStringFailed")
14+
var (
15+
errEnvironmentValueRequired = errors.New("require either value or env")
16+
// ErrEnvironmentVariableRequired the error happens when the name of environment variable is empty.
17+
ErrEnvironmentVariableRequired = errors.New("the environment variable name is empty")
18+
// ErrEnvironmentVariableValueRequired the error happens when the value from environment variable is empty.
19+
ErrEnvironmentVariableValueRequired = errors.New("the environment variable value is empty")
20+
// ErrParseStringFailed is the error when failed to parse a string to another type.
21+
ErrParseStringFailed = errors.New("ParseStringFailed")
22+
)
1623

1724
// ParseStringMapFromString parses a string map from a string with format:
1825
//
@@ -154,3 +161,11 @@ func validateEnvironmentMapValue(variable *string) error {
154161

155162
return nil
156163
}
164+
165+
func getEnvVariableValueRequiredError(envName *string) error {
166+
if envName != nil {
167+
return fmt.Errorf("%s: %w", *envName, ErrEnvironmentVariableValueRequired)
168+
}
169+
170+
return ErrEnvironmentVariableValueRequired
171+
}

0 commit comments

Comments
 (0)