Skip to content

Commit

Permalink
refactor support for recursive schema to address issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dhubler committed Oct 9, 2023
1 parent 84b345f commit c384ff8
Show file tree
Hide file tree
Showing 31 changed files with 1,079 additions and 475 deletions.
2 changes: 1 addition & 1 deletion meta/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (b *Builder) Units(o interface{}, units string) {

func (b *Builder) AddExtension(o interface{}, keyword string, ext *Extension) {
ext.keyword = keyword
m, valid := o.(HasExtensions)
m, valid := o.(hasAddExtension)
if !valid {
b.setErr(fmt.Errorf("%T does not support extensions", o))
} else {
Expand Down
5 changes: 4 additions & 1 deletion meta/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func Compile(root *Module) error {
c := &compiler{
root: root,
pool: make(map[HasDefinitions]struct{}),
}
// loads submodules, imports and then resolve uses with groupings
if err := resolve(root); err != nil {
Expand All @@ -23,6 +24,7 @@ func Compile(root *Module) error {

type compiler struct {
root *Module
pool map[HasDefinitions]struct{}
}

func (c *compiler) module(y *Module) error {
Expand Down Expand Up @@ -130,7 +132,8 @@ func (c *compiler) compile(o interface{}) error {
}

if x, ok := o.(HasDataDefinitions); ok {
if !x.IsRecursive() {
if _, alreadyCompiled := c.pool[x]; !alreadyCompiled {
c.pool[x] = struct{}{}
for _, y := range x.DataDefinitions() {
if err := c.compile(y); err != nil {
return err
Expand Down
76 changes: 73 additions & 3 deletions meta/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (y *Module) getOriginalParent() Definition {
return nil
}

func (y *Module) setParent(p Meta) {
y.parent = p.(*Module)
}

func (y *Module) RevisionHistory() []*Revision {
return y.rev
}
Expand Down Expand Up @@ -194,6 +198,7 @@ type Choice struct {
}

func (y *Choice) addCase(c *ChoiceCase) error {
c.setParent(y)
if _, exists := y.cases[c.Ident()]; exists {
return fmt.Errorf("conflict adding add %s to %s. ", c.Ident(), y.Ident())
}
Expand All @@ -215,6 +220,10 @@ func (y *Choice) CaseIdents() []string {
return idents
}

func (y *Choice) setParent(p Meta) {
y.parent = p
}

type ChoiceCase struct {
ident string
desc string
Expand All @@ -228,7 +237,10 @@ type ChoiceCase struct {
dataDefsIndex map[string]Definition
ifs []*IfFeature
extensions []*Extension
recursive bool
}

func (y *ChoiceCase) setParent(p Meta) {
y.parent = p
}

// Revision is like a version for a module. Format is YYYY-MM-DD and should match
Expand Down Expand Up @@ -261,7 +273,10 @@ type Container struct {
ifs []*IfFeature
musts []*Must
extensions []*Extension
recursive bool
}

func (y *Container) setParent(p Meta) {
y.parent = p
}

type OrderedBy int
Expand Down Expand Up @@ -297,7 +312,10 @@ type List struct {
musts []*Must
extensions []*Extension
unique [][]string
recursive bool
}

func (y *List) setParent(p Meta) {
y.parent = p
}

func (y *List) KeyMeta() (keyMeta []Leafable) {
Expand All @@ -322,6 +340,10 @@ type Leaf struct {
extensions []*Extension
}

func (y *Leaf) setParent(p Meta) {
y.parent = p
}

type LeafList struct {
ident string
parent Meta
Expand All @@ -344,6 +366,10 @@ type LeafList struct {
extensions []*Extension
}

func (y *LeafList) setParent(p Meta) {
y.parent = p
}

var anyType = newType("any")

type Any struct {
Expand All @@ -361,6 +387,10 @@ type Any struct {
extensions []*Extension
}

func (y *Any) setParent(p Meta) {
y.parent = p
}

func (y *Any) HasDefault() bool {
return false
}
Expand Down Expand Up @@ -430,6 +460,10 @@ type Grouping struct {
extensions []*Extension
}

func (y *Grouping) setParent(p Meta) {
y.parent = p
}

type Uses struct {
ident string
desc string
Expand All @@ -444,6 +478,10 @@ type Uses struct {
extensions []*Extension
}

func (y *Uses) setParent(p Meta) {
y.parent = p
}

func (y *Uses) Refinements() []*Refine {
return y.refines
}
Expand All @@ -465,6 +503,10 @@ type Refine struct {
extensions []*Extension
}

func (y *Refine) setParent(p Meta) {
y.parent = p.(*Uses)
}

func (y *Refine) splitIdent() (string, string) {
slash := strings.IndexRune(y.ident, '/')
if slash < 0 {
Expand All @@ -487,6 +529,10 @@ type RpcInput struct {
extensions []*Extension
}

func (y *RpcInput) setParent(p Meta) {
y.parent = p
}

func (y *RpcInput) Ident() string {
return "input"
}
Expand All @@ -505,6 +551,10 @@ type RpcOutput struct {
extensions []*Extension
}

func (y *RpcOutput) setParent(p Meta) {
y.parent = p
}

func (y *RpcOutput) Ident() string {
return "output"
}
Expand All @@ -524,6 +574,10 @@ type Rpc struct {
extensions []*Extension
}

func (y *Rpc) setParent(p Meta) {
y.parent = p
}

func (y *Rpc) Input() *RpcInput {
return y.input
}
Expand All @@ -547,6 +601,10 @@ type Notification struct {
extensions []*Extension
}

func (y *Notification) setParent(p Meta) {
y.parent = p
}

type Typedef struct {
ident string
parent Meta
Expand All @@ -559,6 +617,10 @@ type Typedef struct {
extensions []*Extension
}

func (y *Typedef) setParent(p Meta) {
y.parent = p
}

type Augment struct {
ident string
parent Meta
Expand All @@ -574,6 +636,10 @@ type Augment struct {
extensions []*Extension
}

func (y *Augment) setParent(p Meta) {
y.parent = p
}

func (a *Augment) addCase(c *ChoiceCase) error {
return a.addDataDefinition(c)
}
Expand Down Expand Up @@ -637,6 +703,10 @@ type Deviation struct {
extensions []*Extension
}

func (y *Deviation) setParent(p Meta) {
y.parent = p
}

type Type struct {
ident string
desc string
Expand Down
Loading

0 comments on commit c384ff8

Please sign in to comment.