mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
db035b09fc
Makes OPA build and pass its tests on Go 1.27, while keeping Go 1.25 and 1.26 working. JSON output is unchanged on every supported version. Go 1.27 json package honours `encoding.TextAppender`. Many v1 ast types implement AppendText to build their Rego string cheaply, so on 1.27 they would have marshalled as Rego text. Files built only with 1.27 now implement MarshalJSONTo. Library users should keep using `json.Marshal` etc. The MarshalJSONTo methods are implementation details, are absent from 1.25 and 1.26 builds, and may change. --------- Signed-off-by: Anders Eknert <anders.eknert@apple.com> Signed-off-by: Charlie Egan <charlie_egan@apple.com> Co-authored-by: Charlie Egan <charlie_egan@apple.com>
125 lines
2.4 KiB
Go
125 lines
2.4 KiB
Go
//go:build !go1.27
|
|
|
|
package ast
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
astJSON "github.com/open-policy-agent/opa/v1/ast/json"
|
|
)
|
|
|
|
func (a *Annotations) MarshalJSON() ([]byte, error) {
|
|
if a == nil {
|
|
return []byte(`{"scope":""}`), nil
|
|
}
|
|
|
|
data := map[string]any{
|
|
"scope": a.Scope,
|
|
}
|
|
|
|
if a.Title != "" {
|
|
data["title"] = a.Title
|
|
}
|
|
|
|
if a.Description != "" {
|
|
data["description"] = a.Description
|
|
}
|
|
|
|
if a.Entrypoint {
|
|
data["entrypoint"] = a.Entrypoint
|
|
}
|
|
|
|
if len(a.Organizations) > 0 {
|
|
data["organizations"] = a.Organizations
|
|
}
|
|
|
|
if len(a.RelatedResources) > 0 {
|
|
data["related_resources"] = a.RelatedResources
|
|
}
|
|
|
|
if len(a.Authors) > 0 {
|
|
data["authors"] = a.Authors
|
|
}
|
|
|
|
if len(a.Schemas) > 0 {
|
|
data["schemas"] = a.Schemas
|
|
}
|
|
|
|
if a.Compile != nil {
|
|
data["compile"] = a.Compile
|
|
}
|
|
|
|
if len(a.Custom) > 0 {
|
|
data["custom"] = a.Custom
|
|
}
|
|
|
|
if len(a.Labels) > 0 {
|
|
data["labels"] = a.Labels
|
|
}
|
|
|
|
if astJSON.GetOptions().MarshalOptions.IncludeLocation.Annotations {
|
|
if a.Location != nil {
|
|
data["location"] = a.Location
|
|
}
|
|
}
|
|
|
|
return json.Marshal(data)
|
|
}
|
|
|
|
func (rr *RelatedResourceAnnotation) MarshalJSON() ([]byte, error) {
|
|
d := map[string]any{
|
|
"ref": rr.Ref.String(),
|
|
}
|
|
|
|
if len(rr.Description) > 0 {
|
|
d["description"] = rr.Description
|
|
}
|
|
|
|
return json.Marshal(d)
|
|
}
|
|
|
|
func (ar *AnnotationsRef) MarshalJSON() ([]byte, error) {
|
|
data := map[string]any{
|
|
"path": ar.Path,
|
|
}
|
|
|
|
if ar.Annotations != nil {
|
|
data["annotations"] = ar.Annotations
|
|
}
|
|
|
|
if astJSON.GetOptions().MarshalOptions.IncludeLocation.AnnotationsRef {
|
|
if ar.Location != nil {
|
|
data["location"] = ar.Location
|
|
}
|
|
}
|
|
|
|
return json.Marshal(data)
|
|
}
|
|
|
|
// schemaAnnotationJSON mirrors SchemaAnnotation's JSON tags, with location-free
|
|
// path terms.
|
|
type schemaAnnotationJSON struct {
|
|
Path []termJSON `json:"path"`
|
|
Schema Ref `json:"schema,omitempty"`
|
|
Definition *any `json:"definition,omitempty"`
|
|
}
|
|
|
|
func (s *SchemaAnnotation) MarshalJSON() ([]byte, error) {
|
|
d := schemaAnnotationJSON{
|
|
Schema: s.Schema,
|
|
Definition: s.Definition,
|
|
}
|
|
|
|
if s.Path != nil {
|
|
d.Path = make([]termJSON, len(s.Path))
|
|
for i, t := range s.Path {
|
|
// The location is omitted: path terms are parsed on their own from
|
|
// the annotation's YAML key, so their locations are offsets into that
|
|
// key (always row 1) rather than positions in the module.
|
|
d.Path[i] = termJSON{Type: ValueName(t.Value), Value: t.Value}
|
|
}
|
|
}
|
|
|
|
return json.Marshal(d)
|
|
}
|