Skip to content

Commit

Permalink
Handle multi-line comments well
Browse files Browse the repository at this point in the history
  • Loading branch information
isimluk committed Feb 4, 2020
1 parent b0a71f7 commit fa035b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 22 additions & 6 deletions metaschema/metaschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ func (da *DefineAssembly) RepresentsRootElement() bool {
return da.Name == "catalog" || da.Name == "profile" || da.Name == "declarations"
}

func (a *DefineAssembly) GoComment() string {
return handleMultiline(a.Description)
}

type DefineField struct {
Name string `xml:"name,attr"`
GroupAs string `xml:"group-as,attr"`
Expand All @@ -245,6 +249,10 @@ func (df *DefineField) RequiresPointer() bool {
return len(df.Flags) > 0
}

func (f *DefineField) GoComment() string {
return handleMultiline(f.Description)
}

type DefineFlag struct {
Name string `xml:"name,attr"`
AsType datatype `xml:"as-type,attr"`
Expand Down Expand Up @@ -278,9 +286,9 @@ type Assembly struct {

func (a *Assembly) GoComment() string {
if a.Description != "" {
return a.Description
return handleMultiline(a.Description)
}
return a.Def.Description
return a.Def.GoComment()
}

func (a *Assembly) GoName() string {
Expand Down Expand Up @@ -331,9 +339,9 @@ type Field struct {

func (f *Field) GoComment() string {
if f.Description != "" {
return f.Description
return handleMultiline(f.Description)
}
return f.Def.Description
return f.Def.GoComment()
}

func (f *Field) RequiresPointer() bool {
Expand Down Expand Up @@ -395,14 +403,18 @@ type Flag struct {

func (f *Flag) GoComment() string {
if f.Description != "" {
return f.Description
return handleMultiline(f.Description)
}
return f.Def.Description
return handleMultiline(f.Def.Description)
}

func (f *Flag) GoDatatype() (string, error) {
dt := f.AsType
if dt == "" {
if f.Ref == "" && f.Name == "position" {
// workaround bug: inline definition without type hint https://github.com/usnistgov/OSCAL/pull/570
return "string", nil
}
dt = f.Def.AsType
}

Expand Down Expand Up @@ -589,3 +601,7 @@ var goDatatypeMap = map[datatype]string{
datatypeID: "string",
datatypeURIRef: "string",
}

func handleMultiline(comment string) string {
return strings.ReplaceAll(comment, "\n", "\n // ")
}
4 changes: 2 additions & 2 deletions metaschema/types.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package {{ $packageName }}

{{$m := . -}}
{{range .DefineAssembly}}
// {{ .Description }}
// {{ .GoComment }}
type {{toCamel .Name}} struct {
{{if .RepresentsRootElement }}
XMLName xml.Name `xml:"http://csrc.nist.gov/ns/oscal/1.0 {{ .Name }}" json:"-"`
Expand Down Expand Up @@ -60,7 +60,7 @@ type {{toCamel .Name}} struct {
{{end}}

{{range .DefineField}}
// {{ .Description }}
// {{ .GoComment }}
{{$l := len .Flags -}}
{{- if gt $l 0 -}}
type {{toCamel .Name}} struct {
Expand Down

0 comments on commit fa035b2

Please sign in to comment.