-
Notifications
You must be signed in to change notification settings - Fork 95
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
Handling duplicate types #100
Comments
Hi @glvr182 ! There are several reasons for that, but mostly it boils down to avoiding type naming collisions that would cause invalid code to be generated, as you can't have two types with the same name within the same package, so I am afraid there is no flag to disable that. |
Understood. I actually also had the problem where a type was declared twice in the same file. I patched it locally by just completely removing the uniquename check but this should not be how to handle such cases. I shall post an example of this in a followup message in this issue. |
awesome, thanks! |
@omissis Okay so i have the following schema {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://www.openchargealliance.org/schemas/oscp/2.0/energy-measurement-unit.json",
"title": "EnergyMeasurementUnit",
"uniqueItems": true,
"type": "string",
"enum": [
"WH",
"KWH"
]
} and then a file referencing this: {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://www.openchargealliance.org/schemas/oscp/2.0/energy-measurement.json",
"title": "EnergyMeasurement",
"type": "object",
"properties": {
"value": {
"type": "number"
},
"phase": {
"$ref": "phase-indicator.json"
},
"unit": {
"$ref": "energy-measurement-unit.json"
},
"direction": {
"$ref": "energy-flow-direction.json"
},
"energy_type": {
"$ref": "energy-type.json"
},
"measure_time": {
"type": "string",
"format": "date-time"
},
"initial_measure_time": {
"type": "string",
"format": "date-time"
}
},
"required": [
"value",
"phase",
"unit",
"direction",
"measure_time"
]
} resulting in the following: type EnergyMeasurementUnit string
type EnergyMeasurementUnit string
const EnergyMeasurementUnitKWH EnergyMeasurementUnit = "KWH"
const EnergyMeasurementUnitKWH EnergyMeasurementUnit = "KWH"
const EnergyMeasurementUnitWH EnergyMeasurementUnit = "WH"
const EnergyMeasurementUnitWH EnergyMeasurementUnit = "WH" I think this should be solved with support for nested schemas. What do you think. (this was generatated with the lastest commit of your Quick note these schemas are taken from the OSCP specification |
thanks for the example. that looks weird, did you use the stable release to generate the go model or is that the result of the |
That is the result of both the stable and |
ah yeah sorry I didn't see that note. ok I'll be looking into it in the next few days, I am working with a fairly complex schema these days and I haven't noticed that behavior. |
This is a hack that my colleague made a while ago on a older version of your project. It made it work but we don't know if it broke other things so we want to do it properly this time. Does this give you any idea what might be the reason. --- a/pkg/generator/generate.go
+++ b/pkg/generator/generate.go
@@ -388,12 +388,16 @@ func (g *schemaGenerator) generateDeclaredType(
return &codegen.NamedType{Decl: decl}, nil
}
+ if decl, ok := g.output.declsByName[scope.string()]; ok {
+ return &codegen.NamedType{Decl: decl}, nil
+ }
+
if t.Enum != nil {
return g.generateEnumType(t, scope)
}
decl := codegen.TypeDecl{
- Name: g.output.uniqueTypeName(scope.string()),
+ Name: scope.string(),
Comment: t.Description,
}
g.output.declsBySchema[t] = &decl
@@ -718,7 +722,7 @@ func (g *schemaGenerator) generateEnumType(
}
enumDecl := codegen.TypeDecl{
- Name: g.output.uniqueTypeName(scope.string()),
+ Name: scope.string(),
Type: enumType,
}
g.output.file.Package.AddDecl(&enumDecl)
@@ -800,22 +804,6 @@ type output struct {
warner func(string)
}
-func (o *output) uniqueTypeName(name string) string {
- if _, ok := o.declsByName[name]; !ok {
- return name
- }
- count := 1
- for {
- suffixed := fmt.Sprintf("%s_%d", name, count)
- if _, ok := o.declsByName[suffixed]; !ok {
- o.warner(fmt.Sprintf(
- "Multiple types map to the name %q; declaring duplicate as %q instead", name, suffixed))
- return suffixed
- }
- count++
- }
-}
-
type cachedEnum struct {
values []interface{}
enum *codegen.TypeDecl |
hello, is there any update on this? I tried the |
Hey! no updates so far. That branch fell behind main and I need to take some time to rebase it before I can release it. 😅 |
thanks for the update, can you give like a days/weeks/months prediction for it? as I said I can also help if there are somewhat clear steps in the PR 🙂 |
I am on it today at the Open Source Saturday Milan 😎 Fingers crossed! |
Hello @omissis and thank you for this great package! I just tested feature/implement-subschema on my schemas and it feels way better than the current main (my case is simply having 2 schemas using the same subschema to generate). I saw that you fixed the tests, can we expect a merge in main soon? |
Hey @CarbonC , the idea is to merge it to main in the next release. I have completed a long due rebase, and I now probably need to to one more to align it to main again. Since it's kind of a big change, I don't want to rush it and break (too many) things: my plan is to take some time to go through the open issues and comments and maybe some big known json schemas and see how things look like with this new version. I don't have a timeline for that though I'm afraid, so if anyone is willing to step up and help with additional testing, they're more than welcome :) |
Well since I was here, I took the chance to rebase: the PR is conflict-free again now. |
I have multiple types referencing to the another type. Currently the generator will just increment the counter after the type (say "example_1").
Is there a way to disable this behaviour. Maybe using a config option?
The text was updated successfully, but these errors were encountered: