Skip to content

Commit

Permalink
ast: Make the node field private
Browse files Browse the repository at this point in the history
This field does not have to be exposed and since we may replace it
with a statement identifier (e.g., a globally unique rule ID) in the
future, it will be better not to expose it.

Signed-off-by: Torin Sandall <[email protected]>
  • Loading branch information
tsandall committed Apr 27, 2021
1 parent fa073bb commit 1001d8b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions ast/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,24 +1222,24 @@ func newAnnotationSet() *annotationSet {
func (as *annotationSet) Add(a *Annotations) *Error {
switch a.Scope {
case annotationScopeRule:
rule := a.Node.(*Rule)
rule := a.node.(*Rule)
as.byRule[rule] = append(as.byRule[rule], a)
case annotationScopePackage:
pkg := a.Node.(*Package)
pkg := a.node.(*Package)
if exist, ok := as.byPackage[pkg]; ok {
return errAnnotationRedeclared(a, exist.Location)
}
as.byPackage[pkg] = a
case annotationScopeDocument:
rule := a.Node.(*Rule)
rule := a.node.(*Rule)
path := rule.Path()
x := as.byPath.Get(path)
if x != nil {
return errAnnotationRedeclared(a, x.Value.Location)
}
as.byPath.Insert(path, a)
case annotationScopeSubpackages:
pkg := a.Node.(*Package)
pkg := a.node.(*Package)
x := as.byPath.Get(pkg.Path)
if x != nil {
return errAnnotationRedeclared(a, x.Value.Location)
Expand Down
10 changes: 5 additions & 5 deletions ast/parser_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,14 @@ func parseModule(filename string, stmts []Statement, comments []*Comment) (*Modu
_, ok := stmt.(*Annotations)
if !ok {
if stmt.Loc().Row > a.Location.Row {
a.Node = stmt
a.node = stmt
break
}
}
}

if a.Scope == "" {
switch a.Node.(type) {
switch a.node.(type) {
case *Rule:
a.Scope = annotationScopeRule
case *Package:
Expand All @@ -688,12 +688,12 @@ func validateAnnotationScopeAttachment(a *Annotations) *Error {

switch a.Scope {
case annotationScopeRule, annotationScopeDocument:
if _, ok := a.Node.(*Rule); ok {
if _, ok := a.node.(*Rule); ok {
return nil
}
return newScopeAttachmentErr(a)
case annotationScopePackage, annotationScopeSubpackages:
if _, ok := a.Node.(*Package); ok {
if _, ok := a.node.(*Package); ok {
return nil
}
return newScopeAttachmentErr(a)
Expand All @@ -703,7 +703,7 @@ func validateAnnotationScopeAttachment(a *Annotations) *Error {
}

func newScopeAttachmentErr(a *Annotations) *Error {
return NewError(ParseErr, a.Loc(), "annotation scope '%v' cannot be applied to %v statement", a.Scope, TypeName(a.Node))
return NewError(ParseErr, a.Loc(), "annotation scope '%v' cannot be applied to %v statement", a.Scope, TypeName(a.node))
}

func setRuleModule(rule *Rule, module *Module) {
Expand Down
4 changes: 2 additions & 2 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2992,7 +2992,7 @@ public_servers_1[server] {
{Path: dataServers, Schema: schemaServers},
},
Scope: annotationScopeRule,
Node: MustParseRule(`public_servers[server] { server = servers[i] }`),
node: MustParseRule(`public_servers[server] { server = servers[i] }`),
},
&Annotations{
Schemas: []*SchemaAnnotation{
Expand All @@ -3001,7 +3001,7 @@ public_servers_1[server] {
{Path: dataPorts, Schema: schemaPorts},
},
Scope: annotationScopeRule,
Node: MustParseRule(`public_servers_1[server] { ports[k].networks[l] = networks[m].id; networks[m].public = true }`),
node: MustParseRule(`public_servers_1[server] { ports[k].networks[l] = networks[m].id; networks[m].public = true }`),
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions ast/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ type (

// Annotations represents metadata attached to other AST nodes such as rules.
Annotations struct {
Node Node `json:"-"`
Location *Location `json:"-"`
Scope string `json:"scope"`
Schemas []*SchemaAnnotation `json:"schemas,omitempty"`
node Node
}

// SchemaAnnotation contains a schema declaration for the document identified by the path.
Expand Down Expand Up @@ -281,7 +281,7 @@ func (s *Annotations) Copy(node Node) *Annotations {
for i := range cpy.Schemas {
cpy.Schemas[i] = s.Schemas[i].Copy()
}
cpy.Node = node
cpy.node = node
return &cpy
}

Expand Down Expand Up @@ -404,7 +404,7 @@ func (mod *Module) Copy() *Module {

cpy.Annotations = make([]*Annotations, len(mod.Annotations))
for i := range mod.Annotations {
cpy.Annotations[i] = mod.Annotations[i].Copy(nodes[mod.Annotations[i].Node])
cpy.Annotations[i] = mod.Annotations[i].Copy(nodes[mod.Annotations[i].node])
}

return &cpy
Expand All @@ -420,7 +420,7 @@ func (mod *Module) String() string {

byNode := map[Node][]*Annotations{}
for _, a := range mod.Annotations {
byNode[a.Node] = append(byNode[a.Node], a)
byNode[a.node] = append(byNode[a.node], a)
}

appendAnnotationStrings := func(buf []string, node Node) []string {
Expand Down

0 comments on commit 1001d8b

Please sign in to comment.