Skip to content

Commit

Permalink
Implement context based parsing
Browse files Browse the repository at this point in the history
To keep track of field&assemblies order.
  • Loading branch information
isimluk committed Oct 30, 2020
1 parent d40f256 commit e7c0e40
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 34 deletions.
50 changes: 50 additions & 0 deletions metaschema/parser/context_parsing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package parser

import (
"encoding/xml"
)

type parentElement interface {
RegisterChild(GoStructItem)
}

type contextParser []parentElement

var context contextParser

func (cp *contextParser) Push(p parentElement) {
*cp = append(*cp, p)
}

func (cp *contextParser) Pop() {
*cp = (*cp)[:len(*cp)-1]
}

func (cp *contextParser) RegisterChild(child GoStructItem) {
(*cp)[len(*cp)-1].RegisterChild(child)
}

func (m *Model) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type model Model
context.Push(m)
defer context.Pop()
return d.DecodeElement((*model)(m), &start)
}

func (m *Model) RegisterChild(child GoStructItem) {
m.sortedChilds = append(m.sortedChilds, child)
}

func (a *Assembly) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type assembly Assembly
err := d.DecodeElement((*assembly)(a), &start)
context.RegisterChild(a)
return err
}

func (f *Field) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type field Field
err := d.DecodeElement((*field)(f), &start)
context.RegisterChild(f)
return err
}
18 changes: 6 additions & 12 deletions metaschema/parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,15 @@ func (Metaschema *Metaschema) ContainsRootElement() bool {
}

type Model struct {
Assembly []Assembly `xml:"assembly"`
Field []Field `xml:"field"`
Choice []Choice `xml:"choice"`
Prose *struct{} `xml:"prose"`
Assembly []Assembly `xml:"assembly"`
Field []Field `xml:"field"`
Choice []Choice `xml:"choice"`
Prose *struct{} `xml:"prose"`
sortedChilds []GoStructItem
}

func (m *Model) GoStructItems() []GoStructItem {
res := make([]GoStructItem, len(m.Assembly)+len(m.Field))
for i, _ := range m.Field {
res[i] = &m.Field[i]
}
for i, _ := range m.Assembly {
res[i+len(m.Field)] = &m.Assembly[i]
}
return res
return m.sortedChilds
}

type Choice struct {
Expand Down
20 changes: 12 additions & 8 deletions metaschema/parser/metaschema_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func (metaschema *Metaschema) registerDependency(name string, dependency GoType)
}
}

func (metaschema *Metaschema) linkItems(list []GoStructItem) error {
for i, _ := range list {
err := list[i].compile(metaschema)
if err != nil {
return err
}
}
return nil
}
func (metaschema *Metaschema) linkAssemblies(list []Assembly) error {
for i, _ := range list {
a := &list[i]
Expand Down Expand Up @@ -63,20 +72,15 @@ func (metaschema *Metaschema) linkDefinitions() error {
if err = metaschema.linkFlags(da.Flags); err != nil {
return err
}
if err = metaschema.linkItems(da.Model.sortedChilds); err != nil {
return err
}
if err = metaschema.linkAssemblies(da.Model.Assembly); err != nil {
return err
}
if err = metaschema.linkFields(da.Model.Field); err != nil {
return err
}
for _, c := range da.Model.Choice {
if err = metaschema.linkAssemblies(c.Assembly); err != nil {
return err
}
if err = metaschema.linkFields(c.Field); err != nil {
return err
}
}
}

for _, df := range metaschema.DefineField {
Expand Down
13 changes: 0 additions & 13 deletions metaschema/templates/generated_models.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ type {{.GoTypeName}} struct {
// {{ .GoComment }}
{{.GoName}} {{.GoMemLayout}}{{.GoTypeNameMultiplexed}} `xml:"{{.XmlAnnotation}}" json:"{{.JsonName}},omitempty"`
{{- end}}

{{- range .Model.Choice}}
{{- range .Field}}
// {{ .GoComment }}
{{.GoName}} {{.GoMemLayout}}{{.GoTypeName}} `xml:"{{.XmlAnnotation}}" json:"{{.JsonName}},omitempty"`
{{- end}}

{{- range .Assembly}}
// {{ .GoComment }}
{{.GoName}} {{.GoMemLayout}}{{.GoTypeName}} `xml:"{{.XmlAnnotation}}" json:"{{.JsonName}},omitempty"`
{{- end}}

{{- end}}
{{end}}

}
Expand Down
2 changes: 1 addition & 1 deletion metaschema/templates/pkged.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e7c0e40

Please sign in to comment.