Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
Fill in metaschema model tree with references to the particular eleme…
Browse files Browse the repository at this point in the history
…nt definitions

This will surely end-up less tedious than getting Def in template.
  • Loading branch information
isimluk committed Dec 18, 2019
1 parent 2a60214 commit ddc3075
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions metaschema/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func decode(r io.Reader) (*metaschema.Metaschema, error) {

meta.ImportedMetaschema = append(meta.ImportedMetaschema, *importedMeta)
}
meta.LinkDefinitions()

return &meta, nil
}
40 changes: 40 additions & 0 deletions metaschema/metaschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,42 @@ type Metaschema struct {
ImportedMetaschema []Metaschema
}

func (metaschema *Metaschema) LinkDefinitions() error {
var err error
for _, da := range metaschema.DefineAssembly {
for i, f := range da.Flags {
if f.Ref != "" {
f.Def, err = metaschema.GetDefineFlag(f.Ref)
if err != nil {
return err
}
da.Flags[i] = f
}

}
for i, a := range da.Model.Assembly {
if a.Ref != "" {
a.Def, err = metaschema.GetDefineAssembly(a.Ref)
if err != nil {
return err
}
da.Model.Assembly[i] = a
}
}
for i, f := range da.Model.Field {
if f.Ref != "" {
f.Def, err = metaschema.GetDefineField(f.Ref)
if err != nil {
return err
}
da.Model.Field[i] = f

}
}
}
return nil
}

func (metaschema *Metaschema) GetDefineField(name string) (*DefineField, error) {
for _, v := range metaschema.DefineField {
if name == v.Name {
Expand Down Expand Up @@ -164,6 +200,7 @@ type Assembly struct {
Remarks *Remarks `xml:"remarks"`
Ref string `xml:"ref,attr"`
GroupAs *GroupAs `xml:"group-as"`
Def *DefineAssembly
}

type Field struct {
Expand All @@ -174,6 +211,7 @@ type Field struct {
Remarks *Remarks `xml:"remarks"`
Ref string `xml:"ref,attr"`
GroupAs *GroupAs `xml:"group-as"`
Def *DefineField
}

type Flag struct {
Expand All @@ -184,6 +222,8 @@ type Flag struct {
Description string `xml:"description"`
Remarks *Remarks `xml:"remarks"`
Values []Value `xml:"value"`
Ref string `xml:"ref,attr"`
Def *DefineFlag
}

type Choice struct {
Expand Down
8 changes: 4 additions & 4 deletions metaschema/types.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type {{toCamel .Name}} struct {
{{- end}}
{{if .Model}}
{{ range .Model.Field}}
{{- if .Ref }}
{{- if .Def }}
{{- $groupAs := .GroupAs }}
{{- with $m.GetDefineField .Ref }}
{{- with .Def }}
{{ toCamel .Name}} {{if $groupAs}}[]{{else}}{{if .RequiresPointer}}*{{end}}{{end}}{{packageImport .Name $m}}{{toCamel .Name}} `xml:"{{ if $groupAs}}{{ $groupAs.Name }}{{else}}{{ .Name }}{{end}},omitempty" json:"{{toLowerCamel .Name}},omitempty"`
{{- end }}
{{- else }}
Expand All @@ -43,9 +43,9 @@ type {{toCamel .Name}} struct {
{{- end}}

{{- range .Model.Assembly}}
{{- if .Ref}}
{{- if .Def}}
{{- $groupAs := .GroupAs }}
{{- with $m.GetDefineAssembly .Ref }}
{{- with .Def }}
{{ toCamel .Name}} {{if $groupAs}}[]{{else}}*{{end}}{{packageImport .Name $m}}{{toCamel .Name}} `xml:"{{ if $groupAs }}{{$groupAs.Name}}{{else}}{{ .Name }}{{end}},omitempty" json:"{{toLowerCamel .Name}},omitempty"`
{{- end}}
{{- else }}
Expand Down

0 comments on commit ddc3075

Please sign in to comment.