Skip to content

Commit 867b7dd

Browse files
committed
re-added v1 code
1 parent a30825a commit 867b7dd

22 files changed

+1348
-0
lines changed

Diff for: bool.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2018 Philipp Brüll <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package env
16+
17+
import (
18+
"os"
19+
)
20+
21+
const (
22+
trueValue = "true"
23+
falseValue = "false"
24+
)
25+
26+
// BoolField implements a duration field.
27+
type BoolField struct {
28+
*field
29+
defaultValue bool
30+
}
31+
32+
// Bool registers a field of the provided name.
33+
func Bool(name string, defaultValue bool, opts ...Option) *BoolField {
34+
field := &BoolField{
35+
field: newField("Boolean", name, append(opts, AllowedValues("0", "1", falseValue, trueValue, "no", "yes"))),
36+
defaultValue: defaultValue,
37+
}
38+
RegisterField(field)
39+
return field
40+
}
41+
42+
// Value returns the field's value.
43+
func (i *BoolField) Value() string {
44+
if i.Get() {
45+
return trueValue
46+
}
47+
return falseValue
48+
}
49+
50+
// DefaultValue returns the field's default value.
51+
func (i *BoolField) DefaultValue() string {
52+
if i.defaultValue {
53+
return trueValue
54+
}
55+
return falseValue
56+
}
57+
58+
// Description returns the field's description.
59+
func (i *BoolField) Description() string {
60+
return i.description(i.DefaultValue())
61+
}
62+
63+
// Get returns the field value or the default value.
64+
func (i *BoolField) Get() bool {
65+
text := os.Getenv(i.Name())
66+
if text == "" {
67+
if i.options.required {
68+
requiredError(i)
69+
}
70+
return i.defaultValue
71+
}
72+
if !i.options.isAllowedValue(text) {
73+
unallowedError(i, text, i.options.allowedValues)
74+
return i.defaultValue
75+
}
76+
switch text {
77+
case "1", trueValue, "yes":
78+
return true
79+
default:
80+
return false
81+
}
82+
}

Diff for: bool_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2018 Philipp Brüll <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package env_test
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"testing"
21+
22+
"github.com/simia-tech/env"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func TestBool(t *testing.T) {
28+
var (
29+
optional = env.Bool("OPTIONAL_FIELD", false)
30+
required = env.Bool("REQUIRED_FIELD", false, env.Required())
31+
lastErr error
32+
)
33+
env.ErrorHandler = func(err error) {
34+
lastErr = err
35+
}
36+
37+
testFn := func(field *env.BoolField, value string, expectValue bool, expectErr error) func(*testing.T) {
38+
return func(t *testing.T) {
39+
require.NoError(t, os.Setenv(field.Name(), value))
40+
41+
assert.Equal(t, expectValue, field.Get())
42+
43+
if expectErr != nil {
44+
assert.Equal(t, expectErr, lastErr)
45+
lastErr = nil
46+
}
47+
}
48+
}
49+
50+
t.Run("Value", testFn(optional, "1", true, nil))
51+
t.Run("DefaultValue", testFn(optional, "", false, nil))
52+
t.Run("RequiredAndSet", testFn(required, "yes", true, nil))
53+
t.Run("RequiredNotSet", testFn(required, "", false, fmt.Errorf("required field REQUIRED_FIELD is not set - using default value 'false'")))
54+
t.Run("UnallowedValue", testFn(optional, "okaydokay", false, fmt.Errorf("field OPTIONAL_FIELD does not allow value 'okaydokay' (allowed values are '0', '1', 'false', 'true', 'no' and 'yes') - using default value 'false'")))
55+
}

Diff for: bytes.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2018 Philipp Brüll <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package env
16+
17+
import (
18+
"encoding/hex"
19+
"os"
20+
)
21+
22+
// BytesField implements a string field.
23+
type BytesField struct {
24+
*field
25+
defaultValue []byte
26+
}
27+
28+
// Bytes registers a field of the provided name.
29+
func Bytes(name string, defaultValue []byte, opts ...Option) *BytesField {
30+
field := &BytesField{
31+
field: newField("Bytes", name, opts),
32+
defaultValue: defaultValue,
33+
}
34+
RegisterField(field)
35+
return field
36+
}
37+
38+
// Value returns the field's value.
39+
func (bf *BytesField) Value() string {
40+
return hex.EncodeToString(bf.Get())
41+
}
42+
43+
// DefaultValue returns the field's default value.
44+
func (bf *BytesField) DefaultValue() string {
45+
return hex.EncodeToString(bf.defaultValue)
46+
}
47+
48+
// Description returns the field's description.
49+
func (bf *BytesField) Description() string {
50+
return bf.description(bf.DefaultValue())
51+
}
52+
53+
// Get returns the field value or the default value.
54+
func (bf *BytesField) Get() []byte {
55+
text := os.Getenv(bf.Name())
56+
if text == "" {
57+
if bf.options.required {
58+
requiredError(bf)
59+
}
60+
return bf.defaultValue
61+
}
62+
if !bf.options.isAllowedValue(text) {
63+
unallowedError(bf, text, bf.options.allowedValues)
64+
return bf.defaultValue
65+
}
66+
value, err := hex.DecodeString(text)
67+
if err != nil {
68+
parseError(bf, "bytes", text)
69+
return bf.defaultValue
70+
}
71+
return value
72+
}

