Closed andygrunwald closed 6 years ago
Maybe those types need to be flagged as interface{}
?
Yeah, it's difficult to know how to handle a field that can be one of two things, e.g. an object or an array of objects.
Your idea of using interface{} would allow the schema to be handled, even though you'd end up having to use type assertions to figure out what it was:
[https://play.golang.org/p/89O8MrWnm_]
Do you think it should generate helper functions to access the values appropriately, or just dump out interface{} in the generated schema?
@a-h Your snipped can't be found anymore. Anyway.
Do you think it should generate helper functions to access the values appropriately, or just dump out interface{} in the generated schema?
Both ideas are good. An alternative idea would be to throw out a log message like field X can't be parsed, because of reason Y. IMO generate is often used as a one time tool and after this the generated code will be modified. All suggested options are fine. I think in the end it would be your choice in relation to the time you have available for development.
interface{}
would be goodThis is my rate scala where 0. is less effort and 3. the most.
Hey @andygrunwald, got a chance to look at this and created https://github.com/a-h/generate/pull/33
For cases where there are multiple possible types, e.g. string or number, it generates using interface{}
, clients can unpack the values using type assertions, so it's item 2 on your list.
Look good to you?
If you try to compile this example:
via
If you change
"type": ["object", "array"]
to"type": "object",
it works and producesTo be honest: This is tricky. I don`t know how to react or solve this one. Maybe you got an idea and we can discuss it?