Skip to content

Commit cdb1c32

Browse files
committed
Updates
1 parent 44390bf commit cdb1c32

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

pkg/provider/meta/meta.go

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77
"os"
88
"reflect"
9+
"slices"
910
"strconv"
1011
"strings"
1112
"time"
@@ -28,7 +29,7 @@ type Meta struct {
2829
Fields []*Meta
2930

3031
// Private fields
31-
label []string
32+
label string
3233
parent *Meta
3334
}
3435

@@ -53,7 +54,6 @@ func New(v server.Plugin) (*Meta, error) {
5354
return nil, httpresponse.ErrInternalError.Withf("expected struct, got %T", v)
5455
} else {
5556
meta.Name = v.Name()
56-
meta.label = []string{}
5757
meta.Description = v.Description()
5858
meta.Type = rt
5959
}
@@ -73,26 +73,6 @@ func New(v server.Plugin) (*Meta, error) {
7373
return meta, nil
7474
}
7575

76-
// Return a new metadata object, with a label
77-
func (m *Meta) WithLabel(label string) *Meta {
78-
// Copy object
79-
meta := new(Meta)
80-
meta.Name = m.Name
81-
meta.Description = m.Description
82-
meta.Default = m.Default
83-
meta.Type = m.Type
84-
meta.Index = m.Index
85-
meta.Fields = m.Fields
86-
87-
// Make a copy of the label
88-
meta.label = make([]string, len(m.label))
89-
copy(meta.label, m.label)
90-
meta.label = append(meta.label, label)
91-
92-
// Return copy of meta
93-
return meta
94-
}
95-
9676
////////////////////////////////////////////////////////////////////////////////
9777
// STRINGIFY
9878

@@ -117,8 +97,8 @@ func (m *Meta) writeBlock(buf *bytes.Buffer, indent int) {
11797
buf.WriteString(prefix + "// " + m.Description + "\n")
11898
}
11999
buf.WriteString(prefix + m.Name)
120-
if label := m.Label(); label != "" {
121-
buf.WriteString(" " + strconv.Quote(label))
100+
if m.label != "" {
101+
buf.WriteString(" " + strconv.Quote(m.label))
122102
}
123103
buf.WriteString(" {\n")
124104
for _, field := range m.Fields {
@@ -134,7 +114,6 @@ func (m *Meta) writeBlock(buf *bytes.Buffer, indent int) {
134114
if field.Description != "" {
135115
buf.WriteString(" // " + field.Description)
136116
}
137-
buf.WriteString(prefix + "// " + m.Label() + "\n")
138117
if field.Default != "" {
139118
buf.WriteString(" (default: " + types.Quote(field.Default) + ")")
140119
}
@@ -150,8 +129,15 @@ func (m *Meta) writeBlock(buf *bytes.Buffer, indent int) {
150129
func (m *Meta) Label() string {
151130
var parts []string
152131
for meta := m; meta != nil; meta = meta.parent {
153-
parts = append(parts, meta.label...)
132+
parts = append(parts, meta.Name)
133+
if m.label != "" {
134+
parts = append(parts, meta.label)
135+
}
154136
}
137+
// Needs to be reversed
138+
slices.Reverse(parts)
139+
140+
// Return label parts
155141
return strings.Join(parts, labelSeparator)
156142
}
157143

@@ -194,7 +180,6 @@ func newMetaField(rf reflect.StructField, parent *Meta) (*Meta, error) {
194180
return nil, nil
195181
} else {
196182
meta.Name = name
197-
meta.label = []string{name}
198183
meta.parent = parent
199184
}
200185

pkg/provider/parser/block.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const (
1010
)
1111

1212
type Resource struct {
13-
Meta *meta.Meta
13+
Meta *meta.Meta
14+
Label string
1415
}
1516

1617
type Variable struct {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"log": {
3-
"debug": true
3+
"label": {
4+
"debug": true
5+
}
46
}
57
}

pkg/provider/parser/parser.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type parser struct {
2626
////////////////////////////////////////////////////////////////////////////////
2727
// LIFECYCLE
2828

29+
// Create a new parser with the given plugins
2930
func New(plugins ...server.Plugin) (*parser, error) {
3031
p := new(parser)
3132
p.meta = make(map[string]*meta.Meta, len(plugins))
@@ -51,7 +52,7 @@ func New(plugins ...server.Plugin) (*parser, error) {
5152
////////////////////////////////////////////////////////////////////////////////
5253
// PUBLIC METHODS
5354

54-
// Read a file and parse it
55+
// Read a configuration file and parse it, creating variables and resources
5556
func (p *parser) Parse(path string) error {
5657
ext := strings.ToLower(filepath.Ext(path))
5758
switch ext {
@@ -170,16 +171,16 @@ func (p *parser) jsonPluginsParse(meta *meta.Meta, root ast.Node) error {
170171
if !ok || label == "" {
171172
return httpresponse.ErrBadRequest.Withf("expected label, got %q", ident.Value())
172173
} else {
173-
meta = meta.WithLabel(label)
174+
label = meta.Label(label)
174175
}
175176

176-
if _, exists := p.resources[meta.Label()]; exists {
177-
return httpresponse.ErrBadRequest.Withf("duplicate resource: %q", meta.Label())
177+
if _, exists := p.resources[label]; exists {
178+
return httpresponse.ErrBadRequest.Withf("duplicate resource: %q", label)
178179
} else if len(ident.Children()) > 0 {
179-
if resource, err := jsonResourceParse(meta, ident.Children()[0]); err != nil {
180-
return httpresponse.ErrBadRequest.Withf("failed to parse %q: %s", meta.Label(), err)
180+
if resource, err := jsonResourceParse(meta, label, ident.Children()[0]); err != nil {
181+
return httpresponse.ErrBadRequest.Withf("failed to parse %q: %s", label, err)
181182
} else {
182-
p.resources[meta.Label()] = resource
183+
p.resources[label] = resource
183184
}
184185
}
185186
}
@@ -223,14 +224,15 @@ func jsonVariableParse(name string, root ast.Node) (*Variable, error) {
223224
return v, nil
224225
}
225226

226-
func jsonResourceParse(meta *meta.Meta, root ast.Node) (*Resource, error) {
227+
func jsonResourceParse(meta *meta.Meta, label string, root ast.Node) (*Resource, error) {
227228
if root.Type() != ast.Dict {
228229
return nil, httpresponse.ErrBadRequest.Withf("expected object, got %s", root.Type())
229230
}
230231

231232
// tree should contain key/value pairs and objects which may be nested
232233
r := new(Resource)
233234
r.Meta = meta
235+
r.Label = label
234236

235237
for _, ident := range root.Children() {
236238
if ident.Type() != ast.Ident {

0 commit comments

Comments
 (0)