Skip to content

Commit

Permalink
fix: preserve jsonOptions in NewAnnotationsRef
Browse files Browse the repository at this point in the history
Makes sure that `jsonOptions` are preserved in NewAnnotationsRef.

Signed-off-by: Zoran Regvart <zoran@regvart.com>
  • Loading branch information
zregvart committed Mar 14, 2023
1 parent 97408d1 commit d1fdc0d
Show file tree
Hide file tree
Showing 2 changed files with 316 additions and 0 deletions.
1 change: 1 addition & 0 deletions ast/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func NewAnnotationsRef(a *Annotations) *AnnotationsRef {
Path: a.GetTargetPath(),
Annotations: a,
node: a.node,
jsonOptions: a.jsonOptions,
}
}

Expand Down
315 changes: 315 additions & 0 deletions ast/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

// Test of example code in docs/content/annotations.md
Expand Down Expand Up @@ -1095,6 +1097,319 @@ func TestAnnotations_toObject(t *testing.T) {
}
}

func TestNewAnnotationsSet_WithOptions(t *testing.T) {
tests := []struct {
note string
module string
expected []string
options ParserOptions
}{
{
note: "all JSON marshaller options set to true",
module: `# METADATA
# title: pkg
# description: pkg
# organizations:
# - pkg
# related_resources:
# - https://pkg
# authors:
# - pkg
# schemas:
# - input.foo: {"type": "boolean"}
# custom:
# pkg: pkg
package test
# METADATA
# scope: document
# title: doc
# description: doc
# organizations:
# - doc
# related_resources:
# - https://doc
# authors:
# - doc
# schemas:
# - input.bar: {"type": "integer"}
# custom:
# doc: doc
# METADATA
# title: rule
# description: rule
# organizations:
# - rule
# related_resources:
# - https://rule
# authors:
# - rule
# schemas:
# - input.baz: {"type": "string"}
# custom:
# rule: rule
p = 1`,
options: ParserOptions{
ProcessAnnotation: true,
JSONOptions: &JSONOptions{
MarshalOptions: JSONMarshalOptions{
IncludeLocation: NodeToggle{
Term: true,
Package: true,
Comment: true,
Import: true,
Rule: true,
Head: true,
Expr: true,
SomeDecl: true,
Every: true,
With: true,
Annotations: true,
AnnotationsRef: true,
},
},
},
},
expected: []string{
`{
"annotations": {
"authors": [
{
"name": "pkg"
}
],
"custom": {
"pkg": "pkg"
},
"description": "pkg",
"location": {
"col": 1,
"file": "",
"row": 1
},
"organizations": [
"pkg"
],
"related_resources": [
{
"ref": "https://pkg"
}
],
"schemas": [
{
"definition": {
"type": "boolean"
},
"path": [
{
"type": "var",
"value": "input"
},
{
"type": "string",
"value": "foo"
}
]
}
],
"scope": "package",
"title": "pkg"
},
"location": {
"col": 1,
"file": "",
"row": 14
},
"path": [
{
"location": {
"col": 9,
"file": "",
"row": 14
},
"type": "var",
"value": "data"
},
{
"location": {
"col": 9,
"file": "",
"row": 14
},
"type": "string",
"value": "test"
}
]
}`,
`{
"annotations": {
"authors": [
{
"name": "doc"
}
],
"custom": {
"doc": "doc"
},
"description": "doc",
"location": {
"col": 1,
"file": "",
"row": 16
},
"organizations": [
"doc"
],
"related_resources": [
{
"ref": "https://doc"
}
],
"schemas": [
{
"definition": {
"type": "integer"
},
"path": [
{
"type": "var",
"value": "input"
},
{
"type": "string",
"value": "bar"
}
]
}
],
"scope": "document",
"title": "doc"
},
"location": {
"col": 1,
"file": "",
"row": 44
},
"path": [
{
"location": {
"col": 9,
"file": "",
"row": 14
},
"type": "var",
"value": "data"
},
{
"location": {
"col": 9,
"file": "",
"row": 14
},
"type": "string",
"value": "test"
},
{
"type": "string",
"value": "p"
}
]
}`,
`{
"annotations": {
"authors": [
{
"name": "rule"
}
],
"custom": {
"rule": "rule"
},
"description": "rule",
"location": {
"col": 1,
"file": "",
"row": 31
},
"organizations": [
"rule"
],
"related_resources": [
{
"ref": "https://rule"
}
],
"schemas": [
{
"definition": {
"type": "string"
},
"path": [
{
"type": "var",
"value": "input"
},
{
"type": "string",
"value": "baz"
}
]
}
],
"scope": "rule",
"title": "rule"
},
"location": {
"col": 1,
"file": "",
"row": 44
},
"path": [
{
"location": {
"col": 9,
"file": "",
"row": 14
},
"type": "var",
"value": "data"
},
{
"location": {
"col": 9,
"file": "",
"row": 14
},
"type": "string",
"value": "test"
},
{
"type": "string",
"value": "p"
}
]
}`,
},
},
}

for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
module := MustParseModuleWithOpts(tc.module, tc.options)

for i, a := range module.Annotations {
ref := NewAnnotationsRef(a)

asJSON := toJSON(ref)

assert.JSONEq(t, tc.expected[i], asJSON, "serialized AnnotationsRef differs, expected:\n%s\n got\n%s\n", tc.expected[i], asJSON)
}

})
}
}

func toJSON(v interface{}) string {
b, _ := json.MarshalIndent(v, "", " ")
return string(b)
Expand Down

0 comments on commit d1fdc0d

Please sign in to comment.