| 1 |
package koanf_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding" |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/knadh/koanf/providers/env/v2" |
| 10 |
"github.com/knadh/koanf/providers/structs" |
| 11 |
"github.com/knadh/koanf/v2" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
) |
| 14 |
|
| 15 |
func TestTextUnmarshalStringFixed(t *testing.T) { |
| 16 |
defer func() { |
| 17 |
assert.Nil(t, recover()) |
| 18 |
}() |
| 19 |
|
| 20 |
type targetStruct struct { |
| 21 |
LogFormatPointer LogFormatPointer // default should map to json |
| 22 |
LogFormatValue LogFormatValue // default should map to json |
| 23 |
} |
| 24 |
|
| 25 |
target := &targetStruct{"text_custom", "text_custom"} |
| 26 |
before := *target |
| 27 |
|
| 28 |
var ( |
| 29 |
bptr any = &(target.LogFormatPointer) |
| 30 |
cptr any = target.LogFormatValue |
| 31 |
) |
| 32 |
_, ok := (bptr).(encoding.TextMarshaler) |
| 33 |
assert.True(t, ok) |
| 34 |
|
| 35 |
_, ok = (cptr).(encoding.TextMarshaler) |
| 36 |
assert.True(t, ok) |
| 37 |
|
| 38 |
k := koanf.New(".") |
| 39 |
k.Load(structs.Provider(target, "koanf"), nil) |
| 40 |
|
| 41 |
k.Load(env.Provider(".", env.Opt{ |
| 42 |
TransformFunc: func(k string, v string) (string, any) { |
| 43 |
return strings.ReplaceAll(strings.ToLower(k), "_", "."), v |
| 44 |
}, |
| 45 |
}), nil) |
| 46 |
|
| 47 |
// default values |
| 48 |
err := k.Unmarshal("", &target) |
| 49 |
assert.NoError(t, err) |
| 50 |
assert.Equal(t, &before, target) |
| 51 |
} |
| 52 |
|
| 53 |
// LogFormatValue is a custom string type that implements the TextUnmarshaler interface |
| 54 |
// Additionally it implements the TextMarshaler interface (value receiver) |
| 55 |
type LogFormatValue string |
| 56 |
|
| 57 |
// pointer receiver |
| 58 |
func (c *LogFormatValue) UnmarshalText(data []byte) error { |
| 59 |
switch strings.ToLower(string(data)) { |
| 60 |
case "", "json": |
| 61 |
*c = "json_custom" |
| 62 |
case "text": |
| 63 |
*c = "text_custom" |
| 64 |
default: |
| 65 |
return fmt.Errorf("invalid log format: %s", string(data)) |
| 66 |
} |
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
// value receiver |
| 71 |
func (c LogFormatValue) MarshalText() ([]byte, error) { |
| 72 |
// overcomplicated custom internal string representation |
| 73 |
switch c { |
| 74 |
case "", "json_custom": |
| 75 |
return []byte("json"), nil |
| 76 |
case "text_custom": |
| 77 |
return []byte("text"), nil |
| 78 |
} |
| 79 |
return nil, fmt.Errorf("invalid internal string representation: %q", c) |
| 80 |
} |
| 81 |
|
| 82 |
// LogFormatPointer is a custom string type that implements the TextUnmarshaler interface |
| 83 |
// Additionally it implements the TextMarshaler interface (pointer receiver) |
| 84 |
type LogFormatPointer string |
| 85 |
|
| 86 |
// pointer receiver |
| 87 |
func (c *LogFormatPointer) UnmarshalText(data []byte) error { |
| 88 |
switch strings.ToLower(string(data)) { |
| 89 |
case "", "json": |
| 90 |
*c = "json_custom" |
| 91 |
case "text": |
| 92 |
*c = "text_custom" |
| 93 |
default: |
| 94 |
return fmt.Errorf("invalid log format: %s", string(data)) |
| 95 |
} |
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
// also pointer receiver |
| 100 |
func (c *LogFormatPointer) MarshalText() ([]byte, error) { |
| 101 |
// overcomplicated custom internal string representation |
| 102 |
switch *c { |
| 103 |
case "", "json_custom": |
| 104 |
return []byte("json"), nil |
| 105 |
case "text_custom": |
| 106 |
return []byte("text"), nil |
| 107 |
} |
| 108 |
return nil, fmt.Errorf("invalid internal string representation: %q", *c) |
| 109 |
} |