@@ -26,7 +26,6 @@ const (
26
26
type (
27
27
// Annotations represents metadata attached to other AST nodes such as rules.
28
28
Annotations struct {
29
- Location * Location `json:"-"`
30
29
Scope string `json:"scope"`
31
30
Title string `json:"title,omitempty"`
32
31
Entrypoint bool `json:"entrypoint,omitempty"`
@@ -36,8 +35,11 @@ type (
36
35
Authors []* AuthorAnnotation `json:"authors,omitempty"`
37
36
Schemas []* SchemaAnnotation `json:"schemas,omitempty"`
38
37
Custom map [string ]interface {} `json:"custom,omitempty"`
39
- node Node
40
- comments []* Comment
38
+ Location * Location `json:"location,omitempty"`
39
+
40
+ comments []* Comment
41
+ node Node
42
+ jsonOptions JSONOptions
41
43
}
42
44
43
45
// SchemaAnnotation contains a schema declaration for the document identified by the path.
@@ -70,10 +72,13 @@ type (
70
72
}
71
73
72
74
AnnotationsRef struct {
73
- Location * Location `json:"location"` // The location of the node the annotations are applied to
74
- Path Ref `json:"path"` // The path of the node the annotations are applied to
75
+ Path Ref `json:"path"` // The path of the node the annotations are applied to
75
76
Annotations * Annotations `json:"annotations,omitempty"`
76
- node Node // The node the annotations are applied to
77
+ Location * Location `json:"location,omitempty"` // The location of the node the annotations are applied to
78
+
79
+ jsonOptions JSONOptions
80
+
81
+ node Node // The node the annotations are applied to
77
82
}
78
83
79
84
AnnotationsRefSet []* AnnotationsRef
82
87
)
83
88
84
89
func (a * Annotations ) String () string {
85
- bs , _ := json . Marshal ( a )
90
+ bs , _ := a . MarshalJSON ( )
86
91
return string (bs )
87
92
}
88
93
@@ -175,6 +180,60 @@ func (a *Annotations) GetTargetPath() Ref {
175
180
}
176
181
}
177
182
183
+ func (a * Annotations ) setJSONOptions (opts JSONOptions ) {
184
+ a .jsonOptions = opts
185
+ }
186
+
187
+ func (a * Annotations ) MarshalJSON () ([]byte , error ) {
188
+ if a == nil {
189
+ return []byte (`{"scope":""}` ), nil
190
+ }
191
+
192
+ data := map [string ]interface {}{
193
+ "scope" : a .Scope ,
194
+ }
195
+
196
+ if a .Title != "" {
197
+ data ["title" ] = a .Title
198
+ }
199
+
200
+ if a .Description != "" {
201
+ data ["description" ] = a .Description
202
+ }
203
+
204
+ if a .Entrypoint {
205
+ data ["entrypoint" ] = a .Entrypoint
206
+ }
207
+
208
+ if len (a .Organizations ) > 0 {
209
+ data ["organizations" ] = a .Organizations
210
+ }
211
+
212
+ if len (a .RelatedResources ) > 0 {
213
+ data ["related_resources" ] = a .RelatedResources
214
+ }
215
+
216
+ if len (a .Authors ) > 0 {
217
+ data ["authors" ] = a .Authors
218
+ }
219
+
220
+ if len (a .Schemas ) > 0 {
221
+ data ["schemas" ] = a .Schemas
222
+ }
223
+
224
+ if len (a .Custom ) > 0 {
225
+ data ["custom" ] = a .Custom
226
+ }
227
+
228
+ if a .jsonOptions .MarshalOptions .IncludeLocation .Annotations {
229
+ if a .Location != nil {
230
+ data ["location" ] = a .Location
231
+ }
232
+ }
233
+
234
+ return json .Marshal (data )
235
+ }
236
+
178
237
func NewAnnotationsRef (a * Annotations ) * AnnotationsRef {
179
238
var loc * Location
180
239
if a .node != nil {
@@ -209,6 +268,24 @@ func (ar *AnnotationsRef) GetRule() *Rule {
209
268
}
210
269
}
211
270
271
+ func (ar * AnnotationsRef ) MarshalJSON () ([]byte , error ) {
272
+ data := map [string ]interface {}{
273
+ "path" : ar .Path ,
274
+ }
275
+
276
+ if ar .Annotations != nil {
277
+ data ["annotations" ] = ar .Annotations
278
+ }
279
+
280
+ if ar .jsonOptions .MarshalOptions .IncludeLocation .AnnotationsRef {
281
+ if ar .Location != nil {
282
+ data ["location" ] = ar .Location
283
+ }
284
+ }
285
+
286
+ return json .Marshal (data )
287
+ }
288
+
212
289
func scopeCompare (s1 , s2 string ) int {
213
290
214
291
o1 := scopeOrder (s1 )
0 commit comments