Skip to content

Commit de6cae1

Browse files
authored
chore: Update config pkg to env (#3)
1 parent f498862 commit de6cae1

6 files changed

Lines changed: 37 additions & 11 deletions

File tree

config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/kzs0/bedrock/config"
10+
"github.com/kzs0/bedrock/env"
1111
"github.com/kzs0/bedrock/trace"
1212
)
1313

@@ -87,7 +87,7 @@ func DefaultConfig() Config {
8787

8888
// FromEnv loads configuration from environment variables.
8989
func FromEnv() (Config, error) {
90-
cfg, err := config.Parse[Config]()
90+
cfg, err := env.Parse[Config]()
9191
if err != nil {
9292
return Config{}, fmt.Errorf("bedrock: failed to parse config from env: %w", err)
9393
}

config/config.go renamed to env/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package env
22

33
import (
44
"fmt"
@@ -16,7 +16,7 @@ func ParseWithPrefix[T any](prefix string) (T, error) {
1616
var cfg T
1717
err := parseStruct(reflect.ValueOf(&cfg).Elem(), prefix)
1818
if err != nil {
19-
return cfg, fmt.Errorf("config: %w", err)
19+
return cfg, fmt.Errorf("env: %w", err)
2020
}
2121
return cfg, nil
2222
}
@@ -27,7 +27,7 @@ func ParseWithPrefix[T any](prefix string) (T, error) {
2727
func From[T any](cfg T) (T, error) {
2828
v := reflect.ValueOf(&cfg).Elem()
2929
if err := validateStruct(v, ""); err != nil {
30-
return cfg, fmt.Errorf("config: %w", err)
30+
return cfg, fmt.Errorf("env: %w", err)
3131
}
3232
return cfg, nil
3333
}
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package env
22

33
import (
44
"os"
@@ -194,3 +194,29 @@ func TestFrom(t *testing.T) {
194194
t.Errorf("expected name 'test', got %q", validated.Name)
195195
}
196196
}
197+
198+
type IgnoredFieldConfig struct {
199+
Name string `env:"NAME"`
200+
Secret string `env:"-"`
201+
}
202+
203+
func TestIgnoredFields(t *testing.T) {
204+
_ = os.Setenv("NAME", "test")
205+
_ = os.Setenv("-", "should-be-ignored")
206+
defer func() {
207+
_ = os.Unsetenv("NAME")
208+
_ = os.Unsetenv("-")
209+
}()
210+
211+
cfg, err := Parse[IgnoredFieldConfig]()
212+
if err != nil {
213+
t.Fatalf("unexpected error: %v", err)
214+
}
215+
216+
if cfg.Name != "test" {
217+
t.Errorf("expected name 'test', got %q", cfg.Name)
218+
}
219+
if cfg.Secret != "" {
220+
t.Errorf("expected secret to be empty (ignored), got %q", cfg.Secret)
221+
}
222+
}

config/env.go renamed to env/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package env
22

33
import (
44
"fmt"

config/parser.go renamed to env/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package env
22

33
import (
44
"reflect"
@@ -16,7 +16,7 @@ type tag struct {
1616
// parseTag parses the env struct tag.
1717
func parseTag(field reflect.StructField) (tag, error) {
1818
envTag := field.Tag.Get("env")
19-
if envTag == "" {
19+
if envTag == "" || envTag == "-" {
2020
return tag{}, nil
2121
}
2222

example/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"github.com/kzs0/bedrock"
1414
"github.com/kzs0/bedrock/attr"
15-
"github.com/kzs0/bedrock/config"
15+
"github.com/kzs0/bedrock/env"
1616
)
1717

1818
type Config struct {
@@ -22,7 +22,7 @@ type Config struct {
2222

2323
func main() {
2424
ctx := context.Background()
25-
cfg, err := config.Parse[Config]()
25+
cfg, err := env.Parse[Config]()
2626
if err != nil {
2727
// Use defaults if config parsing fails
2828
cfg = Config{

0 commit comments

Comments
 (0)