File: textmarshal_test.go

package info (click to toggle)
golang-github-knadh-koanf 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 808 kB
  • sloc: sh: 31; makefile: 14
file content (107 lines) | stat: -rw-r--r-- 2,713 bytes parent folder | download
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)
}