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

Commit

Permalink
fix generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Weiss committed Nov 14, 2018
1 parent 35171ce commit c9f25db
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
31 changes: 23 additions & 8 deletions metaschema/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ var datatypes = map[Datatype]string{

func GenerateTypes(metaschema *Metaschema) error {
t, err := template.New("types.tmpl").Funcs(template.FuncMap{
"toLower": strings.ToLower,
"toCamel": strcase.ToCamel,
"toLowerCamel": strcase.ToLowerCamel,
"wrapString": wrapString,
"parseDatatype": parseDatatype,
"commentFlag": commentFlag,
"packageImport": packageImport,
"getImports": getImports,
"toLower": strings.ToLower,
"toCamel": strcase.ToCamel,
"toLowerCamel": strcase.ToLowerCamel,
"wrapString": wrapString,
"parseDatatype": parseDatatype,
"commentFlag": commentFlag,
"packageImport": packageImport,
"getImports": getImports,
"requiresPointer": requiresPointer,
}).ParseFiles("types.tmpl")
if err != nil {
return err
Expand Down Expand Up @@ -135,3 +136,17 @@ func getImports(metaschema Metaschema) string {

return imports.String()
}

func requiresPointer(fieldName string, metaschema Metaschema) bool {
for _, df := range metaschema.DefineField {
if df.Name == fieldName {
if len(df.Flags) > 0 {
return true
}

return false
}
}

return false
}
4 changes: 2 additions & 2 deletions metaschema/types.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type {{toCamel .Name}} struct {
{{- end}}

{{- range .Model.Field}}
{{toCamel .Named}} *{{packageImport .Named $m}}{{toCamel .Named}} `xml:"{{ .Named }},omitempty" json:"{{toLowerCamel .Named}},omitempty"`
{{toCamel .Named}} {{if requiresPointer .Named $m}}*{{end}}{{packageImport .Named $m}}{{toCamel .Named}} `xml:"{{ .Named }},omitempty" json:"{{toLowerCamel .Named}},omitempty"`
{{- end}}

{{- range .Model.Fields}}
Expand All @@ -40,7 +40,7 @@ type {{toCamel .Name}} struct {

{{- range .Model.Choice}}
{{- range .Field}}
{{toCamel .Named}} *{{packageImport .Named $m}}{{toCamel .Named}} `xml:"{{ .Named }},omitempty" json:"{{toLowerCamel .Named}},omitempty"`
{{toCamel .Named}} {{if requiresPointer .Named $m}}*{{end}}{{packageImport .Named $m}}{{toCamel .Named}} `xml:"{{ .Named }},omitempty" json:"{{toLowerCamel .Named}},omitempty"`
{{- end}}

{{- range .Fields}}
Expand Down
16 changes: 8 additions & 8 deletions types/oscal/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Catalog struct {
Id string `xml:"id,attr,omitempty" json:"id,omitempty"`
// Declares a major/minor version for this metaschema
ModelVersion string `xml:"model-version,attr,omitempty" json:"modelVersion,omitempty"`
Title *Title `xml:"title,omitempty" json:"title,omitempty"`
Title Title `xml:"title,omitempty" json:"title,omitempty"`
Declarations *Declarations `xml:"declarations,omitempty" json:"declarations,omitempty"`
References *References `xml:"references,omitempty" json:"references,omitempty"`
Sections []Section `xml:"section,omitempty" json:"sections,omitempty"`
Expand All @@ -26,7 +26,7 @@ type Section struct {
Id string `xml:"id,attr,omitempty" json:"id,omitempty"`
// Identifies the property or object within the control; a semantic hint
Class string `xml:"class,attr,omitempty" json:"class,omitempty"`
Title *Title `xml:"title,omitempty" json:"title,omitempty"`
Title Title `xml:"title,omitempty" json:"title,omitempty"`
References *References `xml:"references,omitempty" json:"references,omitempty"`
Sections []Section `xml:"section,omitempty" json:"sections,omitempty"`
Prose *Prose `xml:",any" json:"prose,omitempty"`
Expand All @@ -39,7 +39,7 @@ type Group struct {
Id string `xml:"id,attr,omitempty" json:"id,omitempty"`
// Identifies the property or object within the control; a semantic hint
Class string `xml:"class,attr,omitempty" json:"class,omitempty"`
Title *Title `xml:"title,omitempty" json:"title,omitempty"`
Title Title `xml:"title,omitempty" json:"title,omitempty"`
Props []Prop `xml:"prop,omitempty" json:"props,omitempty"`
References *References `xml:"references,omitempty" json:"references,omitempty"`
Params []Param `xml:"param,omitempty" json:"params,omitempty"`
Expand All @@ -57,7 +57,7 @@ type Control struct {
Id string `xml:"id,attr,omitempty" json:"id,omitempty"`
// Identifies the property or object within the control; a semantic hint
Class string `xml:"class,attr,omitempty" json:"class,omitempty"`
Title *Title `xml:"title,omitempty" json:"title,omitempty"`
Title Title `xml:"title,omitempty" json:"title,omitempty"`
Props []Prop `xml:"prop,omitempty" json:"props,omitempty"`
Links []Link `xml:"link,omitempty" json:"links,omitempty"`
References *References `xml:"references,omitempty" json:"references,omitempty"`
Expand All @@ -73,7 +73,7 @@ type Subcontrol struct {
Id string `xml:"id,attr,omitempty" json:"id,omitempty"`
// Identifies the property or object within the control; a semantic hint
Class string `xml:"class,attr,omitempty" json:"class,omitempty"`
Title *Title `xml:"title,omitempty" json:"title,omitempty"`
Title Title `xml:"title,omitempty" json:"title,omitempty"`
Props []Prop `xml:"prop,omitempty" json:"props,omitempty"`
Links []Link `xml:"link,omitempty" json:"links,omitempty"`
References *References `xml:"references,omitempty" json:"references,omitempty"`
Expand All @@ -91,12 +91,12 @@ type Param struct {
Class string `xml:"class,attr,omitempty" json:"class,omitempty"`
// Another parameter invoking this one
DependsOn string `xml:"depends-on,attr,omitempty" json:"dependsOn,omitempty"`
Label *Label `xml:"label,omitempty" json:"label,omitempty"`
Label Label `xml:"label,omitempty" json:"label,omitempty"`
Descriptions []Desc `xml:"desc,omitempty" json:"descriptions,omitempty"`
Constraints []Constraint `xml:"constraint,omitempty" json:"constraints,omitempty"`
Links []Link `xml:"link,omitempty" json:"links,omitempty"`
Guidance []Guideline `xml:"guideline,omitempty" json:"guidance,omitempty"`
Value *Value `xml:"value,omitempty" json:"value,omitempty"`
Value Value `xml:"value,omitempty" json:"value,omitempty"`
Select *Select `xml:"select,omitempty" json:"select,omitempty"`
}

Expand All @@ -120,7 +120,7 @@ type Part struct {
Id string `xml:"id,attr,omitempty" json:"id,omitempty"`
// Identifies the property or object within the control; a semantic hint
Class string `xml:"class,attr,omitempty" json:"class,omitempty"`
Title *Title `xml:"title,omitempty" json:"title,omitempty"`
Title Title `xml:"title,omitempty" json:"title,omitempty"`
Props []Prop `xml:"prop,omitempty" json:"props,omitempty"`
Links []Link `xml:"link,omitempty" json:"links,omitempty"`
Parts []Part `xml:"part,omitempty" json:"parts,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions types/oscal/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Import struct {
// A Merge element merges controls in resolution.
type Merge struct {
Combine *Combine `xml:"combine,omitempty" json:"combine,omitempty"`
AsIs *AsIs `xml:"as-is,omitempty" json:"asIs,omitempty"`
AsIs AsIs `xml:"as-is,omitempty" json:"asIs,omitempty"`
Custom *Custom `xml:"custom,omitempty" json:"custom,omitempty"`
}

Expand Down Expand Up @@ -70,12 +70,12 @@ type SetParam struct {
Id string `xml:"id,attr,omitempty" json:"id,omitempty"`
Class string `xml:"class,attr,omitempty" json:"class,omitempty"`
DependsOn string `xml:"depends-on,attr,omitempty" json:"dependsOn,omitempty"`
Label *catalog.Label `xml:"label,omitempty" json:"label,omitempty"`
Label catalog.Label `xml:"label,omitempty" json:"label,omitempty"`
Descriptions []catalog.Desc `xml:"desc,omitempty" json:"descriptions,omitempty"`
Constraints []catalog.Constraint `xml:"constraint,omitempty" json:"constraints,omitempty"`
Links []catalog.Link `xml:"link,omitempty" json:"links,omitempty"`
Parts []catalog.Part `xml:"part,omitempty" json:"parts,omitempty"`
Value *catalog.Value `xml:"value,omitempty" json:"value,omitempty"`
Value catalog.Value `xml:"value,omitempty" json:"value,omitempty"`
Select *catalog.Select `xml:"select,omitempty" json:"select,omitempty"`
}

Expand All @@ -97,7 +97,7 @@ type Add struct {
// Where to add the new content with respect to the targeted element (beside it or
// inside it)
Position string `xml:"position,attr,omitempty" json:"position,omitempty"`
Title *catalog.Title `xml:"title,omitempty" json:"title,omitempty"`
Title catalog.Title `xml:"title,omitempty" json:"title,omitempty"`
Props []catalog.Prop `xml:"prop,omitempty" json:"props,omitempty"`
Links []catalog.Link `xml:"link,omitempty" json:"links,omitempty"`
References *catalog.References `xml:"references,omitempty" json:"references,omitempty"`
Expand Down

0 comments on commit c9f25db

Please sign in to comment.