-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
decoder.go
128 lines (112 loc) · 2.61 KB
/
decoder.go
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
// Copyright 2022 Franklin "Snaipe" Mathieu.
//
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
package encutil
import (
"fmt"
"io"
"io/fs"
"os"
"reflect"
"snai.pe/boa/encoding"
"snai.pe/boa/internal/reflectutil"
"snai.pe/boa/syntax"
)
type UnmarshalerBase struct {
NewParser func(io.Reader) syntax.Parser
Self reflectutil.Unmarshaler
encoding.CommonOptions
encoding.DecoderOptions
Extensions []string
}
type MultiFile interface {
Next(...string) error
File() fs.File
}
func (unmarshaler *UnmarshalerBase) Decode(in io.Reader, v interface{}) error {
ptr := reflect.ValueOf(v)
if ptr.Kind() != reflect.Ptr {
panic("decode: must pass in pointer value")
}
decode := func(in io.Reader) error {
root, err := unmarshaler.NewParser(in).Parse()
if err != nil {
if e, ok := err.(*syntax.Error); ok {
e.Filename = Name(in)
}
return err
}
if node, ok := v.(**syntax.Node); ok {
*node = root
return nil
}
err = reflectutil.Unmarshal(ptr.Elem(), root.Child, unmarshaler.NamingConvention, true, unmarshaler.Self)
if e, ok := err.(*encoding.LoadError); ok {
e.Filename = Name(in)
}
return err
}
if unmarshaler.LookupEnv == nil {
unmarshaler.LookupEnv = os.LookupEnv
}
var names []string
if unmarshaler.EnvPrefix != "" {
names = []string{unmarshaler.EnvPrefix}
}
switch f := in.(type) {
case MultiFile:
for {
if err := f.Next(unmarshaler.Extensions...); err != nil {
if err == fs.ErrNotExist {
break
}
return err
}
fin := f.File()
err := decode(fin)
fin.Close()
if err != nil {
return err
}
}
_, err := reflectutil.PopulateFromEnv(ptr.Elem(), unmarshaler.AutomaticEnv, names, unmarshaler.LookupEnv)
return err
default:
if err := decode(f); err != nil {
return err
}
_, err := reflectutil.PopulateFromEnv(ptr.Elem(), unmarshaler.AutomaticEnv, names, unmarshaler.LookupEnv)
return err
}
}
func (unmarshaler *UnmarshalerBase) Option(opts ...interface{}) error {
for _, opt := range opts {
switch setopt := opt.(type) {
case encoding.CommonOption:
setopt(&unmarshaler.CommonOptions)
case encoding.DecoderOption:
setopt(&unmarshaler.DecoderOptions)
default:
return fmt.Errorf("%T is not a common option, nor an decoder option.", opt)
}
}
return nil
}
type Stater interface {
Stat() (fs.FileInfo, error)
}
type Namer interface {
Name() string
}
func Name(in interface{}) string {
if namer, ok := in.(Namer); ok {
return namer.Name()
}
if stater, ok := in.(Stater); ok {
if info, err := stater.Stat(); err == nil {
return info.Name()
}
}
return ""
}