json: allow "//" keys to be used as comments in bodies
Since the native syntax requires that attribute names and block types be identifiers, there is no way for "//" to be a valid symbol in a body. Therefore we can special-case it here as an ignored property name that users can then use for comments. This special handling intentionally does not apply to objects representing zcl object expressions, since it _is_ valid to have non-identifier keys there and so "//" may be a legitimate object key for some applications.
This commit is contained in:
parent
15e3d80e6c
commit
e7d33665d0
@ -52,6 +52,12 @@ func (b *body) Content(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Diagnostic
|
|||||||
}
|
}
|
||||||
|
|
||||||
for k, attr := range b.obj.Attrs {
|
for k, attr := range b.obj.Attrs {
|
||||||
|
if k == "//" {
|
||||||
|
// Ignore "//" keys in objects representing bodies, to allow
|
||||||
|
// their use as comments.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := hiddenAttrs[k]; !ok {
|
if _, ok := hiddenAttrs[k]; !ok {
|
||||||
var fixItHint string
|
var fixItHint string
|
||||||
suggestion := nameSuggestion(k, nameSuggestions)
|
suggestion := nameSuggestion(k, nameSuggestions)
|
||||||
@ -140,6 +146,12 @@ func (b *body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod
|
|||||||
func (b *body) JustAttributes() (zcl.Attributes, zcl.Diagnostics) {
|
func (b *body) JustAttributes() (zcl.Attributes, zcl.Diagnostics) {
|
||||||
attrs := make(map[string]*zcl.Attribute)
|
attrs := make(map[string]*zcl.Attribute)
|
||||||
for name, jsonAttr := range b.obj.Attrs {
|
for name, jsonAttr := range b.obj.Attrs {
|
||||||
|
if name == "//" {
|
||||||
|
// Ignore "//" keys in objects representing bodies, to allow
|
||||||
|
// their use as comments.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if _, hidden := b.hiddenAttrs[name]; hidden {
|
if _, hidden := b.hiddenAttrs[name]; hidden {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,19 @@ func TestBodyPartialContent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
0,
|
0,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
`{"//": "comment that should be ignored"}`,
|
||||||
|
&zcl.BodySchema{},
|
||||||
|
&zcl.BodyContent{
|
||||||
|
Attributes: map[string]*zcl.Attribute{},
|
||||||
|
MissingItemRange: zcl.Range{
|
||||||
|
Filename: "test.json",
|
||||||
|
Start: zcl.Pos{Line: 1, Column: 40, Byte: 39},
|
||||||
|
End: zcl.Pos{Line: 1, Column: 41, Byte: 40},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
0,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
`{"name":"Ermintrude"}`,
|
`{"name":"Ermintrude"}`,
|
||||||
&zcl.BodySchema{
|
&zcl.BodySchema{
|
||||||
@ -618,6 +631,11 @@ func TestBodyContent(t *testing.T) {
|
|||||||
&zcl.BodySchema{},
|
&zcl.BodySchema{},
|
||||||
1,
|
1,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
`{"//": "comment that should be ignored"}`,
|
||||||
|
&zcl.BodySchema{},
|
||||||
|
0,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
`{"unknow": true}`,
|
`{"unknow": true}`,
|
||||||
&zcl.BodySchema{
|
&zcl.BodySchema{
|
||||||
@ -698,6 +716,10 @@ func TestJustAttributes(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
`{"//": "comment that should be ignored"}`,
|
||||||
|
map[string]*zcl.Attribute{},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
Loading…
Reference in New Issue
Block a user