Diff for: bytes_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2018 Philipp Brüll <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package env_test
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"testing"
21+
22+
"github.com/simia-tech/env"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func TestBytes(t *testing.T) {
28+
var (
29+
optional = env.Bytes("OPTIONAL_FIELD", []byte{0xab})
30+
required = env.Bytes("REQUIRED_FIELD", []byte{0xab}, env.Required())
31+
allowed = env.Bytes("ALLOWED_FIELD", []byte{0xab}, env.AllowedValues("ab", "cd"))
32+
lastErr error
33+
)
34+
env.ErrorHandler = func(err error) {
35+
lastErr = err
36+
}
37+
38+
testFn := func(field *env.BytesField, value string, expectValue []byte, expectErr error) func(*testing.T) {
39+
return func(t *testing.T) {
40+
require.NoError(t, os.Setenv(field.Name(), value))
41+
42+
assert.Equal(t, expectValue, field.Get())
43+
44+
if expectErr != nil {
45+
assert.Equal(t, expectErr, lastErr)
46+
lastErr = nil
47+
}
48+
}
49+
}
50+
51+
t.Run("Value", testFn(optional, "cd", []byte{0xcd}, nil))
52+
t.Run("DefaultValue", testFn(optional, "", []byte{0xab}, nil))
53+
t.Run("RequiredAndSet", testFn(required, "cd", []byte{0xcd}, nil))
54+
t.Run("RequiredNotSet", testFn(required, "", []byte{0xab}, fmt.Errorf("required field REQUIRED_FIELD is not set - using default value 'ab'")))
55+
t.Run("AllowedValue", testFn(allowed, "cd", []byte{0xcd}, nil))
56+
t.Run("UnallowedValue", testFn(allowed, "ef", []byte{0xab}, fmt.Errorf("field ALLOWED_FIELD does not allow value 'ef' (allowed values are 'ab' and 'cd') - using default value 'ab'")))
57+
}

Diff for: duration.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2018 Philipp Brüll <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package env
16+
17+
import (
18+
"os"
19+
"time"
20+
)
21+
22+
// DurationField implements a duration field.
23+
type DurationField struct {
24+
*field
25+
defaultValue time.Duration
26+
}
27+
28+
// Duration registers a field of the provided name.
29+
func Duration(name string, defaultValue time.Duration, opts ...Option) *DurationField {
30+
field := &DurationField{
31+
field: newField("Duration", name, opts),
32+
defaultValue: defaultValue,
33+
}
34+
RegisterField(field)
35+
return field
36+
}
37+
38+
// Value returns the field's value.
39+
func (df *DurationField) Value() string {
40+
return df.Get().String()
41+
}
42+
43+
// DefaultValue returns the field's default value.
44+
func (df *DurationField) DefaultValue() string {
45+
return df.defaultValue.String()
46+
}
47+
48+
// Description returns the field's description.
49+
func (df *DurationField) Description() string {
50+
return df.description(df.DefaultValue())
51+
}
52+
53+
// Get returns the field value or the default value.
54+
func (df *DurationField) Get() time.Duration {
55+
text := os.Getenv(df.Name())
56+
if text == "" {
57+
if df.options.required {
58+
requiredError(df)
59+
}
60+
return df.defaultValue
61+
}
62+
if !df.options.isAllowedValue(text) {
63+
unallowedError(df, text, df.options.allowedValues)
64+
return df.defaultValue
65+
}
66+
value, err := time.ParseDuration(text)
67+
if err != nil {
68+
parseError(df, "duration", text)
69+
return df.defaultValue
70+
}
71+
return value
72+
}

0 commit comments

Comments
 (0)