Skip to content

Commit

Permalink
fix: gracefully handle empty package name with invalid xml input
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
meblum authored and twpayne committed Aug 21, 2024
1 parent 8fd92aa commit 56e6570
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,12 @@ func (g *Generator) Generate() ([]byte, error) {
}
sourceWithoutPackageDeclaration := make([]byte, 0, len(source))
sourceWithoutPackageDeclaration = append(sourceWithoutPackageDeclaration, source[:indexOfPackageDeclaration]...)
sourceWithoutPackageDeclaration = append(sourceWithoutPackageDeclaration, source[indexOfPackageDeclaration+len(packageDeclaration)+1:]...)
indexOfTypeDecleration := indexOfPackageDeclaration + len(packageDeclaration)
// remove \n prefix
if len(source) > indexOfTypeDecleration {
indexOfTypeDecleration++
}
sourceWithoutPackageDeclaration = append(sourceWithoutPackageDeclaration, source[indexOfTypeDecleration:]...)
source = sourceWithoutPackageDeclaration
}

Expand Down
11 changes: 11 additions & 0 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ func TestGenerator(t *testing.T) {
`}`,
),
},
{
name: "no_package_empty_input",
options: []xmlstruct.GeneratorOption{
xmlstruct.WithHeader(""),
xmlstruct.WithNamedTypes(true),
xmlstruct.WithPackageName(""),
xmlstruct.WithFormatSource(true),
},
xmlStr: "",
expectedStr: "",
},
{
name: "no_package_unformatted",
options: []xmlstruct.GeneratorOption{
Expand Down

0 comments on commit 56e6570

Please sign in to comment.