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