@@ -50,13 +50,30 @@ type NonImplWrapperType[T any] struct {
50
50
inner T `mapstructure:"-"`
51
51
}
52
52
53
+ var _ confmap.Unmarshaler = (* wrapperType [any ])(nil )
53
54
var _ ScalarMarshaler = wrapperType [any ]{}
54
55
var _ ScalarUnmarshaler = (* wrapperType [any ])(nil )
55
56
56
57
type wrapperType [T any ] struct {
57
58
inner T `mapstructure:"-"`
58
59
}
59
60
61
+ func (wt * wrapperType [T ]) Unmarshal (conf * confmap.Conf ) error {
62
+ if err := conf .Unmarshal (& wt .inner , WithScalarUnmarshaler ()); err != nil {
63
+ return err
64
+ }
65
+
66
+ return nil
67
+ }
68
+
69
+ func (wt wrapperType [T ]) Marshal (conf * confmap.Conf ) error {
70
+ if err := conf .Marshal (wt .inner , WithScalarMarshaler ()); err != nil {
71
+ return fmt .Errorf ("failed to marshal wrapperType value: %w" , err )
72
+ }
73
+
74
+ return nil
75
+ }
76
+
60
77
func (wt wrapperType [T ]) MarshalScalar (in * string ) (any , error ) {
61
78
if in != nil {
62
79
return * in , nil
@@ -65,15 +82,15 @@ func (wt wrapperType[T]) MarshalScalar(in *string) (any, error) {
65
82
return wt .inner , nil
66
83
}
67
84
68
- func (wt wrapperType [T ]) GetScalarValue () any {
69
- return wt .inner
85
+ func (wt wrapperType [T ]) GetScalarValue () ( any , error ) {
86
+ return wt .inner , nil
70
87
}
71
88
72
89
func (wt * wrapperType [T ]) UnmarshalScalar (val any ) error {
73
90
v , ok := val .(T )
74
91
75
92
if ! ok {
76
- return fmt .Errorf ("val is %T, not %T" , val , v )
93
+ return fmt .Errorf ("could not unmarshal scalar: val is %T, not %T" , val , v )
77
94
}
78
95
79
96
wt .inner = v
@@ -86,16 +103,17 @@ func (wt *wrapperType[T]) ScalarType() any {
86
103
87
104
type testConfig struct {
88
105
// Handled by confmap, treated as string
89
- Tma textMarshalerAlias `mapstructure:"tma"`
90
- Ntma nonTextMarshalerAlias `mapstructure:"ntma"`
91
- Nonimplint NonImplWrapperType [int ] `mapstructure:"non_impl_int"`
92
- Nonimplstr NonImplWrapperType [string ] `mapstructure:"non_impl_str"`
93
- Nonimpltms NonImplWrapperType [textMarshalerStruct ] `mapstructure:"non_impl_tms"`
94
- Nonimplntms NonImplWrapperType [nonTextMarshalerStruct ] `mapstructure:"non_impl_ntms"`
95
- Implint wrapperType [int ] `mapstructure:"impl_int"`
96
- Implstr wrapperType [string ] `mapstructure:"impl_str"`
97
- Impltms wrapperType [textMarshalerStruct ] `mapstructure:"impl_tms"`
98
- Implntms wrapperType [nonTextMarshalerStruct ] `mapstructure:"impl_ntms"`
106
+ Tma textMarshalerAlias `mapstructure:"tma"`
107
+ Ntma nonTextMarshalerAlias `mapstructure:"ntma"`
108
+ Nonimplint NonImplWrapperType [int ] `mapstructure:"non_impl_int"`
109
+ Nonimplstr NonImplWrapperType [string ] `mapstructure:"non_impl_str"`
110
+ Nonimpltms NonImplWrapperType [textMarshalerStruct ] `mapstructure:"non_impl_tms"`
111
+ Nonimplntms NonImplWrapperType [nonTextMarshalerStruct ] `mapstructure:"non_impl_ntms"`
112
+ Implint wrapperType [int ] `mapstructure:"impl_int"`
113
+ Implstr wrapperType [string ] `mapstructure:"impl_str"`
114
+ Impltms wrapperType [textMarshalerStruct ] `mapstructure:"impl_tms"`
115
+ Implntms wrapperType [nonTextMarshalerStruct ] `mapstructure:"impl_ntms"`
116
+ Recursive wrapperType [wrapperType [textMarshalerStruct ]] `mapstructure:"recursive"`
99
117
}
100
118
101
119
func (cfg * testConfig ) Unmarshal (conf * confmap.Conf ) error {
@@ -125,6 +143,7 @@ func TestMarshalConfig(t *testing.T) {
125
143
Implstr : wrapperType [string ]{inner : "test" },
126
144
Impltms : wrapperType [textMarshalerStruct ]{inner : textMarshalerStruct {id : 0 , data : []byte {80 }}},
127
145
Implntms : wrapperType [nonTextMarshalerStruct ]{inner : nonTextMarshalerStruct {id : 2 , data : []byte {80 }}},
146
+ Recursive : wrapperType [wrapperType [textMarshalerStruct ]]{inner : wrapperType [textMarshalerStruct ]{inner : textMarshalerStruct {id : 2 , data : []byte {80 }}}},
128
147
}
129
148
130
149
require .NoError (t , conf .Marshal (cfg , WithScalarMarshaler ()))
0 commit comments