Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to disable XML fields/import when using Go #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions cmd/xgen/xgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ import (
// Config holds user-defined overrides and filters that are used when
// generating source code from an XSD document.
type Config struct {
I string
O string
Pkg string
Lang string
Version string
I string
O string
Pkg string
Lang string
Version string
NoXMLName bool
}

// Cfg are the default config for xgen. The default package name and output
Expand All @@ -62,13 +63,14 @@ var SupportLang = map[string]bool{
func parseFlags() *Config {
iPtr := flag.String("i", "", "Input file path or directory for the XML schema definition")
oPtr := flag.String("o", "xgen_out", "Output file path or directory for the generated code")
noXmlName := flag.Bool("noxml", false, "Don't add xmlName fields")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that using the --skip-go-xml-name instead of noxml, will be more clear.

pkgPtr := flag.String("p", "", "Specify the package name")
langPtr := flag.String("l", "", "Specify the language of generated code")
verPtr := flag.Bool("v", false, "Show version and exit")
helpPtr := flag.Bool("h", false, "Show this help and exit")
flag.Parse()
if *helpPtr {
fmt.Printf("xgen version: %s\r\nCopyright (c) 2020 - 2021 Ri Xu https://xuri.me All rights reserved.\r\n\r\nUsage:\r\n$ xgen [<flag> ...] <XSD file or directory> ...\n -i <path>\tInput file path or directory for the XML schema definition\r\n -o <path>\tOutput file path or directory for the generated code\r\n -p \tSpecify the package name\r\n -l \tSpecify the language of generated code (Go/C/Java/Rust/TypeScript)\r\n -h \tOutput this help and exit\r\n -v \tOutput version and exit\r\n", Cfg.Version)
fmt.Printf("xgen version: %s\r\nCopyright (c) 2020 - 2021 Ri Xu https://xuri.me All rights reserved.\r\n\r\nUsage:\r\n$ xgen [<flag> ...] <XSD file or directory> ...\n -i <path>\tInput file path or directory for the XML schema definition\r\n -o <path>\tOutput file path or directory for the generated code\r\n -p \tSpecify the package name\r\n -l \tSpecify the language of generated code (Go/C/Java/Rust/TypeScript)\r\n --noxml \tDon't generate XMLName fields on structs (Go)\r\n -h \tOutput this help and exit\r\n -v \tOutput version and exit\r\n", Cfg.Version)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc in the README.md also needs to update.

os.Exit(0)
}
if *verPtr {
Expand All @@ -85,6 +87,9 @@ func parseFlags() *Config {
os.Exit(1)
}
Cfg.Lang = *langPtr
if *noXmlName {
Cfg.NoXMLName = *noXmlName
}
if *oPtr != "" {
Cfg.O = *oPtr
}
Expand All @@ -110,6 +115,7 @@ func main() {
FilePath: file,
InputDir: cfg.I,
OutputDir: cfg.O,
NoXMLName: cfg.NoXMLName,
Lang: cfg.Lang,
Package: cfg.Pkg,
IncludeMap: make(map[string]bool),
Expand Down
9 changes: 5 additions & 4 deletions genGo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type CodeGenerator struct {
Package string
ImportTime bool // For Go language
ImportEncodingXML bool // For Go language
NoXMLName bool // For Go language
ProtoTree []interface{}
StructAST map[string]string
}
Expand Down Expand Up @@ -149,7 +150,7 @@ func (gen *CodeGenerator) GoSimpleType(v *SimpleType) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " struct {\n"
fieldName := genGoFieldName(v.Name, true)
if fieldName != v.Name {
if fieldName != v.Name && !gen.NoXMLName {
gen.ImportEncodingXML = true
content += fmt.Sprintf("\tXMLName\txml.Name\t`xml:\"%s\"`\n", v.Name)
}
Expand Down Expand Up @@ -182,7 +183,7 @@ func (gen *CodeGenerator) GoComplexType(v *ComplexType) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " struct {\n"
fieldName := genGoFieldName(v.Name, true)
if fieldName != v.Name {
if fieldName != v.Name && !gen.NoXMLName {
gen.ImportEncodingXML = true
content += fmt.Sprintf("\tXMLName\txml.Name\t`xml:\"%s\"`\n", v.Name)
}
Expand Down Expand Up @@ -250,7 +251,7 @@ func (gen *CodeGenerator) GoGroup(v *Group) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " struct {\n"
fieldName := genGoFieldName(v.Name, true)
if fieldName != v.Name {
if fieldName != v.Name && !gen.NoXMLName{
gen.ImportEncodingXML = true
content += fmt.Sprintf("\tXMLName\txml.Name\t`xml:\"%s\"`\n", v.Name)
}
Expand Down Expand Up @@ -282,7 +283,7 @@ func (gen *CodeGenerator) GoAttributeGroup(v *AttributeGroup) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " struct {\n"
fieldName := genGoFieldName(v.Name, true)
if fieldName != v.Name {
if fieldName != v.Name && !gen.NoXMLName{
gen.ImportEncodingXML = true
content += fmt.Sprintf("\tXMLName\txml.Name\t`xml:\"%s\"`\n", v.Name)
}
Expand Down
2 changes: 2 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Options struct {
FileDir string
InputDir string
OutputDir string
NoXMLName bool
Extract bool
Lang string
Package string
Expand Down Expand Up @@ -140,6 +141,7 @@ func (opt *Options) Parse() (err error) {
Lang: opt.Lang,
Package: opt.Package,
File: path,
NoXMLName: opt.NoXMLName,
ProtoTree: opt.ProtoTree,
StructAST: map[string]string{},
}
Expand Down