Skip to content

Commit 6147c0e

Browse files
committed
improve godocs
1 parent 4a0f3e5 commit 6147c0e

File tree

7 files changed

+63
-42
lines changed

7 files changed

+63
-42
lines changed

env.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import (
55
"os"
66
)
77

8-
// EnvResolver resolves values using environment variables.
9-
// Usage: "env:MY_VAR" -> returns value of MY_VAR
8+
// Resolves a value from environment variables.
9+
// The value after the prefix should be the name of the environment variable.
10+
// Example:
11+
// "env:MY_ENV_VAR"
12+
// would return the value of the MY_ENV_VAR environment variable.
13+
//
14+
// If the variable is not set, an error is returned.
1015
type EnvResolver struct{}
1116

1217
func (r *EnvResolver) Resolve(value string) (string, error) {

file.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ import (
88
"strings"
99
)
1010

11-
// Resolves values from key = values files. The format can be:
12-
// "file:/config/app.txt//Key"
13-
// If no key is provided, returns the whole file.
11+
// Resolves a value by reading a key from a plain key=value text file.
12+
// The value after the prefix should be in the format "path/to/file.txt//Key"
13+
// If no key is provided, returns the entire file as a string.
14+
// Example:
15+
// "file:/config/app.txt//USERNAME"
16+
// would search for a line like "USERNAME = alice" and return "alice".
17+
//
18+
// Lines are matched by exact key name before the equals sign.
19+
// If no key is provided (no "//" present), returns the entire file as string.
1420
type KeyValueFileResolver struct{}
1521

1622
func (f *KeyValueFileResolver) Resolve(value string) (string, error) {

ini.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import (
88
"gopkg.in/ini.v1"
99
)
1010

11+
// Resolves a value by loading an INI file and extracting a section.key pair.
12+
// The value after the prefix should be in the format "path/to/file.ini//Section.Key"
13+
// If no section is provided, the default section is used.
14+
// If no key is provided, returns the entire INI file as a string.
15+
// Example:
16+
// "ini:/config/app.ini//Database.User"
17+
// would load app.ini, locate the [Database] section, and return the value of User.
18+
//
19+
// Keys are navigated via "Section.Key" notation.
20+
// If no key is provided (no "//" present), returns the entire INI file as string.
1121
type INIResolver struct{}
1222

1323
func (r *INIResolver) Resolve(value string) (string, error) {

json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r *JSONResolver) Resolve(value string) (string, error) {
3232
return strings.TrimSpace(string(data)), nil
3333
}
3434

35-
var content map[string]interface{}
35+
var content map[string]any
3636
if err := json.Unmarshal(data, &content); err != nil {
3737
return "", fmt.Errorf("failed to parse JSON in '%s': %w", filePath, err)
3838
}

utils.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ func splitFileAndKey(value string) (string, string) {
2828
// - Take the 0th element of that slice
2929
// - Expect that element to be a map with a "host" key
3030
// - Return the value at "host"
31-
func navigateData(data interface{}, keys []string) (interface{}, error) {
31+
func navigateData(data any, keys []string) (any, error) {
3232
current := data
3333
for _, k := range keys {
3434
switch curr := current.(type) {
35-
case map[string]interface{}:
35+
case map[string]any:
3636
// Current data is a map, so k is a field name
3737
val, ok := curr[k]
3838
if !ok {
3939
return nil, fmt.Errorf("key '%s' not found", k)
4040
}
4141
current = val
4242

43-
case []interface{}:
43+
case []any:
4444
// Current data is a slice, so k should be a numeric index
4545
idx, err := strconv.Atoi(k)
4646
if err != nil {
@@ -59,11 +59,11 @@ func navigateData(data interface{}, keys []string) (interface{}, error) {
5959
return current, nil
6060
}
6161

62-
// convertToMapStringInterface attempts to convert arbitrary YAML-parsed data into a map[string]interface{} for uniform handling.
62+
// convertToMapStringInterface attempts to convert arbitrary YAML-parsed data into a map[string]any for uniform handling.
6363
// It recursively ensures arrays and maps are properly converted.
64-
func convertToMapStringInterface(val interface{}) (map[string]interface{}, error) {
64+
func convertToMapStringInterface(val any) (map[string]any, error) {
6565
switch v := val.(type) {
66-
case map[string]interface{}:
66+
case map[string]any:
6767
// Recursively convert values
6868
for key, val2 := range v {
6969
converted, err := convertValue(val2)
@@ -75,14 +75,14 @@ func convertToMapStringInterface(val interface{}) (map[string]interface{}, error
7575
return v, nil
7676
default:
7777
// If it's not a map at the root level, return an empty map
78-
return map[string]interface{}{}, nil
78+
return map[string]any{}, nil
7979
}
8080
}
8181

82-
func convertValue(val interface{}) (interface{}, error) {
82+
func convertValue(val any) (any, error) {
8383
// Recursively convert slices and maps
8484
switch vv := val.(type) {
85-
case map[string]interface{}:
85+
case map[string]any:
8686
for k, v := range vv {
8787
converted, err := convertValue(v)
8888
if err != nil {
@@ -91,7 +91,7 @@ func convertValue(val interface{}) (interface{}, error) {
9191
vv[k] = converted
9292
}
9393
return vv, nil
94-
case []interface{}:
94+
case []any:
9595
for i, elem := range vv {
9696
converted, err := convertValue(elem)
9797
if err != nil {

utils_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ func TestUtils(t *testing.T) {
5151
t.Run("navigateData", func(t *testing.T) {
5252
t.Parallel()
5353

54-
data := map[string]interface{}{
55-
"server": map[string]interface{}{
54+
data := map[string]any{
55+
"server": map[string]any{
5656
"host": "localhost",
57-
"ports": []interface{}{80, 443},
58-
"nested": map[string]interface{}{
57+
"ports": []any{80, 443},
58+
"nested": map[string]any{
5959
"key": "value",
6060
},
6161
},
62-
"array": []interface{}{
63-
"zero", "one", map[string]interface{}{"nested": "val"},
62+
"array": []any{
63+
"zero", "one", map[string]any{"nested": "val"},
6464
},
6565
}
6666

@@ -122,16 +122,16 @@ func TestUtils(t *testing.T) {
122122

123123
t.Run("TopLevelMap", func(t *testing.T) {
124124
t.Parallel()
125-
input := map[string]interface{}{
125+
input := map[string]any{
126126
"key": "value",
127-
"nested": map[string]interface{}{
128-
"subKey": []interface{}{"val1", "val2"},
127+
"nested": map[string]any{
128+
"subKey": []any{"val1", "val2"},
129129
},
130130
}
131-
want := map[string]interface{}{
131+
want := map[string]any{
132132
"key": "value",
133-
"nested": map[string]interface{}{
134-
"subKey": []interface{}{"val1", "val2"},
133+
"nested": map[string]any{
134+
"subKey": []any{"val1", "val2"},
135135
},
136136
}
137137
got, err := convertToMapStringInterface(input)
@@ -141,36 +141,36 @@ func TestUtils(t *testing.T) {
141141

142142
t.Run("NonMapTopLevel", func(t *testing.T) {
143143
t.Parallel()
144-
input := []interface{}{"val1", "val2"}
145-
want := map[string]interface{}{}
144+
input := []any{"val1", "val2"}
145+
want := map[string]any{}
146146
got, err := convertToMapStringInterface(input)
147147
assert.NoError(t, err)
148148
assert.Equal(t, want, got)
149149
})
150150

151151
t.Run("AlreadyClean", func(t *testing.T) {
152152
t.Parallel()
153-
input := map[string]interface{}{"simple": "val"}
154-
want := map[string]interface{}{"simple": "val"}
153+
input := map[string]any{"simple": "val"}
154+
want := map[string]any{"simple": "val"}
155155
got, err := convertToMapStringInterface(input)
156156
assert.NoError(t, err)
157157
assert.Equal(t, want, got)
158158
})
159159

160160
t.Run("ComplexNestedStructures", func(t *testing.T) {
161161
t.Parallel()
162-
input := map[string]interface{}{
163-
"level1": map[string]interface{}{
164-
"level2": []interface{}{
165-
map[string]interface{}{"key": "val"},
162+
input := map[string]any{
163+
"level1": map[string]any{
164+
"level2": []any{
165+
map[string]any{"key": "val"},
166166
"stringVal",
167167
},
168168
},
169169
}
170-
want := map[string]interface{}{
171-
"level1": map[string]interface{}{
172-
"level2": []interface{}{
173-
map[string]interface{}{"key": "val"},
170+
want := map[string]any{
171+
"level1": map[string]any{
172+
"level2": []any{
173+
map[string]any{"key": "val"},
174174
"stringVal",
175175
},
176176
},

yaml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ func (r *YAMLResolver) Resolve(value string) (string, error) {
2222
return "", fmt.Errorf("failed to read YAML file '%s': %w", filePath, err)
2323
}
2424

25-
var content interface{}
25+
var content any
2626
if err := yaml.Unmarshal(data, &content); err != nil {
2727
return "", fmt.Errorf("failed to parse YAML in '%s': %w", filePath, err)
2828
}
2929

30-
// Convert YAML to map[string]interface{} if needed
30+
// Convert YAML to map[string]any if needed
3131
contentMap, err := convertToMapStringInterface(content)
3232
if err != nil {
3333
return "", fmt.Errorf("failed to process YAML '%s': %w", filePath, err)

0 commit comments

Comments
 (0)