-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add tests for the syntax package - Replace the term 'list' with 'array' in most occurrences
- Loading branch information
Showing
33 changed files
with
2,041 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package encoding | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/pulumi/esc/syntax" | ||
) | ||
|
||
type NodeProperty struct { | ||
Key Node `json:"key"` | ||
Value Node `json:"value"` | ||
} | ||
|
||
type Node struct { | ||
Literal any `json:"literal,omitempty"` | ||
Array []Node `json:"array,omitempty"` | ||
Object []NodeProperty `json:"object,omitempty"` | ||
Range *hcl.Range `json:"range,omitempty"` | ||
} | ||
|
||
func NewNode(n syntax.Node) Node { | ||
switch n := n.(type) { | ||
case nil: | ||
return Node{} | ||
case *syntax.NullNode: | ||
return Node{Range: n.Syntax().Range()} | ||
case *syntax.BooleanNode: | ||
return Node{Literal: n.Value(), Range: n.Syntax().Range()} | ||
case *syntax.NumberNode: | ||
return Node{Literal: n.Value(), Range: n.Syntax().Range()} | ||
case *syntax.StringNode: | ||
return Node{Literal: n.Value(), Range: n.Syntax().Range()} | ||
case *syntax.ArrayNode: | ||
arr := make([]Node, n.Len()) | ||
for i := range arr { | ||
arr[i] = NewNode(n.Index(i)) | ||
} | ||
return Node{Array: arr, Range: n.Syntax().Range()} | ||
case *syntax.ObjectNode: | ||
obj := make([]NodeProperty, n.Len()) | ||
for i := 0; i < n.Len(); i++ { | ||
prop := n.Index(i) | ||
obj[i] = NodeProperty{Key: NewNode(prop.Key), Value: NewNode(prop.Value)} | ||
} | ||
return Node{Object: obj, Range: n.Syntax().Range()} | ||
default: | ||
panic(fmt.Errorf("unexpected node of type %T", n)) | ||
} | ||
} |
Oops, something went wrong.