omissis / go-jsonschema

A tool to generate Go data types from JSON Schema definitions.
MIT License
563 stars 90 forks source link

improve support for additionalProperties #188

Closed codeboten closed 4 months ago

codeboten commented 7 months ago

With the following schema, I would expect to be able to access additional properties on the generated struct:

        "Attributes": {
            "title": "Attributes",
            "type": "object",
            "additionalProperties": true,
            "properties": {
                "service.name": {
                    "type": "string"
                }
            }
        }

But looking at the generated struct, there's no way for me to get additional properties:

type Attributes struct {
    // ServiceName corresponds to the JSON schema field "service.name".
    ServiceName *string `mapstructure:"service.name,omitempty"`
}

I'd like to propose an additional field is added to the generated struct to look something like this:

type Attributes struct {
    // ServiceName corresponds to the JSON schema field "service.name".
    ServiceName *string `mapstructure:"service.name,omitempty"`
    AdditionalProperties map[string]interface{} // <--- the type here could depend on the "type" field if one is set
}

Reference

Full schema: https://github.com/open-telemetry/opentelemetry-configuration/blob/c5128cc4bb08514431e2c7b0573e0503e23df522/schema/resource.json#L15

Generated code: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/044e09ac8bfe0d5a60510778017bec57413243d9/config/generated_config.go#L19-L22

omissis commented 4 months ago

closed by #218