-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
186 lines (146 loc) · 3.7 KB
/
reader.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
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
package bigpack
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
stdpath "path"
"path/filepath"
"strings"
"github.com/rjeczalik/bigstruct/big"
"github.com/rjeczalik/bigstruct/big/codec"
"github.com/rjeczalik/bigstruct/bigpack/pak"
"github.com/rjeczalik/bigstruct/internal/types"
"github.com/rjeczalik/bigstruct/storage/model"
"gopkg.in/yaml.v3"
)
var globalReader = &Reader{}
type Reader struct {
Codec big.Codec // or codec.Default if nil
}
func (r *Reader) Read(ctx context.Context, fs pak.FS) (*pak.Pak, error) {
const (
def = "bigpack.yaml"
index = ".bigpack"
sep = string(os.PathSeparator)
)
var (
pk pak.Pak
err error
ok bool
)
err = fs(func(path string, rc io.ReadCloser) error {
if path != def {
return nil // skip
}
ok = true
p, err := ioutil.ReadAll(rc)
if err != nil {
return fmt.Errorf("error reading %q: %w", path, err)
}
if err := yaml.Unmarshal(p, &pk); err != nil {
return fmt.Errorf("error parsing %q: %w", path, err)
}
return nil
})
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("did not find definition file %q", def)
}
var (
mschema = make(map[string]big.Fields)
mvalue = make(map[string]big.Fields)
)
err = fs(func(path string, rc io.ReadCloser) error {
if path == def || !(strings.HasPrefix(path, "schema"+sep) || strings.HasPrefix(path, "value"+sep)) {
return nil // skip
}
// path ~ <type>/<overlay>[/<property>]/<key>
parts := strings.SplitN(path, sep, 4)
out := mschema
if parts[0] == "value" {
out = mvalue
}
if len(parts) < 3 {
return fmt.Errorf("file %q has invalid layout", path)
}
o := pk.Overlays.ByName(parts[1])
if o == nil {
return fmt.Errorf("overlay %q not found for key: %q", parts[1], path)
}
var key string
if !o.Meta().NoProperty {
if err := o.SetProperty(parts[2]); err != nil {
return fmt.Errorf(
"error setting %q property for %q overlay and %q key: %w",
parts[2], o.Name, path, err,
)
}
key = stdpath.Join("/", filepath.ToSlash(parts[3]))
} else if len(parts) == 4 {
key = stdpath.Join("/", parts[2], filepath.ToSlash(parts[3]))
} else {
key = stdpath.Join("/", filepath.ToSlash(parts[2]))
}
p, err := ioutil.ReadAll(rc)
if err != nil {
return fmt.Errorf("error reading %q key from %q overlay: %w", key, o.Ref(), err)
}
if stdpath.Base(key) == index {
var idx map[string]string
if err := types.YAML(p).Unmarshal(&idx); err != nil {
return fmt.Errorf("error parsing %q index from %q overlay: %w", key, o.Ref(), err)
}
for k, typ := range idx {
out[o.Ref()] = append(out[o.Ref()], big.Field{
Key: stdpath.Join(stdpath.Dir(key), k),
Type: typ,
})
}
} else {
out[o.Ref()] = append(out[o.Ref()], big.Field{
Key: key,
Value: string(p),
})
}
return nil
})
if err != nil {
return nil, err
}
for ref, f := range mschema {
s := f.Struct()
if err := s.Decode(ctx, r.codec()); err != nil {
return nil, fmt.Errorf("error decoding schema for %q overlay: %w", ref, err)
}
var (
o = pk.Overlays.ByRef(ref)
schema = model.MakeSchemas(o, s.Fields())
)
pk.Schemas = append(pk.Schemas, schema...)
}
for ref, f := range mvalue {
s := f.Struct()
if err := s.Decode(ctx, r.codec()); err != nil {
return nil, fmt.Errorf("error decoding values for %q overlay: %w", ref, err)
}
var (
o = pk.Overlays.ByRef(ref)
value = model.MakeValues(o, s.Fields())
)
pk.Values = append(pk.Values, value...)
}
return &pk, nil
}
func (r *Reader) codec() big.Codec {
if r.Codec != nil {
return r.Codec
}
return codec.Default
}
func Read(ctx context.Context, fs pak.FS) (*pak.Pak, error) {
return globalReader.Read(ctx, fs)
}