xuri / xgen

XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
BSD 3-Clause "New" or "Revised" License
339 stars 80 forks source link

Fix directory creation and filename extension handling #57

Closed brianpursley closed 2 years ago

brianpursley commented 2 years ago

PR Details

Fix directory creation and filename extension handling

Description

Fixes a bug where xgen assumes that the output path provided by the user is a directory. This removes the call to PrepareOutputDir from xgen.go, but this should be fine because PrepareOutputDir is also called, correctly, in parser.go.

Fixes a bug where the language-specific generator code assumes that the output path does not contain the file extension. This commit adds a helper function called FileWithExtension that checks to see if the extension already exists on the filename and does not add it again if it does.

Related Issue

Fixes #56

Motivation and Context

How Has This Been Tested

Unit tests and manual tests (see below).

$ go run cmd/xgen/xgen.go -l Java -i test/xsd/base64.xsd -o output.java
done
$ ls -la | grep output
-rw-rw-r--  1 bpursley bpursley  2066 Jul 20 10:05 output.java
$ go run cmd/xgen/xgen.go -l Java -i test/xsd/base64.xsd -o output
done
$ ls -la | grep output
-rw-rw-r--  1 bpursley bpursley  2066 Jul 20 10:06 output.java
$ go run cmd/xgen/xgen.go -l Java -i test/xsd/base64.xsd
done
$ ls -la | grep xgen_out
-rw-rw-r--  1 bpursley bpursley  2066 Jul 20 10:06 xgen_out.java
$ go run cmd/xgen/xgen.go -l Java -i test/xsd -o output
done
$ ls -la | grep output
drwxr-xr-x  2 bpursley bpursley  4096 Jul 20 10:08 output
$ ls -la output
total 12
drwxr-xr-x  2 bpursley bpursley 4096 Jul 20 10:08 .
drwxrwxr-x 11 bpursley bpursley 4096 Jul 20 10:08 ..
-rw-rw-r--  1 bpursley bpursley 2066 Jul 20 10:08 base64.xsd.java
$ go run cmd/xgen/xgen.go -l Java -i test/xsd
done
$ ls -la | grep xgen_out
drwxr-xr-x  2 bpursley bpursley  4096 Jul 20 10:09 xgen_out
$ ls -la xgen_out
total 12
drwxr-xr-x  2 bpursley bpursley 4096 Jul 20 10:09 .
drwxrwxr-x 11 bpursley bpursley 4096 Jul 20 10:09 ..
-rw-rw-r--  1 bpursley bpursley 2066 Jul 20 10:09 base64.xsd.java

Types of changes

Checklist