grafana / cog

Code Generation with a human touch
Apache License 2.0
48 stars 2 forks source link

Restructure codegen pipeline loading & running code, in preparation for even more refactoring #406

Closed K-Phoen closed 5 months ago

K-Phoen commented 5 months ago

This PR starts reworking the "config-related" code: by config, we actually mean "codegen pipeline" and there's a lot of code in a single file that should be split better.

More PRs will follow, to move this code out of the cmd/ folder and hopefully end-up with a public API that will make cog usable as a library as well.

Relates to #408

github-actions[bot] commented 5 months ago

Note: in addition to the changes introduced by this PR, the diff includes unreleased changes living in main.

### 🔎 Changes to `grafana-foundation-sdk@next+cog-v0.0.x` ```patch diff --git a/go/alerting/rule_builder_gen.go b/go/alerting/rule_builder_gen.go index 26c5fa1..6e66fe8 100644 --- a/go/alerting/rule_builder_gen.go +++ b/go/alerting/rule_builder_gen.go @@ -4,6 +4,7 @@ package alerting import ( "errors" + "time" cog "github.com/grafana/grafana-foundation-sdk/go/cog" ) @@ -197,7 +198,7 @@ func (builder *RuleBuilder) Uid(uid string) *RuleBuilder { return builder } -func (builder *RuleBuilder) Updated(updated string) *RuleBuilder { +func (builder *RuleBuilder) Updated(updated time.Time) *RuleBuilder { builder.internal.Updated = &updated return builder diff --git a/go/alerting/types_gen.go b/go/alerting/types_gen.go index e12da94..3f3142d 100644 --- a/go/alerting/types_gen.go +++ b/go/alerting/types_gen.go @@ -3,6 +3,10 @@ package alerting import ( + "encoding/json" + "time" + + cog "github.com/grafana/grafana-foundation-sdk/go/cog" cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" ) @@ -82,7 +86,7 @@ type Rule struct { RuleGroup string `json:"ruleGroup"` Title string `json:"title"` Uid *string `json:"uid,omitempty"` - Updated *string `json:"updated,omitempty"` + Updated *time.Time `json:"updated,omitempty"` } type NotificationSettings struct { @@ -102,6 +106,52 @@ type Query struct { RelativeTimeRange *RelativeTimeRange `json:"relativeTimeRange,omitempty"` } +func (resource *Query) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + fields := make(map[string]json.RawMessage) + if err := json.Unmarshal(raw, &fields); err != nil { + return err + } + + if fields["datasourceUid"] != nil { + if err := json.Unmarshal(fields["datasourceUid"], &resource.DatasourceUid); err != nil { + return err + } + } + + if fields["queryType"] != nil { + if err := json.Unmarshal(fields["queryType"], &resource.QueryType); err != nil { + return err + } + } + + if fields["refId"] != nil { + if err := json.Unmarshal(fields["refId"], &resource.RefId); err != nil { + return err + } + } + + if fields["relativeTimeRange"] != nil { + if err := json.Unmarshal(fields["relativeTimeRange"], &resource.RelativeTimeRange); err != nil { + return err + } + } + + dataqueryTypeHint := "" + + if fields["model"] != nil { + model, err := cog.UnmarshalDataquery(fields["model"], dataqueryTypeHint) + if err != nil { + return err + } + resource.Model = model + } + + return nil +} + // EmbeddedContactPoint is the contact point type that is used // by grafanas embedded alertmanager implementation. type ContactPoint struct { diff --git a/go/alerting/types_json_marshalling_gen.go b/go/alerting/types_json_marshalling_gen.go deleted file mode 100644 index 70e71ee..0000000 --- a/go/alerting/types_json_marshalling_gen.go +++ /dev/null @@ -1,55 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package alerting - -import ( - "encoding/json" - - cog "github.com/grafana/grafana-foundation-sdk/go/cog" -) - -func (resource *Query) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - fields := make(map[string]json.RawMessage) - if err := json.Unmarshal(raw, &fields); err != nil { - return err - } - - if fields["datasourceUid"] != nil { - if err := json.Unmarshal(fields["datasourceUid"], &resource.DatasourceUid); err != nil { - return err - } - } - - if fields["queryType"] != nil { - if err := json.Unmarshal(fields["queryType"], &resource.QueryType); err != nil { - return err - } - } - - if fields["refId"] != nil { - if err := json.Unmarshal(fields["refId"], &resource.RefId); err != nil { - return err - } - } - - if fields["relativeTimeRange"] != nil { - if err := json.Unmarshal(fields["relativeTimeRange"], &resource.RelativeTimeRange); err != nil { - return err - } - } - - dataqueryTypeHint := "" - - if fields["model"] != nil { - model, err := cog.UnmarshalDataquery(fields["model"], dataqueryTypeHint) - if err != nil { - return err - } - resource.Model = model - } - - return nil -} diff --git a/go/annotationslist/types_gen.go b/go/annotationslist/types_gen.go index 86321c3..c55d672 100644 --- a/go/annotationslist/types_gen.go +++ b/go/annotationslist/types_gen.go @@ -2,6 +2,12 @@ package annotationslist +import ( + "encoding/json" + + cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" +) + type Options struct { OnlyFromThisDashboard bool `json:"onlyFromThisDashboard"` OnlyInTimeRange bool `json:"onlyInTimeRange"` @@ -14,3 +20,18 @@ type Options struct { NavigateBefore string `json:"navigateBefore"` NavigateAfter string `json:"navigateAfter"` } + +func VariantConfig() cogvariants.PanelcfgConfig { + return cogvariants.PanelcfgConfig{ + Identifier: "annolist", + OptionsUnmarshaler: func(raw []byte) (any, error) { + options := Options{} + + if err := json.Unmarshal(raw, &options); err != nil { + return nil, err + } + + return options, nil + }, + } +} diff --git a/go/annotationslist/types_json_marshalling_gen.go b/go/annotationslist/types_json_marshalling_gen.go deleted file mode 100644 index 68b520f..0000000 --- a/go/annotationslist/types_json_marshalling_gen.go +++ /dev/null @@ -1,24 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package annotationslist - -import ( - "encoding/json" - - cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" -) - -func VariantConfig() cogvariants.PanelcfgConfig { - return cogvariants.PanelcfgConfig{ - Identifier: "annolist", - OptionsUnmarshaler: func(raw []byte) (any, error) { - options := Options{} - - if err := json.Unmarshal(raw, &options); err != nil { - return nil, err - } - - return options, nil - }, - } -} diff --git a/go/azuremonitor/types_gen.go b/go/azuremonitor/types_gen.go index bf889c2..0e98f37 100644 --- a/go/azuremonitor/types_gen.go +++ b/go/azuremonitor/types_gen.go @@ -2,6 +2,14 @@ package azuremonitor +import ( + "encoding/json" + "errors" + "fmt" + + cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" +) + type AzureMonitorQuery struct { // A unique identifier for the query within the list of targets. // In server side expressions, the refId is used as a variable name to identify results. @@ -42,6 +50,21 @@ type AzureMonitorQuery struct { func (resource AzureMonitorQuery) ImplementsDataqueryVariant() {} +func VariantConfig() cogvariants.DataqueryConfig { + return cogvariants.DataqueryConfig{ + Identifier: "grafana-azure-monitor-datasource", + DataqueryUnmarshaler: func(raw []byte) (cogvariants.Dataquery, error) { + dataquery := AzureMonitorQuery{} + + if err := json.Unmarshal(raw, &dataquery); err != nil { + return nil, err + } + + return dataquery, nil + }, + } +} + // Defines the supported queryTypes. GrafanaTemplateVariableFn is deprecated type AzureQueryType string @@ -282,3 +305,140 @@ type AppInsightsMetricNameQueryOrAppInsightsGroupByQueryOrSubscriptionsQueryOrRe WorkspacesQuery *WorkspacesQuery `json:"WorkspacesQuery,omitempty"` UnknownQuery *UnknownQuery `json:"UnknownQuery,omitempty"` } + +func (resource AppInsightsMetricNameQueryOrAppInsightsGroupByQueryOrSubscriptionsQueryOrResourceGroupsQueryOrResourceNamesQueryOrMetricNamespaceQueryOrMetricDefinitionsQueryOrMetricNamesQueryOrWorkspacesQueryOrUnknownQuery) MarshalJSON() ([]byte, error) { + if resource.AppInsightsMetricNameQuery != nil { + return json.Marshal(resource.AppInsightsMetricNameQuery) + } + if resource.AppInsightsGroupByQuery != nil { + return json.Marshal(resource.AppInsightsGroupByQuery) + } + if resource.SubscriptionsQuery != nil { + return json.Marshal(resource.SubscriptionsQuery) + } + if resource.ResourceGroupsQuery != nil { + return json.Marshal(resource.ResourceGroupsQuery) + } + if resource.ResourceNamesQuery != nil { + return json.Marshal(resource.ResourceNamesQuery) + } + if resource.MetricNamespaceQuery != nil { + return json.Marshal(resource.MetricNamespaceQuery) + } + if resource.MetricDefinitionsQuery != nil { + return json.Marshal(resource.MetricDefinitionsQuery) + } + if resource.MetricNamesQuery != nil { + return json.Marshal(resource.MetricNamesQuery) + } + if resource.WorkspacesQuery != nil { + return json.Marshal(resource.WorkspacesQuery) + } + if resource.UnknownQuery != nil { + return json.Marshal(resource.UnknownQuery) + } + + return nil, fmt.Errorf("no value for disjunction of refs") +} + +func (resource *AppInsightsMetricNameQueryOrAppInsightsGroupByQueryOrSubscriptionsQueryOrResourceGroupsQueryOrResourceNamesQueryOrMetricNamespaceQueryOrMetricDefinitionsQueryOrMetricNamesQueryOrWorkspacesQueryOrUnknownQuery) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. + parsedAsMap := make(map[string]any) + if err := json.Unmarshal(raw, &parsedAsMap); err != nil { + return err + } + + discriminator, found := parsedAsMap["kind"] + if !found { + return errors.New("discriminator field 'kind' not found in payload") + } + + switch discriminator { + case "AppInsightsGroupByQuery": + var appInsightsGroupByQuery AppInsightsGroupByQuery + if err := json.Unmarshal(raw, &appInsightsGroupByQuery); err != nil { + return err + } + + resource.AppInsightsGroupByQuery = &appInsightsGroupByQuery + return nil + case "AppInsightsMetricNameQuery": + var appInsightsMetricNameQuery AppInsightsMetricNameQuery + if err := json.Unmarshal(raw, &appInsightsMetricNameQuery); err != nil { + return err + } + + resource.AppInsightsMetricNameQuery = &appInsightsMetricNameQuery + return nil + case "MetricDefinitionsQuery": + var metricDefinitionsQuery MetricDefinitionsQuery + if err := json.Unmarshal(raw, &metricDefinitionsQuery); err != nil { + return err + } + + resource.MetricDefinitionsQuery = &metricDefinitionsQuery + return nil + case "MetricNamesQuery": + var metricNamesQuery MetricNamesQuery + if err := json.Unmarshal(raw, &metricNamesQuery); err != nil { + return err + } + + resource.MetricNamesQuery = &metricNamesQuery + return nil + case "MetricNamespaceQuery": + var metricNamespaceQuery MetricNamespaceQuery + if err := json.Unmarshal(raw, &metricNamespaceQuery); err != nil { + return err + } + + resource.MetricNamespaceQuery = &metricNamespaceQuery + return nil + case "ResourceGroupsQuery": + var resourceGroupsQuery ResourceGroupsQuery + if err := json.Unmarshal(raw, &resourceGroupsQuery); err != nil { + return err + } + + resource.ResourceGroupsQuery = &resourceGroupsQuery + return nil + case "ResourceNamesQuery": + var resourceNamesQuery ResourceNamesQuery + if err := json.Unmarshal(raw, &resourceNamesQuery); err != nil { + return err + } + + resource.ResourceNamesQuery = &resourceNamesQuery + return nil + case "SubscriptionsQuery": + var subscriptionsQuery SubscriptionsQuery + if err := json.Unmarshal(raw, &subscriptionsQuery); err != nil { + return err + } + + resource.SubscriptionsQuery = &subscriptionsQuery + return nil + case "UnknownQuery": + var unknownQuery UnknownQuery + if err := json.Unmarshal(raw, &unknownQuery); err != nil { + return err + } + + resource.UnknownQuery = &unknownQuery + return nil + case "WorkspacesQuery": + var workspacesQuery WorkspacesQuery + if err := json.Unmarshal(raw, &workspacesQuery); err != nil { + return err + } + + resource.WorkspacesQuery = &workspacesQuery + return nil + } + + return fmt.Errorf("could not unmarshal resource with `kind = %v`", discriminator) +} diff --git a/go/azuremonitor/types_json_marshalling_gen.go b/go/azuremonitor/types_json_marshalling_gen.go deleted file mode 100644 index ba51cd0..0000000 --- a/go/azuremonitor/types_json_marshalling_gen.go +++ /dev/null @@ -1,162 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package azuremonitor - -import ( - "encoding/json" - "errors" - "fmt" - - cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" -) - -func VariantConfig() cogvariants.DataqueryConfig { - return cogvariants.DataqueryConfig{ - Identifier: "grafana-azure-monitor-datasource", - DataqueryUnmarshaler: func(raw []byte) (cogvariants.Dataquery, error) { - dataquery := AzureMonitorQuery{} - - if err := json.Unmarshal(raw, &dataquery); err != nil { - return nil, err - } - - return dataquery, nil - }, - } -} - -func (resource AppInsightsMetricNameQueryOrAppInsightsGroupByQueryOrSubscriptionsQueryOrResourceGroupsQueryOrResourceNamesQueryOrMetricNamespaceQueryOrMetricDefinitionsQueryOrMetricNamesQueryOrWorkspacesQueryOrUnknownQuery) MarshalJSON() ([]byte, error) { - if resource.AppInsightsMetricNameQuery != nil { - return json.Marshal(resource.AppInsightsMetricNameQuery) - } - if resource.AppInsightsGroupByQuery != nil { - return json.Marshal(resource.AppInsightsGroupByQuery) - } - if resource.SubscriptionsQuery != nil { - return json.Marshal(resource.SubscriptionsQuery) - } - if resource.ResourceGroupsQuery != nil { - return json.Marshal(resource.ResourceGroupsQuery) - } - if resource.ResourceNamesQuery != nil { - return json.Marshal(resource.ResourceNamesQuery) - } - if resource.MetricNamespaceQuery != nil { - return json.Marshal(resource.MetricNamespaceQuery) - } - if resource.MetricDefinitionsQuery != nil { - return json.Marshal(resource.MetricDefinitionsQuery) - } - if resource.MetricNamesQuery != nil { - return json.Marshal(resource.MetricNamesQuery) - } - if resource.WorkspacesQuery != nil { - return json.Marshal(resource.WorkspacesQuery) - } - if resource.UnknownQuery != nil { - return json.Marshal(resource.UnknownQuery) - } - - return nil, fmt.Errorf("no value for disjunction of refs") -} -func (resource *AppInsightsMetricNameQueryOrAppInsightsGroupByQueryOrSubscriptionsQueryOrResourceGroupsQueryOrResourceNamesQueryOrMetricNamespaceQueryOrMetricDefinitionsQueryOrMetricNamesQueryOrWorkspacesQueryOrUnknownQuery) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. - parsedAsMap := make(map[string]any) - if err := json.Unmarshal(raw, &parsedAsMap); err != nil { - return err - } - - discriminator, found := parsedAsMap["kind"] - if !found { - return errors.New("discriminator field 'kind' not found in payload") - } - - switch discriminator { - case "AppInsightsGroupByQuery": - var appInsightsGroupByQuery AppInsightsGroupByQuery - if err := json.Unmarshal(raw, &appInsightsGroupByQuery); err != nil { - return err - } - - resource.AppInsightsGroupByQuery = &appInsightsGroupByQuery - return nil - case "AppInsightsMetricNameQuery": - var appInsightsMetricNameQuery AppInsightsMetricNameQuery - if err := json.Unmarshal(raw, &appInsightsMetricNameQuery); err != nil { - return err - } - - resource.AppInsightsMetricNameQuery = &appInsightsMetricNameQuery - return nil - case "MetricDefinitionsQuery": - var metricDefinitionsQuery MetricDefinitionsQuery - if err := json.Unmarshal(raw, &metricDefinitionsQuery); err != nil { - return err - } - - resource.MetricDefinitionsQuery = &metricDefinitionsQuery - return nil - case "MetricNamesQuery": - var metricNamesQuery MetricNamesQuery - if err := json.Unmarshal(raw, &metricNamesQuery); err != nil { - return err - } - - resource.MetricNamesQuery = &metricNamesQuery - return nil - case "MetricNamespaceQuery": - var metricNamespaceQuery MetricNamespaceQuery - if err := json.Unmarshal(raw, &metricNamespaceQuery); err != nil { - return err - } - - resource.MetricNamespaceQuery = &metricNamespaceQuery - return nil - case "ResourceGroupsQuery": - var resourceGroupsQuery ResourceGroupsQuery - if err := json.Unmarshal(raw, &resourceGroupsQuery); err != nil { - return err - } - - resource.ResourceGroupsQuery = &resourceGroupsQuery - return nil - case "ResourceNamesQuery": - var resourceNamesQuery ResourceNamesQuery - if err := json.Unmarshal(raw, &resourceNamesQuery); err != nil { - return err - } - - resource.ResourceNamesQuery = &resourceNamesQuery - return nil - case "SubscriptionsQuery": - var subscriptionsQuery SubscriptionsQuery - if err := json.Unmarshal(raw, &subscriptionsQuery); err != nil { - return err - } - - resource.SubscriptionsQuery = &subscriptionsQuery - return nil - case "UnknownQuery": - var unknownQuery UnknownQuery - if err := json.Unmarshal(raw, &unknownQuery); err != nil { - return err - } - - resource.UnknownQuery = &unknownQuery - return nil - case "WorkspacesQuery": - var workspacesQuery WorkspacesQuery - if err := json.Unmarshal(raw, &workspacesQuery); err != nil { - return err - } - - resource.WorkspacesQuery = &workspacesQuery - return nil - } - - return fmt.Errorf("could not unmarshal resource with `kind = %v`", discriminator) -} diff --git a/go/barchart/types_gen.go b/go/barchart/types_gen.go index 26c1fce..6c1970c 100644 --- a/go/barchart/types_gen.go +++ b/go/barchart/types_gen.go @@ -3,6 +3,9 @@ package barchart import ( + "encoding/json" + + cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" common "github.com/grafana/grafana-foundation-sdk/go/common" ) @@ -60,3 +63,27 @@ type FieldConfig struct { ThresholdsStyle *common.GraphThresholdsStyleConfig `json:"thresholdsStyle,omitempty"` AxisBorderShow *bool `json:"axisBorderShow,omitempty"` } + +func VariantConfig() cogvariants.PanelcfgConfig { + return cogvariants.PanelcfgConfig{ + Identifier: "barchart", + OptionsUnmarshaler: func(raw []byte) (any, error) { + options := Options{} + + if err := json.Unmarshal(raw, &options); err != nil { + return nil, err + } + + return options, nil + }, + FieldConfigUnmarshaler: func(raw []byte) (any, error) { + fieldConfig := FieldConfig{} + + if err := json.Unmarshal(raw, &fieldConfig); err != nil { + return nil, err + } + + return fieldConfig, nil + }, + } +} diff --git a/go/barchart/types_json_marshalling_gen.go b/go/barchart/types_json_marshalling_gen.go deleted file mode 100644 index 2896b62..0000000 --- a/go/barchart/types_json_marshalling_gen.go +++ /dev/null @@ -1,33 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package barchart - -import ( - "encoding/json" - - cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" -) - -func VariantConfig() cogvariants.PanelcfgConfig { - return cogvariants.PanelcfgConfig{ - Identifier: "barchart", - OptionsUnmarshaler: func(raw []byte) (any, error) { - options := Options{} - - if err := json.Unmarshal(raw, &options); err != nil { - return nil, err - } - - return options, nil - }, - FieldConfigUnmarshaler: func(raw []byte) (any, error) { - fieldConfig := FieldConfig{} - - if err := json.Unmarshal(raw, &fieldConfig); err != nil { - return nil, err - } - - return fieldConfig, nil - }, - } -} diff --git a/go/bargauge/types_gen.go b/go/bargauge/types_gen.go index 456c9bf..2ab483a 100644 --- a/go/bargauge/types_gen.go +++ b/go/bargauge/types_gen.go @@ -3,6 +3,9 @@ package bargauge import ( + "encoding/json" + + cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" common "github.com/grafana/grafana-foundation-sdk/go/common" ) @@ -19,3 +22,18 @@ type Options struct { MaxVizHeight uint32 `json:"maxVizHeight"` Orientation common.VizOrientation `json:"orientation"` } + +func VariantConfig() cogvariants.PanelcfgConfig { + return cogvariants.PanelcfgConfig{ + Identifier: "bargauge", + OptionsUnmarshaler: func(raw []byte) (any, error) { + options := Options{} + + if err := json.Unmarshal(raw, &options); err != nil { + return nil, err + } + + return options, nil + }, + } +} diff --git a/go/bargauge/types_json_marshalling_gen.go b/go/bargauge/types_json_marshalling_gen.go deleted file mode 100644 index 7c90968..0000000 --- a/go/bargauge/types_json_marshalling_gen.go +++ /dev/null @@ -1,24 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package bargauge - -import ( - "encoding/json" - - cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" -) - -func VariantConfig() cogvariants.PanelcfgConfig { - return cogvariants.PanelcfgConfig{ - Identifier: "bargauge", - OptionsUnmarshaler: func(raw []byte) (any, error) { - options := Options{} - - if err := json.Unmarshal(raw, &options); err != nil { - return nil, err - } - - return options, nil - }, - } -} diff --git a/go/candlestick/types_gen.go b/go/candlestick/types_gen.go index a51fd99..0b9ef70 100644 --- a/go/candlestick/types_gen.go +++ b/go/candlestick/types_gen.go @@ -3,6 +3,9 @@ package candlestick import ( + "encoding/json" + + cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" common "github.com/grafana/grafana-foundation-sdk/go/common" ) @@ -65,3 +68,27 @@ type Options struct { } type FieldConfig = common.GraphFieldConfig + +func VariantConfig() cogvariants.PanelcfgConfig { + return cogvariants.PanelcfgConfig{ + Identifier: "candlestick", + OptionsUnmarshaler: func(raw []byte) (any, error) { + options := Options{} + + if err := json.Unmarshal(raw, &options); err != nil { + return nil, err + } + + return options, nil + }, + FieldConfigUnmarshaler: func(raw []byte) (any, error) { + fieldConfig := FieldConfig{} + + if err := json.Unmarshal(raw, &fieldConfig); err != nil { + return nil, err + } + + return fieldConfig, nil + }, + } +} diff --git a/go/candlestick/types_json_marshalling_gen.go b/go/candlestick/types_json_marshalling_gen.go deleted file mode 100644 index f26dd4d..0000000 --- a/go/candlestick/types_json_marshalling_gen.go +++ /dev/null @@ -1,33 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package candlestick - -import ( - "encoding/json" - - cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" -) - -func VariantConfig() cogvariants.PanelcfgConfig { - return cogvariants.PanelcfgConfig{ - Identifier: "candlestick", - OptionsUnmarshaler: func(raw []byte) (any, error) { - options := Options{} - - if err := json.Unmarshal(raw, &options); err != nil { - return nil, err - } - - return options, nil - }, - FieldConfigUnmarshaler: func(raw []byte) (any, error) { - fieldConfig := FieldConfig{} - - if err := json.Unmarshal(raw, &fieldConfig); err != nil { - return nil, err - } - - return fieldConfig, nil - }, - } -} diff --git a/go/canvas/types_gen.go b/go/canvas/types_gen.go index 3e37f11..ebfb670 100644 --- a/go/canvas/types_gen.go +++ b/go/canvas/types_gen.go @@ -3,6 +3,9 @@ package canvas import ( + "encoding/json" + + cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" common "github.com/grafana/grafana-foundation-sdk/go/common" ) @@ -126,3 +129,18 @@ type Options struct { Elements []CanvasElementOptions `json:"elements"` } `json:"root"` } + +func VariantConfig() cogvariants.PanelcfgConfig { + return cogvariants.PanelcfgConfig{ + Identifier: "canvas", + OptionsUnmarshaler: func(raw []byte) (any, error) { + options := Options{} + + if err := json.Unmarshal(raw, &options); err != nil { + return nil, err + } + + return options, nil + }, + } +} diff --git a/go/canvas/types_json_marshalling_gen.go b/go/canvas/types_json_marshalling_gen.go deleted file mode 100644 index f2f980f..0000000 --- a/go/canvas/types_json_marshalling_gen.go +++ /dev/null @@ -1,24 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package canvas - -import ( - "encoding/json" - - cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" -) - -func VariantConfig() cogvariants.PanelcfgConfig { - return cogvariants.PanelcfgConfig{ - Identifier: "canvas", - OptionsUnmarshaler: func(raw []byte) (any, error) { - options := Options{} - - if err := json.Unmarshal(raw, &options); err != nil { - return nil, err - } - - return options, nil - }, - } -} diff --git a/go/cloudwatch/types_gen.go b/go/cloudwatch/types_gen.go index 8fe1bc2..6f32fe2 100644 --- a/go/cloudwatch/types_gen.go +++ b/go/cloudwatch/types_gen.go @@ -2,6 +2,14 @@ package cloudwatch +import ( + "encoding/json" + "errors" + "fmt" + + cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" +) + type MetricStat struct { // AWS region to query for the metric Region string `json:"region"` @@ -292,6 +300,21 @@ type CloudWatchQuery = CloudWatchMetricsQueryOrCloudWatchLogsQueryOrCloudWatchAn func (resource CloudWatchQuery) ImplementsDataqueryVariant() {} +func VariantConfig() cogvariants.DataqueryConfig { + return cogvariants.DataqueryConfig{ + Identifier: "cloudwatch", + DataqueryUnmarshaler: func(raw []byte) (cogvariants.Dataquery, error) { + dataquery := CloudWatchQuery{} + + if err := json.Unmarshal(raw, &dataquery); err != nil { + return nil, err + } + + return dataquery, nil + }, + } +} + type QueryEditorArrayExpressionType string const ( @@ -304,11 +327,102 @@ type StringOrArrayOfString struct { ArrayOfString []string `json:"ArrayOfString,omitempty"` } +func (resource StringOrArrayOfString) MarshalJSON() ([]byte, error) { + if resource.String != nil { + return json.Marshal(resource.String) + } + + if resource.ArrayOfString != nil { + return json.Marshal(resource.ArrayOfString) + } + + return nil, fmt.Errorf("no value for disjunction of scalars") +} + +func (resource *StringOrArrayOfString) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + var errList []error + + // String + var String string + if err := json.Unmarshal(raw, &String); err != nil { + errList = append(errList, err) + resource.String = nil + } else { + resource.String = &String + return nil + } + + // ArrayOfString + var ArrayOfString []string + if err := json.Unmarshal(raw, &ArrayOfString); err != nil { + errList = append(errList, err) + resource.ArrayOfString = nil + } else { + resource.ArrayOfString = ArrayOfString + return nil + } + + return errors.Join(errList...) +} + type QueryEditorPropertyExpressionOrQueryEditorFunctionExpression struct { QueryEditorPropertyExpression *QueryEditorPropertyExpression `json:"QueryEditorPropertyExpression,omitempty"` QueryEditorFunctionExpression *QueryEditorFunctionExpression `json:"QueryEditorFunctionExpression,omitempty"` } +func (resource QueryEditorPropertyExpressionOrQueryEditorFunctionExpression) MarshalJSON() ([]byte, error) { + if resource.QueryEditorPropertyExpression != nil { + return json.Marshal(resource.QueryEditorPropertyExpression) + } + if resource.QueryEditorFunctionExpression != nil { + return json.Marshal(resource.QueryEditorFunctionExpression) + } + + return nil, fmt.Errorf("no value for disjunction of refs") +} + +func (resource *QueryEditorPropertyExpressionOrQueryEditorFunctionExpression) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. + parsedAsMap := make(map[string]any) + if err := json.Unmarshal(raw, &parsedAsMap); err != nil { + return err + } + + discriminator, found := parsedAsMap["type"] + if !found { + return errors.New("discriminator field 'type' not found in payload") + } + + switch discriminator { + case "function": + var queryEditorFunctionExpression QueryEditorFunctionExpression + if err := json.Unmarshal(raw, &queryEditorFunctionExpression); err != nil { + return err + } + + resource.QueryEditorFunctionExpression = &queryEditorFunctionExpression + return nil + case "property": + var queryEditorPropertyExpression QueryEditorPropertyExpression + if err := json.Unmarshal(raw, &queryEditorPropertyExpression); err != nil { + return err + } + + resource.QueryEditorPropertyExpression = &queryEditorPropertyExpression + return nil + } + + return fmt.Errorf("could not unmarshal resource with `type = %v`", discriminator) +} + type StringOrBoolOrInt64OrArrayOfQueryEditorOperatorType struct { String *string `json:"String,omitempty"` Bool *bool `json:"Bool,omitempty"` @@ -316,12 +430,138 @@ type StringOrBoolOrInt64OrArrayOfQueryEditorOperatorType struct { ArrayOfQueryEditorOperatorType []QueryEditorOperatorType `json:"ArrayOfQueryEditorOperatorType,omitempty"` } +func (resource StringOrBoolOrInt64OrArrayOfQueryEditorOperatorType) MarshalJSON() ([]byte, error) { + if resource.String != nil { + return json.Marshal(resource.String) + } + + if resource.Bool != nil { + return json.Marshal(resource.Bool) + } + + if resource.Int64 != nil { + return json.Marshal(resource.Int64) + } + + if resource.ArrayOfQueryEditorOperatorType != nil { + return json.Marshal(resource.ArrayOfQueryEditorOperatorType) + } + + return nil, fmt.Errorf("no value for disjunction of scalars") +} + +func (resource *StringOrBoolOrInt64OrArrayOfQueryEditorOperatorType) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + var errList []error + + // String + var String string + if err := json.Unmarshal(raw, &String); err != nil { + errList = append(errList, err) + resource.String = nil + } else { + resource.String = &String + return nil + } + + // Bool + var Bool bool + if err := json.Unmarshal(raw, &Bool); err != nil { + errList = append(errList, err) + resource.Bool = nil + } else { + resource.Bool = &Bool + return nil + } + + // Int64 + var Int64 int64 + if err := json.Unmarshal(raw, &Int64); err != nil { + errList = append(errList, err) + resource.Int64 = nil + } else { + resource.Int64 = &Int64 + return nil + } + + // ArrayOfQueryEditorOperatorType + var ArrayOfQueryEditorOperatorType []QueryEditorOperatorType + if err := json.Unmarshal(raw, &ArrayOfQueryEditorOperatorType); err != nil { + errList = append(errList, err) + resource.ArrayOfQueryEditorOperatorType = nil + } else { + resource.ArrayOfQueryEditorOperatorType = ArrayOfQueryEditorOperatorType + return nil + } + + return errors.Join(errList...) +} + type StringOrBoolOrInt64 struct { String *string `json:"String,omitempty"` Bool *bool `json:"Bool,omitempty"` Int64 *int64 `json:"Int64,omitempty"` } +func (resource StringOrBoolOrInt64) MarshalJSON() ([]byte, error) { + if resource.String != nil { + return json.Marshal(resource.String) + } + + if resource.Bool != nil { + return json.Marshal(resource.Bool) + } + + if resource.Int64 != nil { + return json.Marshal(resource.Int64) + } + + return nil, fmt.Errorf("no value for disjunction of scalars") +} + +func (resource *StringOrBoolOrInt64) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + var errList []error + + // String + var String string + if err := json.Unmarshal(raw, &String); err != nil { + errList = append(errList, err) + resource.String = nil + } else { + resource.String = &String + return nil + } + + // Bool + var Bool bool + if err := json.Unmarshal(raw, &Bool); err != nil { + errList = append(errList, err) + resource.Bool = nil + } else { + resource.Bool = &Bool + return nil + } + + // Int64 + var Int64 int64 + if err := json.Unmarshal(raw, &Int64); err != nil { + errList = append(errList, err) + resource.Int64 = nil + } else { + resource.Int64 = &Int64 + return nil + } + + return errors.Join(errList...) +} + type QueryEditorArrayExpressionOrQueryEditorPropertyExpressionOrQueryEditorGroupByExpressionOrQueryEditorFunctionExpressionOrQueryEditorFunctionParameterExpressionOrQueryEditorOperatorExpression struct { QueryEditorArrayExpression *QueryEditorArrayExpression `json:"QueryEditorArrayExpression,omitempty"` QueryEditorPropertyExpression *QueryEditorPropertyExpression `json:"QueryEditorPropertyExpression,omitempty"` @@ -331,8 +571,169 @@ type QueryEditorArrayExpressionOrQueryEditorPropertyExpressionOrQueryEditorGroup QueryEditorOperatorExpression *QueryEditorOperatorExpression `json:"QueryEditorOperatorExpression,omitempty"` } +func (resource QueryEditorArrayExpressionOrQueryEditorPropertyExpressionOrQueryEditorGroupByExpressionOrQueryEditorFunctionExpressionOrQueryEditorFunctionParameterExpressionOrQueryEditorOperatorExpression) MarshalJSON() ([]byte, error) { + if resource.QueryEditorArrayExpression != nil { + return json.Marshal(resource.QueryEditorArrayExpression) + } + if resource.QueryEditorPropertyExpression != nil { + return json.Marshal(resource.QueryEditorPropertyExpression) + } + if resource.QueryEditorGroupByExpression != nil { + return json.Marshal(resource.QueryEditorGroupByExpression) + } + if resource.QueryEditorFunctionExpression != nil { + return json.Marshal(resource.QueryEditorFunctionExpression) + } + if resource.QueryEditorFunctionParameterExpression != nil { + return json.Marshal(resource.QueryEditorFunctionParameterExpression) + } + if resource.QueryEditorOperatorExpression != nil { + return json.Marshal(resource.QueryEditorOperatorExpression) + } + + return nil, fmt.Errorf("no value for disjunction of refs") +} + +func (resource *QueryEditorArrayExpressionOrQueryEditorPropertyExpressionOrQueryEditorGroupByExpressionOrQueryEditorFunctionExpressionOrQueryEditorFunctionParameterExpressionOrQueryEditorOperatorExpression) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. + parsedAsMap := make(map[string]any) + if err := json.Unmarshal(raw, &parsedAsMap); err != nil { + return err + } + + discriminator, found := parsedAsMap["type"] + if !found { + return errors.New("discriminator field 'type' not found in payload") + } + + switch discriminator { + case "and": + var queryEditorArrayExpression QueryEditorArrayExpression + if err := json.Unmarshal(raw, &queryEditorArrayExpression); err != nil { + return err + } + + resource.QueryEditorArrayExpression = &queryEditorArrayExpression + return nil + case "function": + var queryEditorFunctionExpression QueryEditorFunctionExpression + if err := json.Unmarshal(raw, &queryEditorFunctionExpression); err != nil { + return err + } + + resource.QueryEditorFunctionExpression = &queryEditorFunctionExpression + return nil + case "functionParameter": + var queryEditorFunctionParameterExpression QueryEditorFunctionParameterExpression + if err := json.Unmarshal(raw, &queryEditorFunctionParameterExpression); err != nil { + return err + } + + resource.QueryEditorFunctionParameterExpression = &queryEditorFunctionParameterExpression + return nil + case "groupBy": + var queryEditorGroupByExpression QueryEditorGroupByExpression + if err := json.Unmarshal(raw, &queryEditorGroupByExpression); err != nil { + return err + } + + resource.QueryEditorGroupByExpression = &queryEditorGroupByExpression + return nil + case "operator": + var queryEditorOperatorExpression QueryEditorOperatorExpression + if err := json.Unmarshal(raw, &queryEditorOperatorExpression); err != nil { + return err + } + + resource.QueryEditorOperatorExpression = &queryEditorOperatorExpression + return nil + case "or": + var queryEditorArrayExpression QueryEditorArrayExpression + if err := json.Unmarshal(raw, &queryEditorArrayExpression); err != nil { + return err + } + + resource.QueryEditorArrayExpression = &queryEditorArrayExpression + return nil + case "property": + var queryEditorPropertyExpression QueryEditorPropertyExpression + if err := json.Unmarshal(raw, &queryEditorPropertyExpression); err != nil { + return err + } + + resource.QueryEditorPropertyExpression = &queryEditorPropertyExpression + return nil + } + + return fmt.Errorf("could not unmarshal resource with `type = %v`", discriminator) +} + type CloudWatchMetricsQueryOrCloudWatchLogsQueryOrCloudWatchAnnotationQuery struct { CloudWatchMetricsQuery *CloudWatchMetricsQuery `json:"CloudWatchMetricsQuery,omitempty"` CloudWatchLogsQuery *CloudWatchLogsQuery `json:"CloudWatchLogsQuery,omitempty"` CloudWatchAnnotationQuery *CloudWatchAnnotationQuery `json:"CloudWatchAnnotationQuery,omitempty"` } + +func (resource CloudWatchMetricsQueryOrCloudWatchLogsQueryOrCloudWatchAnnotationQuery) MarshalJSON() ([]byte, error) { + if resource.CloudWatchMetricsQuery != nil { + return json.Marshal(resource.CloudWatchMetricsQuery) + } + if resource.CloudWatchLogsQuery != nil { + return json.Marshal(resource.CloudWatchLogsQuery) + } + if resource.CloudWatchAnnotationQuery != nil { + return json.Marshal(resource.CloudWatchAnnotationQuery) + } + + return nil, fmt.Errorf("no value for disjunction of refs") +} + +func (resource *CloudWatchMetricsQueryOrCloudWatchLogsQueryOrCloudWatchAnnotationQuery) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. + parsedAsMap := make(map[string]any) + if err := json.Unmarshal(raw, &parsedAsMap); err != nil { + return err + } + + discriminator, found := parsedAsMap["queryMode"] + if !found { + return errors.New("discriminator field 'queryMode' not found in payload") + } + + switch discriminator { + case "Annotations": + var cloudWatchAnnotationQuery CloudWatchAnnotationQuery + if err := json.Unmarshal(raw, &cloudWatchAnnotationQuery); err != nil { + return err + } + + resource.CloudWatchAnnotationQuery = &cloudWatchAnnotationQuery + return nil + case "Logs": + var cloudWatchLogsQuery CloudWatchLogsQuery + if err := json.Unmarshal(raw, &cloudWatchLogsQuery); err != nil { + return err + } + + resource.CloudWatchLogsQuery = &cloudWatchLogsQuery + return nil + case "Metrics": + var cloudWatchMetricsQuery CloudWatchMetricsQuery + if err := json.Unmarshal(raw, &cloudWatchMetricsQuery); err != nil { + return err + } + + resource.CloudWatchMetricsQuery = &cloudWatchMetricsQuery + return nil + } + + return fmt.Errorf("could not unmarshal resource with `queryMode = %v`", discriminator) +} diff --git a/go/cloudwatch/types_json_marshalling_gen.go b/go/cloudwatch/types_json_marshalling_gen.go deleted file mode 100644 index 0f099b3..0000000 --- a/go/cloudwatch/types_json_marshalling_gen.go +++ /dev/null @@ -1,401 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package cloudwatch - -import ( - "encoding/json" - "errors" - "fmt" - - cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" -) - -func VariantConfig() cogvariants.DataqueryConfig { - return cogvariants.DataqueryConfig{ - Identifier: "cloudwatch", - DataqueryUnmarshaler: func(raw []byte) (cogvariants.Dataquery, error) { - dataquery := CloudWatchQuery{} - - if err := json.Unmarshal(raw, &dataquery); err != nil { - return nil, err - } - - return dataquery, nil - }, - } -} - -func (resource StringOrArrayOfString) MarshalJSON() ([]byte, error) { - if resource.String != nil { - return json.Marshal(resource.String) - } - - if resource.ArrayOfString != nil { - return json.Marshal(resource.ArrayOfString) - } - - return nil, fmt.Errorf("no value for disjunction of scalars") -} - -func (resource *StringOrArrayOfString) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - var errList []error - - // String - var String string - if err := json.Unmarshal(raw, &String); err != nil { - errList = append(errList, err) - resource.String = nil - } else { - resource.String = &String - return nil - } - - // ArrayOfString - var ArrayOfString []string - if err := json.Unmarshal(raw, &ArrayOfString); err != nil { - errList = append(errList, err) - resource.ArrayOfString = nil - } else { - resource.ArrayOfString = ArrayOfString - return nil - } - - return errors.Join(errList...) -} - -func (resource QueryEditorPropertyExpressionOrQueryEditorFunctionExpression) MarshalJSON() ([]byte, error) { - if resource.QueryEditorPropertyExpression != nil { - return json.Marshal(resource.QueryEditorPropertyExpression) - } - if resource.QueryEditorFunctionExpression != nil { - return json.Marshal(resource.QueryEditorFunctionExpression) - } - - return nil, fmt.Errorf("no value for disjunction of refs") -} -func (resource *QueryEditorPropertyExpressionOrQueryEditorFunctionExpression) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. - parsedAsMap := make(map[string]any) - if err := json.Unmarshal(raw, &parsedAsMap); err != nil { - return err - } - - discriminator, found := parsedAsMap["type"] - if !found { - return errors.New("discriminator field 'type' not found in payload") - } - - switch discriminator { - case "function": - var queryEditorFunctionExpression QueryEditorFunctionExpression - if err := json.Unmarshal(raw, &queryEditorFunctionExpression); err != nil { - return err - } - - resource.QueryEditorFunctionExpression = &queryEditorFunctionExpression - return nil - case "property": - var queryEditorPropertyExpression QueryEditorPropertyExpression - if err := json.Unmarshal(raw, &queryEditorPropertyExpression); err != nil { - return err - } - - resource.QueryEditorPropertyExpression = &queryEditorPropertyExpression - return nil - } - - return fmt.Errorf("could not unmarshal resource with `type = %v`", discriminator) -} - -func (resource StringOrBoolOrInt64OrArrayOfQueryEditorOperatorType) MarshalJSON() ([]byte, error) { - if resource.String != nil { - return json.Marshal(resource.String) - } - - if resource.Bool != nil { - return json.Marshal(resource.Bool) - } - - if resource.Int64 != nil { - return json.Marshal(resource.Int64) - } - - if resource.ArrayOfQueryEditorOperatorType != nil { - return json.Marshal(resource.ArrayOfQueryEditorOperatorType) - } - - return nil, fmt.Errorf("no value for disjunction of scalars") -} - -func (resource *StringOrBoolOrInt64OrArrayOfQueryEditorOperatorType) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - var errList []error - - // String - var String string - if err := json.Unmarshal(raw, &String); err != nil { - errList = append(errList, err) - resource.String = nil - } else { - resource.String = &String - return nil - } - - // Bool - var Bool bool - if err := json.Unmarshal(raw, &Bool); err != nil { - errList = append(errList, err) - resource.Bool = nil - } else { - resource.Bool = &Bool - return nil - } - - // Int64 - var Int64 int64 - if err := json.Unmarshal(raw, &Int64); err != nil { - errList = append(errList, err) - resource.Int64 = nil - } else { - resource.Int64 = &Int64 - return nil - } - - // ArrayOfQueryEditorOperatorType - var ArrayOfQueryEditorOperatorType []QueryEditorOperatorType - if err := json.Unmarshal(raw, &ArrayOfQueryEditorOperatorType); err != nil { - errList = append(errList, err) - resource.ArrayOfQueryEditorOperatorType = nil - } else { - resource.ArrayOfQueryEditorOperatorType = ArrayOfQueryEditorOperatorType - return nil - } - - return errors.Join(errList...) -} - -func (resource StringOrBoolOrInt64) MarshalJSON() ([]byte, error) { - if resource.String != nil { - return json.Marshal(resource.String) - } - - if resource.Bool != nil { - return json.Marshal(resource.Bool) - } - - if resource.Int64 != nil { - return json.Marshal(resource.Int64) - } - - return nil, fmt.Errorf("no value for disjunction of scalars") -} - -func (resource *StringOrBoolOrInt64) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - var errList []error - - // String - var String string - if err := json.Unmarshal(raw, &String); err != nil { - errList = append(errList, err) - resource.String = nil - } else { - resource.String = &String - return nil - } - - // Bool - var Bool bool - if err := json.Unmarshal(raw, &Bool); err != nil { - errList = append(errList, err) - resource.Bool = nil - } else { - resource.Bool = &Bool - return nil - } - - // Int64 - var Int64 int64 - if err := json.Unmarshal(raw, &Int64); err != nil { - errList = append(errList, err) - resource.Int64 = nil - } else { - resource.Int64 = &Int64 - return nil - } - - return errors.Join(errList...) -} - -func (resource QueryEditorArrayExpressionOrQueryEditorPropertyExpressionOrQueryEditorGroupByExpressionOrQueryEditorFunctionExpressionOrQueryEditorFunctionParameterExpressionOrQueryEditorOperatorExpression) MarshalJSON() ([]byte, error) { - if resource.QueryEditorArrayExpression != nil { - return json.Marshal(resource.QueryEditorArrayExpression) - } - if resource.QueryEditorPropertyExpression != nil { - return json.Marshal(resource.QueryEditorPropertyExpression) - } - if resource.QueryEditorGroupByExpression != nil { - return json.Marshal(resource.QueryEditorGroupByExpression) - } - if resource.QueryEditorFunctionExpression != nil { - return json.Marshal(resource.QueryEditorFunctionExpression) - } - if resource.QueryEditorFunctionParameterExpression != nil { - return json.Marshal(resource.QueryEditorFunctionParameterExpression) - } - if resource.QueryEditorOperatorExpression != nil { - return json.Marshal(resource.QueryEditorOperatorExpression) - } - - return nil, fmt.Errorf("no value for disjunction of refs") -} -func (resource *QueryEditorArrayExpressionOrQueryEditorPropertyExpressionOrQueryEditorGroupByExpressionOrQueryEditorFunctionExpressionOrQueryEditorFunctionParameterExpressionOrQueryEditorOperatorExpression) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. - parsedAsMap := make(map[string]any) - if err := json.Unmarshal(raw, &parsedAsMap); err != nil { - return err - } - - discriminator, found := parsedAsMap["type"] - if !found { - return errors.New("discriminator field 'type' not found in payload") - } - - switch discriminator { - case "and": - var queryEditorArrayExpression QueryEditorArrayExpression - if err := json.Unmarshal(raw, &queryEditorArrayExpression); err != nil { - return err - } - - resource.QueryEditorArrayExpression = &queryEditorArrayExpression - return nil - case "function": - var queryEditorFunctionExpression QueryEditorFunctionExpression - if err := json.Unmarshal(raw, &queryEditorFunctionExpression); err != nil { - return err - } - - resource.QueryEditorFunctionExpression = &queryEditorFunctionExpression - return nil - case "functionParameter": - var queryEditorFunctionParameterExpression QueryEditorFunctionParameterExpression - if err := json.Unmarshal(raw, &queryEditorFunctionParameterExpression); err != nil { - return err - } - - resource.QueryEditorFunctionParameterExpression = &queryEditorFunctionParameterExpression - return nil - case "groupBy": - var queryEditorGroupByExpression QueryEditorGroupByExpression - if err := json.Unmarshal(raw, &queryEditorGroupByExpression); err != nil { - return err - } - - resource.QueryEditorGroupByExpression = &queryEditorGroupByExpression - return nil - case "operator": - var queryEditorOperatorExpression QueryEditorOperatorExpression - if err := json.Unmarshal(raw, &queryEditorOperatorExpression); err != nil { - return err - } - - resource.QueryEditorOperatorExpression = &queryEditorOperatorExpression - return nil - case "or": - var queryEditorArrayExpression QueryEditorArrayExpression - if err := json.Unmarshal(raw, &queryEditorArrayExpression); err != nil { - return err - } - - resource.QueryEditorArrayExpression = &queryEditorArrayExpression - return nil - case "property": - var queryEditorPropertyExpression QueryEditorPropertyExpression - if err := json.Unmarshal(raw, &queryEditorPropertyExpression); err != nil { - return err - } - - resource.QueryEditorPropertyExpression = &queryEditorPropertyExpression - return nil - } - - return fmt.Errorf("could not unmarshal resource with `type = %v`", discriminator) -} - -func (resource CloudWatchMetricsQueryOrCloudWatchLogsQueryOrCloudWatchAnnotationQuery) MarshalJSON() ([]byte, error) { - if resource.CloudWatchMetricsQuery != nil { - return json.Marshal(resource.CloudWatchMetricsQuery) - } - if resource.CloudWatchLogsQuery != nil { - return json.Marshal(resource.CloudWatchLogsQuery) - } - if resource.CloudWatchAnnotationQuery != nil { - return json.Marshal(resource.CloudWatchAnnotationQuery) - } - - return nil, fmt.Errorf("no value for disjunction of refs") -} -func (resource *CloudWatchMetricsQueryOrCloudWatchLogsQueryOrCloudWatchAnnotationQuery) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. - parsedAsMap := make(map[string]any) - if err := json.Unmarshal(raw, &parsedAsMap); err != nil { - return err - } - - discriminator, found := parsedAsMap["queryMode"] - if !found { - return errors.New("discriminator field 'queryMode' not found in payload") - } - - switch discriminator { - case "Annotations": - var cloudWatchAnnotationQuery CloudWatchAnnotationQuery - if err := json.Unmarshal(raw, &cloudWatchAnnotationQuery); err != nil { - return err - } - - resource.CloudWatchAnnotationQuery = &cloudWatchAnnotationQuery - return nil - case "Logs": - var cloudWatchLogsQuery CloudWatchLogsQuery - if err := json.Unmarshal(raw, &cloudWatchLogsQuery); err != nil { - return err - } - - resource.CloudWatchLogsQuery = &cloudWatchLogsQuery - return nil - case "Metrics": - var cloudWatchMetricsQuery CloudWatchMetricsQuery - if err := json.Unmarshal(raw, &cloudWatchMetricsQuery); err != nil { - return err - } - - resource.CloudWatchMetricsQuery = &cloudWatchMetricsQuery - return nil - } - - return fmt.Errorf("could not unmarshal resource with `queryMode = %v`", discriminator) -} diff --git a/go/common/types_gen.go b/go/common/types_gen.go index 4cb5641..515fd60 100644 --- a/go/common/types_gen.go +++ b/go/common/types_gen.go @@ -2,6 +2,12 @@ package common +import ( + "encoding/json" + "errors" + "fmt" +) + // A topic is attached to DataFrame metadata in query results. // This specifies where the data should be used. type DataTopic string @@ -486,6 +492,15 @@ const ( BigValueTextModeNone BigValueTextMode = "none" ) +// TODO docs +type PercentChangeColorMode string + +const ( + PercentChangeColorModeStandard PercentChangeColorMode = "standard" + PercentChangeColorModeInverted PercentChangeColorMode = "inverted" + PercentChangeColorModeSameAsValue PercentChangeColorMode = "same_as_value" +) + // TODO -- should not be table specific! // TODO docs type FieldTextAlignment string @@ -892,6 +907,48 @@ type BoolOrFloat64 struct { Float64 *float64 `json:"Float64,omitempty"` } +func (resource BoolOrFloat64) MarshalJSON() ([]byte, error) { + if resource.Bool != nil { + return json.Marshal(resource.Bool) + } + + if resource.Float64 != nil { + return json.Marshal(resource.Float64) + } + + return nil, fmt.Errorf("no value for disjunction of scalars") +} + +func (resource *BoolOrFloat64) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + var errList []error + + // Bool + var Bool bool + if err := json.Unmarshal(raw, &Bool); err != nil { + errList = append(errList, err) + resource.Bool = nil + } else { + resource.Bool = &Bool + return nil + } + + // Float64 + var Float64 float64 + if err := json.Unmarshal(raw, &Float64); err != nil { + errList = append(errList, err) + resource.Float64 = nil + } else { + resource.Float64 = &Float64 + return nil + } + + return errors.Join(errList...) +} + type TableAutoCellOptionsOrTableSparklineCellOptionsOrTableBarGaugeCellOptionsOrTableColoredBackgroundCellOptionsOrTableColorTextCellOptionsOrTableImageCellOptionsOrTableDataLinksCellOptionsOrTableJsonViewCellOptions struct { TableAutoCellOptions *TableAutoCellOptions `json:"TableAutoCellOptions,omitempty"` TableSparklineCellOptions *TableSparklineCellOptions `json:"TableSparklineCellOptions,omitempty"` @@ -902,3 +959,118 @@ type TableAutoCellOptionsOrTableSparklineCellOptionsOrTableBarGaugeCellOptionsOr TableDataLinksCellOptions *TableDataLinksCellOptions `json:"TableDataLinksCellOptions,omitempty"` TableJsonViewCellOptions *TableJsonViewCellOptions `json:"TableJsonViewCellOptions,omitempty"` } + +func (resource TableAutoCellOptionsOrTableSparklineCellOptionsOrTableBarGaugeCellOptionsOrTableColoredBackgroundCellOptionsOrTableColorTextCellOptionsOrTableImageCellOptionsOrTableDataLinksCellOptionsOrTableJsonViewCellOptions) MarshalJSON() ([]byte, error) { + if resource.TableAutoCellOptions != nil { + return json.Marshal(resource.TableAutoCellOptions) + } + if resource.TableSparklineCellOptions != nil { + return json.Marshal(resource.TableSparklineCellOptions) + } + if resource.TableBarGaugeCellOptions != nil { + return json.Marshal(resource.TableBarGaugeCellOptions) + } + if resource.TableColoredBackgroundCellOptions != nil { + return json.Marshal(resource.TableColoredBackgroundCellOptions) + } + if resource.TableColorTextCellOptions != nil { + return json.Marshal(resource.TableColorTextCellOptions) + } + if resource.TableImageCellOptions != nil { + return json.Marshal(resource.TableImageCellOptions) + } + if resource.TableDataLinksCellOptions != nil { + return json.Marshal(resource.TableDataLinksCellOptions) + } + if resource.TableJsonViewCellOptions != nil { + return json.Marshal(resource.TableJsonViewCellOptions) + } + + return nil, fmt.Errorf("no value for disjunction of refs") +} + +func (resource *TableAutoCellOptionsOrTableSparklineCellOptionsOrTableBarGaugeCellOptionsOrTableColoredBackgroundCellOptionsOrTableColorTextCellOptionsOrTableImageCellOptionsOrTableDataLinksCellOptionsOrTableJsonViewCellOptions) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + + // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. + parsedAsMap := make(map[string]any) + if err := json.Unmarshal(raw, &parsedAsMap); err != nil { + return err + } + + discriminator, found := parsedAsMap["type"] + if !found { + return errors.New("discriminator field 'type' not found in payload") + } + + switch discriminator { + case "auto": + var tableAutoCellOptions TableAutoCellOptions + if err := json.Unmarshal(raw, &tableAutoCellOptions); err != nil { + return err + } + + resource.TableAutoCellOptions = &tableAutoCellOptions + return nil + case "color-background": + var tableColoredBackgroundCellOptions TableColoredBackgroundCellOptions + if err := json.Unmarshal(raw, &tableColoredBackgroundCellOptions); err != nil { + return err + } + + resource.TableColoredBackgroundCellOptions = &tableColoredBackgroundCellOptions + return nil + case "color-text": + var tableColorTextCellOptions TableColorTextCellOptions + if err := json.Unmarshal(raw, &tableColorTextCellOptions); err != nil { + return err + } + + resource.TableColorTextCellOptions = &tableColorTextCellOptions + return nil + case "data-links": + var tableDataLinksCellOptions TableDataLinksCellOptions + if err := json.Unmarshal(raw, &tableDataLinksCellOptions); err != nil { + return err + } + + resource.TableDataLinksCellOptions = &tableDataLinksCellOptions + return nil + case "gauge": + var tableBarGaugeCellOptions TableBarGaugeCellOptions + if err := json.Unmarshal(raw, &tableBarGaugeCellOptions); err != nil { + return err + } + + resource.TableBarGaugeCellOptions = &tableBarGaugeCellOptions + return nil + case "image": + var tableImageCellOptions TableImageCellOptions + if err := json.Unmarshal(raw, &tableImageCellOptions); err != nil { + return err + } + + resource.TableImageCellOptions = &tableImageCellOptions + return nil + case "json-view": + var tableJsonViewCellOptions TableJsonViewCellOptions + if err := json.Unmarshal(raw, &tableJsonViewCellOptions); err != nil { + return err + } + + resource.TableJsonViewCellOptions = &tableJsonViewCellOptions + return nil + case "sparkline": + var tableSparklineCellOptions TableSparklineCellOptions + if err := json.Unmarshal(raw, &tableSparklineCellOptions); err != nil { + return err + } + + resource.TableSparklineCellOptions = &tableSparklineCellOptions + return nil + } + + return fmt.Errorf("could not unmarshal resource with `type = %v`", discriminator) +} diff --git a/go/common/types_json_marshalling_gen.go b/go/common/types_json_marshalling_gen.go deleted file mode 100644 index 8a380db..0000000 --- a/go/common/types_json_marshalling_gen.go +++ /dev/null @@ -1,165 +0,0 @@ -// Code generated - EDITING IS FUTILE. DO NOT EDIT. - -package common - -import ( - "encoding/json" - "errors" - "fmt" -) - -func (resource BoolOrFloat64) MarshalJSON() ([]byte, error) { - if resource.Bool != nil { - return json.Marshal(resource.Bool) - } - - if resource.Float64 != nil { - return json.Marshal(resource.Float64) - } - - return nil, fmt.Errorf("no value for disjunction of scalars") -} - -func (resource *BoolOrFloat64) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - var errList []error - - // Bool - var Bool bool - if err := json.Unmarshal(raw, &Bool); err != nil { - errList = append(errList, err) - resource.Bool = nil - } else { - resource.Bool = &Bool - return nil - } - - // Float64 - var Float64 float64 - if err := json.Unmarshal(raw, &Float64); err != nil { - errList = append(errList, err) - resource.Float64 = nil - } else { - resource.Float64 = &Float64 - return nil - } - - return errors.Join(errList...) -} - -func (resource TableAutoCellOptionsOrTableSparklineCellOptionsOrTableBarGaugeCellOptionsOrTableColoredBackgroundCellOptionsOrTableColorTextCellOptionsOrTableImageCellOptionsOrTableDataLinksCellOptionsOrTableJsonViewCellOptions) MarshalJSON() ([]byte, error) { - if resource.TableAutoCellOptions != nil { - return json.Marshal(resource.TableAutoCellOptions) - } - if resource.TableSparklineCellOptions != nil { - return json.Marshal(resource.TableSparklineCellOptions) - } - if resource.TableBarGaugeCellOptions != nil { - return json.Marshal(resource.TableBarGaugeCellOptions) - } - if resource.TableColoredBackgroundCellOptions != nil { - return json.Marshal(resource.TableColoredBackgroundCellOptions) - } - if resource.TableColorTextCellOptions != nil { - return json.Marshal(resource.TableColorTextCellOptions) - } - if resource.TableImageCellOptions != nil { - return json.Marshal(resource.TableImageCellOptions) - } - if resource.TableDataLinksCellOptions != nil { - return json.Marshal(resource.TableDataLinksCellOptions) - } - if resource.TableJsonViewCellOptions != nil { - return json.Marshal(resource.TableJsonViewCellOptions) - } - - return nil, fmt.Errorf("no value for disjunction of refs") -} -func (resource *TableAutoCellOptionsOrTableSparklineCellOptionsOrTableBarGaugeCellOptionsOrTableColoredBackgroundCellOptionsOrTableColorTextCellOptionsOrTableImageCellOptionsOrTableDataLinksCellOptionsOrTableJsonViewCellOptions) UnmarshalJSON(raw []byte) error { - if raw == nil { - return nil - } - - // FIXME: this is wasteful, we need to find a more efficient way to unmarshal this. - parsedAsMap := make(map[string]any) - if err := json.Unmarshal(raw, &parsedAsMap); err != nil { - return err - } - - discriminator, found := parsedAsMap["type"] - if !found { - return errors.New("discriminator field 'type' not found in payload") - } - - switch discriminator { - case "auto": - var tableAutoCellOptions TableAutoCellOptions - if err := json.Unmarshal(raw, &tableAutoCellOptions); err != nil { - return err - } - - resource.TableAutoCellOptions = &tableAutoCellOptions - return nil - case "color-background": - var tableColoredBackgroundCellOptions TableColoredBackgroundCellOptions - if err := json.Unmarshal(raw, &tableColoredBackgroundCellOptions); err != nil { - return err - } - - resource.TableColoredBackgroundCellOptions = &tableColoredBackgroundCellOptions - return nil - case "color-text": - var tableColorTextCellOptions TableColorTextCellOptions - if err := json.Unmarshal(raw, &tableColorTextCellOptions); err != nil { - return err - } - - resource.TableColorTextCellOptions = &tableColorTextCellOptions - return nil - case "data-links": - var tableDataLinksCellOptions TableDataLinksCellOptions - if err := json.Unmarshal(raw, &tableDataLinksCellOptions); err != nil { - return err - } - - resource.TableDataLinksCellOptions = &tableDataLinksCellOptions - return nil - case "gauge": - var tableBarGaugeCellOptions TableBarGaugeCellOptions - if err := json.Unmarshal(raw, &tableBarGaugeCellOptions); err != nil { - return err - } - - resource.TableBarGaugeCellOptions = &tableBarGaugeCellOptions - return nil - case "image": - var tableImageCellOptions TableImageCellOptions - if err := json.Unmarshal(raw, &tableImageCellOptions); err != nil { - return err - } - - resource.TableImageCellOptions = &tableImageCellOptions - return nil - case "json-view": - var tableJsonViewCellOptions TableJsonViewCellOptions - if err := json.Unmarshal(raw, &tableJsonViewCellOptions); err != nil { - return err - } - - resource.TableJsonViewCellOptions = &tableJsonViewCellOptions - return nil - case "sparkline": - var tableSparklineCellOptions TableSparklineCellOptions - if err := json.Unmarshal(raw, &tableSparklineCellOptions); err != nil { - return err - } - - resource.TableSparklineCellOptions = &tableSparklineCellOptions - return nil - } - - return fmt.Errorf("could not unmarshal resource with `type = %v`", discriminator) -} diff --git a/go/dashboard/snapshot_builder_gen.go b/go/dashboard/snapshot_builder_gen.go index 67f670f..3bcc084 100644 --- a/go/dashboard/snapshot_builder_gen.go +++ b/go/dashboard/snapshot_builder_gen.go @@ -3,6 +3,8 @@ package dashboard import ( + "time" + cog "github.com/grafana/grafana-foundation-sdk/go/cog" ) @@ -45,7 +47,7 @@ func (builder *SnapshotBuilder) Build() (Snapshot, error) { } // Time when the snapshot was created -func (builder *SnapshotBuilder) Created(created string) *SnapshotBuilder { +func (builder *SnapshotBuilder) Created(created time.Time) *SnapshotBuilder { builder.internal.Created = created return builder @@ -108,7 +110,7 @@ func (builder *SnapshotBuilder) OrgId(orgId uint32) *SnapshotBuilder { } // last time when the snapshot was updated -func (builder *SnapshotBuilder) Updated(updated string) *SnapshotBuilder { +func (builder *SnapshotBuilder) Updated(updated time.Time) *SnapshotBuilder { builder.internal.Updated = updated return builder diff --git a/go/dashboard/types_gen.go b/go/dashboard/types_gen.go index 51549a5..8efedaa 100644 --- a/go/dashboard/types_gen.go +++ b/go/dashboard/types_gen.go @@ -3,6 +3,12 @@ package dashboard import ( + "encoding/json" + "errors" + "fmt" + "time" + + cog "github.com/grafana/grafana-foundation-sdk/go/cog" cogvariants "github.com/grafana/grafana-foundation-sdk/go/cog/variants" ) @@ -520,7 +526,7 @@ const ( // Sensitive information stripped: queries (metric, template,annotation) and panel links. type Snapshot struct { // Time when the snapshot was created - Created string `json:"created"` + Created time.Time `json:"created"` // Time when the snapshot expires, default is never to expire Expires string `json:"expires"` // Is the snapshot saved in an external grafana instance @@ -538,7 +544,7 @@ type Snapshot struct { // org id of the snapshot OrgId uint32 `json:"orgId"` // last time when the snapshot was updated - Updated string `json:"updated"` + Updated time.Time `json:"updated"` // url of the snapshot, if snapshot was shared internally Url *string `json:"url,omitempty"` // user id of the snapshot creator @@ -614,6 +620,199 @@ type Panel struct { FieldConfig *FieldConfigSource `json:"fieldConfig,omitempty"` } +func (resource *Panel) UnmarshalJSON(raw []byte) error { + if raw == nil { + return nil + } + fields := make(map[string]json.RawMessage) + if err := json.Unmarshal(raw, &fields); err != nil { + return err + } + + if fields["type"] != nil { + if err := json.Unmarshal(fields["type"], &resource.Type); err != nil { + return err + } + } + + if fields["id"] != nil { + if err := json.Unmarshal(fields["id"], &resource.Id); err != nil { + return err + } + } + + if fields["pluginVersion"] != nil { + if err := json.Unmarshal(fields["pluginVersion"], &resource.PluginVersion); err != nil { + return err + } + } + + if fields["title"] != nil { + if err := json.Unmarshal(fields["title"], &resource.Title); err != nil { + return err + } + } + + if fields["description"] != nil { + if err := json.Unmarshal(fields["description"], &resource.Description); err != nil { + return err + } + } + + if fields["transparent"] != nil { + if err := json.Unmarshal(fields["transparent"], &resource.Transparent); err != nil { + return err + } + } + + if fields["datasource"] != nil { + if err := json.Unmarshal(fields["datasource"], &resource.Datasource); err != nil { + return err + } + } + + i...*[Comment body truncated]*