-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.go
More file actions
203 lines (184 loc) · 4.8 KB
/
Copy pathdecoder.go
File metadata and controls
203 lines (184 loc) · 4.8 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package klon
import (
"encoding"
"errors"
"fmt"
"reflect"
"github.com/ProCode-Software/klar/internal/ranges"
"github.com/ProCode-Software/klar/pkg/klon/ast"
"github.com/ProCode-Software/klar/pkg/klon/klonerrs"
"github.com/ProCode-Software/klar/pkg/klon/klonflags"
)
var (
unmarshallerType = reflect.TypeFor[Unmarshaller]()
textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
)
type decoder struct {
ctx *Context
flags klonflags.Flags
vars map[string]ast.Value
}
type decodeFunc func(reflect.Value, ast.Value, *decoder) error
var decodeCache = makeCache[reflect.Type, decodeFunc]()
func decode(rd *reader, ctx *Context, v any, flgs ...klonflags.Flags) (err error) {
defer handlePanic(&err)
doc, errs := rd.parseDocument()
if len(errs) > 0 {
return errs[0]
}
return decodeDocument(doc, ctx, v, flgs...)
}
func decodeDocument(doc *ast.Document, ctx *Context, v any, flgs ...klonflags.Flags) error {
rv := reflect.ValueOf(v)
// Make sure v is a non-nil pointer
rt := rv.Type()
switch {
case rt == nil:
return errors.New("klon: nil argument passed to Unmarshall")
case rv.Kind() != reflect.Pointer:
return fmt.Errorf("klon: non-pointer type %s passed to Unmarshall", rt.String())
case rv.IsNil():
return fmt.Errorf("klon: nil %s passed to Unmarshall", rt.String())
}
d := &decoder{
ctx: ctx,
vars: doc.Variables,
flags: parseFlags(flgs...),
}
return d.decodeValue(doc.Body, rv.Elem())
}
func (d *decoder) decodeValue(val ast.Value, rv reflect.Value) error {
decode := d.getDecoder(rv.Type())
return decode(rv, val, d)
}
// Looks up a decoder or creates one if it doesn't exist.
func (d *decoder) getDecoder(rt reflect.Type) decodeFunc {
if decode, ok := decodeCache.get(rt); ok {
return decode
}
// If the type implements [Unmarshaller] or [encoding.TextUnmarshaler], use their decoder.
var decode decodeFunc
ptr := reflect.PointerTo(rt)
switch {
case rt.Implements(unmarshallerType), ptr.Implements(unmarshallerType):
decode = decodeUnmarshaller
case rt.Implements(textUnmarshalerType), ptr.Implements(textUnmarshalerType):
decode = decodeTextUnmarshaller
default:
decode = d.makeDefaultDecoder(rt)
}
decode = preprocessValue(decode)
decodeCache.set(rt, decode)
return decode
}
func typeMismatchError(rv reflect.Value, val ast.Value) *Error {
goType := rv.Type()
var msg string
if rv.Kind() == reflect.Interface {
msg = "Can't use " + FormatNodeType(val) + " as a value"
} else {
msg = "Expected " + formatGoType(goType) + ", but found " + FormatNodeType(val)
}
return &Error{
Code: klonerrs.ErrTypeMismatch,
Type: goType,
Value: val,
Range: val.Pos(),
Text: msg,
isDecodeErr: true,
}
}
func formatGoType(rt reflect.Type) string {
switch k := rt.Kind(); k {
case reflect.String:
return "a string"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float64, reflect.Float32:
return "a number"
case reflect.Bool:
return "a boolean"
case reflect.Slice, reflect.Array:
return "a list"
case reflect.Map, reflect.Struct:
return "an object"
case reflect.Pointer:
return formatGoType(rt.Elem())
default:
return k.String()
}
}
func FormatNodeType(node ast.Value) string {
switch node.(type) {
case *ast.String, *ast.StringGroup:
return "a string"
case *ast.Number:
return "a number"
case *ast.Boolean:
return "a boolean"
case *ast.List:
return "a list"
case *ast.Object:
return "an object"
case *ast.None:
return "no value"
default:
panic(fmt.Sprintf("unhandled node type: %T", node))
}
}
func decodeError(code klonerrs.Code, rv reflect.Value, val ast.Value,
msg string, v ...any,
) *Error {
var errMsg string
if len(v) > 0 {
errMsg = fmt.Sprintf(msg, v...)
} else {
errMsg = msg
}
var rt reflect.Type
if rv.IsValid() {
rt = rv.Type()
}
var pos ranges.Range
if val != nil {
pos = val.Pos()
}
return &Error{
Code: code,
Text: errMsg,
Type: rt,
Value: val,
Range: pos,
isDecodeErr: true,
}
}
func (d *decoder) warn(e error) {
w, ok := e.(*Error)
if ok {
w.Warning = true
d.ctx.Warnings = append(d.ctx.Warnings, w)
}
}
func (d *decoder) shouldWarn(err error) bool {
e, ok := err.(*Error)
if !ok || d.ctx == nil || d.ctx.WarningKinds == nil || d.ctx.Warnings == nil {
return false
}
_, ok = d.ctx.WarningKinds[e.Code]
return ok
}
func (d *decoder) valueToString(v ast.Value) (string, error) {
switch v := v.(type) {
case *ast.String:
return d.evaluateString(v)
case *ast.Boolean:
return v.String(), nil
case *ast.Number:
return v.Source, nil
default:
return "", decodeError(
klonerrs.ErrCantConvertToString, reflect.Value{}, v,
"Can't convert %s to a string", FormatNodeType(v),
)
}
}