Skip to content

Commit

Permalink
better json output and initialize document on parse
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Feb 15, 2024
1 parent 337f0b1 commit f8f9832
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/ast/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ import (
)

type Assignment struct {
Comments []*Comment `json:"comments"` // Comments attached to the assignment (e.g. doc block before it)
Complete bool `json:"complete"` // The key/value had no value/content after the "=" sign
Enabled bool `json:"enabled"` // The assignment was enabled out (#KEY=VALUE)
Group *Group `json:"-"` // The (optional) group this assignment belongs to
Interpolated string `json:"value"` // Value of the key (after interpolation)
Interpolated string `json:"interpolated"` // Value of the key (after interpolation)
Literal string `json:"literal"` // Value of the key (right hand side of the "=" sign)
Name string `json:"key"` // Name of the key (left hand side of the "=" sign)
Position Position `json:"position"` // Information about position of the assignment in the file
Quote token.Quote `json:"quote"` // The style of quotes used for the assignment
Position Position `json:"position"` // Information about position of the assignment in the file
Comments []*Comment `json:"comments"` // Comments attached to the assignment (e.g. doc block before it)
Dependencies map[string]template.Variable `json:"dependencies"` // Assignments that this assignment depends on
Dependents map[string]*Assignment `json:"dependents"` // Assignments dependents on this assignment
}

func (a *Assignment) statementNode() {}

func (a *Assignment) Initialize() {
a.Dependencies = template.ExtractVariables(a.Literal, nil)
a.Dependents = make(map[string]*Assignment)
if dependencies := template.ExtractVariables(a.Literal, nil); len(dependencies) > 0 {
a.Dependencies = dependencies
}
}

func (a *Assignment) Is(other Statement) bool {
Expand Down
4 changes: 4 additions & 0 deletions pkg/ast/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ func (document *Document) Initialize() {
// Add current assignment as dependent on its own dependencies
for _, dependency := range assignment.Dependencies {
if x := document.Get(dependency.Name); x != nil {
if x.Dependents == nil {
x.Dependents = make(map[string]*Assignment)
}

x.Dependents[assignment.Name] = assignment
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func (p *Parser) Parse() (*ast.Document, error) {
}
}

// Make sure to initialize the document so dependencies and such are
// computed immediately
document.Initialize()

return document, nil
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/token/quote.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package token

import "encoding/json"

type Quote uint

const (
Expand Down Expand Up @@ -47,6 +49,22 @@ func (qt Quote) String() string {
return str
}

func (qt Quote) MarshalJSON() ([]byte, error) {
switch qt {
case NoQuotes:
return json.Marshal(nil)

case SingleQuotes:
return json.Marshal("single")

case DoubleQuotes:
return json.Marshal("double")

default:
panic("unknown quote style")
}
}

func QuoteFromString(in string) Quote {
switch in {
case "\"", "double":
Expand Down

0 comments on commit f8f9832

Please sign in to comment.