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

Add support for static type overrides with imports #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

halostatue
Copy link

@halostatue halostatue commented Jun 21, 2024

This allows static type overrides to be provided on the generator command-line, optionally with imports.

This has been added specifically to override the use of float64 for xsd:decimal. For our current need we only need to swap the handled type from float64 to string:

$ ./gocomply_xsd2go convert XSD NAMESPACE DESTINATION \
    --type-override decimal=string

For the catalog.xsd (which also requires the xml.xsd) from https://salesforcecommercecloud.github.io/b2c-dev-doc/docs/current/xsd/, this generates the following partial diff:

diff --git i/catalog/models.go w/catalog/models.go
index e0be71998af5..80d355eb279d 100644
--- i/catalog/models.go
+++ w/catalog/models.go
@@ -150,17 +150,17 @@ type Product struct {

 	Upc *SimpleTypeGenericString256 `xml:"upc"`

 	Unit *SimpleTypeGenericString256 `xml:"unit"`

-	UnitQuantity *float64 `xml:"unit-quantity"`
+	UnitQuantity string `xml:"unit-quantity"`

 	UnitMeasure *SimpleTypeGenericString60 `xml:"unit-measure"`

-	MinOrderQuantity *float64 `xml:"min-order-quantity"`
+	MinOrderQuantity string `xml:"min-order-quantity"`

-	StepQuantity *float64 `xml:"step-quantity"`
+	StepQuantity string `xml:"step-quantity"`

 	DisplayName []SharedTypeLocalizedString `xml:"display-name"`

 	ShortDescription []SharedTypeLocalizedText `xml:"short-description"`

If we needed to keep it usable for math, but support fixed point decimal values, we could use github.com/ericlagergren/decimal:

$ ./gocomply_xsd2go convert XSD NAMESPACE DESTINATION \
    --type-override decimal=decimal:github.com/ericlagergren/decimal

This produces the following partial diff:

diff --git i/catalog/models.go w/catalog/models.go
index e0be71998af5..f3d574061389 100644
--- i/catalog/models.go
+++ w/catalog/models.go
@@ -2,10 +2,11 @@
 // Models for http://www.demandware.com/xml/impex/catalog/2006-10-31
 package catalog

 import (
 	"encoding/xml"
+	"github.com/ericlagergren/decimal"
 )

 // Element
 type Catalog struct {
 	XMLName xml.Name `xml:"catalog"`
@@ -150,17 +151,17 @@ type Product struct {

 	Upc *SimpleTypeGenericString256 `xml:"upc"`

 	Unit *SimpleTypeGenericString256 `xml:"unit"`

-	UnitQuantity *float64 `xml:"unit-quantity"`
+	UnitQuantity *decimal `xml:"unit-quantity"`

 	UnitMeasure *SimpleTypeGenericString60 `xml:"unit-measure"`

-	MinOrderQuantity *float64 `xml:"min-order-quantity"`
+	MinOrderQuantity *decimal `xml:"min-order-quantity"`

-	StepQuantity *float64 `xml:"step-quantity"`
+	StepQuantity *decimal `xml:"step-quantity"`

 	DisplayName []SharedTypeLocalizedString `xml:"display-name"`

 	ShortDescription []SharedTypeLocalizedText `xml:"short-description"`

There are no constraints on ensuring that the XSD static type currently exists, so this could potentially be useful for providing temporary implementations for values like xsd:duration, but it is not clear to me whether this is the case.

This allows static type overrides to be provided on the generator
command-line, optionally with imports.

This has been added specifically to override the use of `float64` for
`xsd:decimal`. For our current need  we only need to swap the handled
type from `float64` to `string`:

```console
$ ./gocomply_xsd2go convert XSD NAMESPACE DESTINATION \
    --type-override decimal=string
```

For the `catalog.xsd` (which also requires the `xml.xsd`) from
https://salesforcecommercecloud.github.io/b2c-dev-doc/docs/current/xsd/,
this generates the following partial diff:

```diff
diff --git i/catalog/models.go w/catalog/models.go
index e0be71998af5..80d355eb279d 100644
--- i/catalog/models.go
+++ w/catalog/models.go
@@ -150,17 +150,17 @@ type Product struct {

 	Upc *SimpleTypeGenericString256 `xml:"upc"`

 	Unit *SimpleTypeGenericString256 `xml:"unit"`

-	UnitQuantity *float64 `xml:"unit-quantity"`
+	UnitQuantity string `xml:"unit-quantity"`

 	UnitMeasure *SimpleTypeGenericString60 `xml:"unit-measure"`

-	MinOrderQuantity *float64 `xml:"min-order-quantity"`
+	MinOrderQuantity string `xml:"min-order-quantity"`

-	StepQuantity *float64 `xml:"step-quantity"`
+	StepQuantity string `xml:"step-quantity"`

 	DisplayName []SharedTypeLocalizedString `xml:"display-name"`

 	ShortDescription []SharedTypeLocalizedText `xml:"short-description"`
```

If we needed to keep it usable for math, but support fixed point decimal
values, we could use `github.com/ericlagergren/decimal`:

```console
$ ./gocomply_xsd2go convert XSD NAMESPACE DESTINATION \
    --type-override decimal=decimal:github.com/ericlagergren/decimal
```

This produces the following partial diff:

```diff
diff --git i/catalog/models.go w/catalog/models.go
index e0be71998af5..f3d574061389 100644
--- i/catalog/models.go
+++ w/catalog/models.go
@@ -2,10 +2,11 @@
 // Models for http://www.demandware.com/xml/impex/catalog/2006-10-31
 package catalog

 import (
 	"encoding/xml"
+	"github.com/ericlagergren/decimal"
 )

 // Element
 type Catalog struct {
 	XMLName xml.Name `xml:"catalog"`
@@ -150,17 +151,17 @@ type Product struct {

 	Upc *SimpleTypeGenericString256 `xml:"upc"`

 	Unit *SimpleTypeGenericString256 `xml:"unit"`

-	UnitQuantity *float64 `xml:"unit-quantity"`
+	UnitQuantity *decimal `xml:"unit-quantity"`

 	UnitMeasure *SimpleTypeGenericString60 `xml:"unit-measure"`

-	MinOrderQuantity *float64 `xml:"min-order-quantity"`
+	MinOrderQuantity *decimal `xml:"min-order-quantity"`

-	StepQuantity *float64 `xml:"step-quantity"`
+	StepQuantity *decimal `xml:"step-quantity"`

 	DisplayName []SharedTypeLocalizedString `xml:"display-name"`

 	ShortDescription []SharedTypeLocalizedText `xml:"short-description"`
```

There are no constraints on ensuring that the XSD static type currently
exists, so this *could* potentially be useful for providing temporary
implementations for values like `xsd:duration`, but it is not clear to
me whether this is the case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant