OneOf method generates useless code? #3597
-
I'm trying to use OneOf in my schema, like this var Report = dsl.Type("Report", func() {
dsl.Description("Report describes a generated report.")
dsl.Attribute("workspace_id", dsl.String, "Workspace ID")
dsl.Attribute("assets", dsl.ArrayOf(dsl.String), "Assets")
dsl.Attribute("from", dsl.String, "From")
dsl.Attribute("to", dsl.String, "To")
dsl.Required("workspace_id", "assets", "from", "to")
})
var Data = dsl.Type("Data", func() {
dsl.Description("Data describes the data needed to generate a report.")
dsl.OneOf("forms", "Payload forms", func() {
dsl.Attribute("report", Report, "Report")
})
})
var Form = dsl.Type("Form", func() {
dsl.Description("Form describes a Report that can be created or updated via the API.")
dsl.Attribute("template_id", dsl.String, func() {
dsl.Meta("struct:tag:json", "template_id")
})
dsl.Attribute("data", Data, func() {
dsl.Meta("struct:tag:json", "data")
})
dsl.Required("template_id", "data")
}) There is only one type so far but if it works I plan to add others. The generated code has these structs which I cannot find a way to use: // Data describes the data needed to generate a report.
type Data struct {
// Payload forms
Forms interface {
formsVal()
}
}
// Form describes a Report that can be created or updated via the API.
type Form struct {
TemplateID string `json:"template_id"`
Data *Data `json:"data"`
}
// GeneratePayload is the payload type of the templates service generate method.
type GeneratePayload struct {
// Workspace ID
WorkspaceID string
// Form of the template
Form *Form
}
// Report describes a generated report.
type Report struct {
// Workspace ID
WorkspaceID string
// Assets
Assets []string
// From
From string
// To
To string
}
func (*Report) formsVal() {} How am I supposed to use it |
Beta Was this translation helpful? Give feedback.
Answered by
douglaswth
Sep 25, 2024
Replies: 1 comment
-
A pointer to the data := &gen.Data{
Forms: &gen.Report{
WorkspaceID: "abcdef",
Assets: []string{"a", "b", "c"},
From: "a",
To: "b",
},
} On the other side, a type switch (or a checked type assertion) can be used to determine which type a value in the switch forms := data.Forms.(type) {
case *gen.Report:
fmt.Println(forms.WorkspaceID, forms.Assets, forms.From, forms.To)
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
antipopp
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A pointer to the
Report
type can be assigned to theForms
field of theData
type since it implements the interface:On the other side, a type switch (or a checked type assertion) can be used to determine which type a value in the
Forms
field of theData
type is: