From 5c4ad8c3e5af3472e5b8f71ffa7b038a51e7f623 Mon Sep 17 00:00:00 2001 From: Peter Turi Date: Fri, 1 Aug 2025 14:20:42 +0200 Subject: [PATCH] chore: fix null metadata and annotation json representation In the DB instead of a NULL field value we end up having a 'null'::jsonb string, which is a pain to handle when it comes to data migrations. --- go.mod | 6 +- go.sum | 12 +- openmeter/ent/db/addon.go | 15 +- openmeter/ent/db/addon/addon.go | 6 + openmeter/ent/db/addon_create.go | 6 +- openmeter/ent/db/addon_query.go | 52 +++---- openmeter/ent/db/addon_update.go | 16 ++- openmeter/ent/db/addonratecard.go | 15 +- .../ent/db/addonratecard/addonratecard.go | 6 + openmeter/ent/db/addonratecard_create.go | 6 +- openmeter/ent/db/addonratecard_query.go | 52 +++---- openmeter/ent/db/addonratecard_update.go | 16 ++- openmeter/ent/db/app.go | 15 +- openmeter/ent/db/app/app.go | 10 ++ openmeter/ent/db/app_create.go | 20 ++- openmeter/ent/db/app_query.go | 52 +++---- openmeter/ent/db/app_update.go | 16 ++- openmeter/ent/db/appcustomer_query.go | 52 +++---- openmeter/ent/db/appcustominvoicing_query.go | 52 +++---- .../ent/db/appcustominvoicingcustomer.go | 15 +- .../appcustominvoicingcustomer.go | 10 ++ .../db/appcustominvoicingcustomer_create.go | 20 ++- .../db/appcustominvoicingcustomer_query.go | 52 +++---- .../db/appcustominvoicingcustomer_update.go | 16 ++- openmeter/ent/db/appstripe_query.go | 52 +++---- openmeter/ent/db/appstripecustomer_query.go | 52 +++---- openmeter/ent/db/balancesnapshot_query.go | 52 +++---- openmeter/ent/db/billingcustomerlock_query.go | 52 +++---- .../ent/db/billingcustomeroverride_query.go | 52 +++---- openmeter/ent/db/billinginvoice.go | 14 +- .../ent/db/billinginvoice/billinginvoice.go | 10 ++ openmeter/ent/db/billinginvoice_create.go | 20 ++- openmeter/ent/db/billinginvoice_query.go | 52 +++---- openmeter/ent/db/billinginvoice_update.go | 16 ++- .../billinginvoiceflatfeelineconfig_query.go | 52 +++---- openmeter/ent/db/billinginvoiceline.go | 26 ++-- .../billinginvoiceline/billinginvoiceline.go | 12 ++ openmeter/ent/db/billinginvoiceline_create.go | 12 +- openmeter/ent/db/billinginvoiceline_query.go | 52 +++---- openmeter/ent/db/billinginvoiceline_update.go | 32 +++-- .../db/billinginvoicelinediscount_query.go | 52 +++---- .../billinginvoicelineusagediscount_query.go | 52 +++---- .../ent/db/billinginvoicesplitlinegroup.go | 14 +- .../billinginvoicesplitlinegroup.go | 6 + .../db/billinginvoicesplitlinegroup_create.go | 6 +- .../db/billinginvoicesplitlinegroup_query.go | 52 +++---- .../db/billinginvoicesplitlinegroup_update.go | 16 ++- ...illinginvoiceusagebasedlineconfig_query.go | 52 +++---- .../db/billinginvoicevalidationissue_query.go | 52 +++---- openmeter/ent/db/billingprofile.go | 15 +- .../ent/db/billingprofile/billingprofile.go | 10 ++ openmeter/ent/db/billingprofile_create.go | 20 ++- openmeter/ent/db/billingprofile_query.go | 52 +++---- openmeter/ent/db/billingprofile_update.go | 16 ++- .../ent/db/billingsequencenumbers_query.go | 52 +++---- .../ent/db/billingworkflowconfig_query.go | 52 +++---- openmeter/ent/db/customer.go | 27 ++-- openmeter/ent/db/customer/customer.go | 16 +++ openmeter/ent/db/customer_create.go | 26 +++- openmeter/ent/db/customer_query.go | 52 +++---- openmeter/ent/db/customer_update.go | 32 +++-- openmeter/ent/db/customersubjects_query.go | 52 +++---- openmeter/ent/db/entitlement.go | 14 +- openmeter/ent/db/entitlement/entitlement.go | 6 + openmeter/ent/db/entitlement_create.go | 6 +- openmeter/ent/db/entitlement_query.go | 52 +++---- openmeter/ent/db/entitlement_update.go | 16 ++- openmeter/ent/db/feature.go | 14 +- openmeter/ent/db/feature/feature.go | 10 ++ openmeter/ent/db/feature_create.go | 20 ++- openmeter/ent/db/feature_query.go | 52 +++---- openmeter/ent/db/feature_update.go | 16 ++- openmeter/ent/db/grant.go | 14 +- openmeter/ent/db/grant/grant.go | 10 ++ openmeter/ent/db/grant_create.go | 20 ++- openmeter/ent/db/grant_query.go | 52 +++---- openmeter/ent/db/grant_update.go | 16 ++- openmeter/ent/db/meter.go | 14 +- openmeter/ent/db/meter/meter.go | 10 ++ openmeter/ent/db/meter_create.go | 20 ++- openmeter/ent/db/meter_query.go | 52 +++---- openmeter/ent/db/meter_update.go | 16 ++- openmeter/ent/db/migrate/schema.go | 48 +++---- openmeter/ent/db/notificationchannel_query.go | 52 +++---- openmeter/ent/db/notificationevent_query.go | 52 +++---- .../notificationeventdeliverystatus_query.go | 52 +++---- openmeter/ent/db/notificationrule_query.go | 52 +++---- openmeter/ent/db/plan.go | 15 +- openmeter/ent/db/plan/plan.go | 6 + openmeter/ent/db/plan_create.go | 6 +- openmeter/ent/db/plan_query.go | 52 +++---- openmeter/ent/db/plan_update.go | 16 ++- openmeter/ent/db/planaddon.go | 27 ++-- openmeter/ent/db/planaddon/planaddon.go | 16 +++ openmeter/ent/db/planaddon_create.go | 26 +++- openmeter/ent/db/planaddon_query.go | 52 +++---- openmeter/ent/db/planaddon_update.go | 32 +++-- openmeter/ent/db/planphase.go | 15 +- openmeter/ent/db/planphase/planphase.go | 10 ++ openmeter/ent/db/planphase_create.go | 20 ++- openmeter/ent/db/planphase_query.go | 52 +++---- openmeter/ent/db/planphase_update.go | 16 ++- openmeter/ent/db/planratecard.go | 15 +- openmeter/ent/db/planratecard/planratecard.go | 6 + openmeter/ent/db/planratecard_create.go | 6 +- openmeter/ent/db/planratecard_query.go | 52 +++---- openmeter/ent/db/planratecard_update.go | 16 ++- openmeter/ent/db/predicate/predicate.go | 132 ++++++++++++++++++ openmeter/ent/db/runtime.go | 98 +++++++++++++ openmeter/ent/db/runtime/runtime.go | 3 +- openmeter/ent/db/subject_query.go | 52 +++---- openmeter/ent/db/subscription.go | 15 +- openmeter/ent/db/subscription/subscription.go | 6 + openmeter/ent/db/subscription_create.go | 6 +- openmeter/ent/db/subscription_query.go | 52 +++---- openmeter/ent/db/subscription_update.go | 16 ++- openmeter/ent/db/subscriptionaddon.go | 15 +- .../db/subscriptionaddon/subscriptionaddon.go | 10 ++ openmeter/ent/db/subscriptionaddon_create.go | 20 ++- openmeter/ent/db/subscriptionaddon_query.go | 52 +++---- openmeter/ent/db/subscriptionaddon_update.go | 16 ++- .../ent/db/subscriptionaddonquantity_query.go | 52 +++---- openmeter/ent/db/subscriptionitem.go | 15 +- .../db/subscriptionitem/subscriptionitem.go | 6 + openmeter/ent/db/subscriptionitem_create.go | 6 +- openmeter/ent/db/subscriptionitem_query.go | 52 +++---- openmeter/ent/db/subscriptionitem_update.go | 16 ++- openmeter/ent/db/subscriptionphase.go | 15 +- .../db/subscriptionphase/subscriptionphase.go | 10 ++ openmeter/ent/db/subscriptionphase_create.go | 20 ++- openmeter/ent/db/subscriptionphase_query.go | 52 +++---- openmeter/ent/db/subscriptionphase_update.go | 16 ++- openmeter/ent/db/usagereset_query.go | 52 +++---- pkg/framework/entutils/mixins.go | 8 +- pkg/framework/entutils/validators.go | 5 + pkg/framework/entutils/valuescanner.go | 25 ++++ 136 files changed, 2290 insertions(+), 1515 deletions(-) create mode 100644 pkg/framework/entutils/validators.go diff --git a/go.mod b/go.mod index a41c948957..f9696ceb17 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ toolchain go1.24.2 replace github.com/oklog/run => github.com/openmeterio/run v0.0.0-20250217124527-c72029d4b634 require ( - entgo.io/ent v0.14.5-0.20250325141242-9db6f4df431f + entgo.io/ent v0.14.6-0.20250731081001-175d96f7ecc2 github.com/AppsFlyer/go-sundheit v0.6.0 github.com/ClickHouse/clickhouse-go/v2 v2.40.1 github.com/IBM/sarama v1.45.2 @@ -197,7 +197,7 @@ require ( ) require ( - ariga.io/atlas v0.32.1-0.20250325101103-175b25e1c1b9 // indirect + ariga.io/atlas v0.36.2-0.20250730182955-2c6300d0a3e1 // indirect cloud.google.com/go v0.121.3 // indirect cloud.google.com/go/auth v0.16.2 // indirect cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect @@ -527,3 +527,5 @@ require ( sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect sigs.k8s.io/yaml v1.5.0 // indirect ) + +replace entgo.io/ent => github.com/turip/ent v0.0.0-20250801121105-693f7a08bbe6 diff --git a/go.sum b/go.sum index 5689d4ec89..7d1560726f 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -ariga.io/atlas v0.32.1-0.20250325101103-175b25e1c1b9 h1:E0wvcUXTkgyN4wy4LGtNzMNGMytJN8afmIWXJVMi4cc= -ariga.io/atlas v0.32.1-0.20250325101103-175b25e1c1b9/go.mod h1:Oe1xWPuu5q9LzyrWfbZmEZxFYeu4BHTyzfjeW2aZp/w= +ariga.io/atlas v0.36.2-0.20250730182955-2c6300d0a3e1 h1:NPPfBaVZgz4LKBCIc0FbMogCjvXN+yGf7CZwotOwJo8= +ariga.io/atlas v0.36.2-0.20250730182955-2c6300d0a3e1/go.mod h1:Ex5l1xHsnWQUc3wYnrJ9gD7RUEzG76P7ZRQp8wNr0wc= buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.6-20250625184727-c923a0c2a132.1 h1:6tCo3lsKNLqUjRPhyc8JuYWYUiQkulufxSDOfG1zgWQ= buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.6-20250625184727-c923a0c2a132.1/go.mod h1:avRlCjnFzl98VPaeCtJ24RrV/wwHFzB8sWXhj26+n/U= cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY= @@ -652,8 +652,6 @@ cuelang.org/go v0.13.2/go.mod h1:8MoQXu+RcXsa2s9mebJN1HJ1orVDc9aI9/yKi6Dzsi4= dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8= dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -entgo.io/ent v0.14.5-0.20250325141242-9db6f4df431f h1:AAfmRfBzbqJtKJ/eY31atgFcnFt5Hzf1EaXTQA8ICrw= -entgo.io/ent v0.14.5-0.20250325141242-9db6f4df431f/go.mod h1:Kfi8EJfm12KhXDChE98cJ724r/ZrPjU+SXmqtEfZFZg= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8= @@ -1881,8 +1879,8 @@ github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= -github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= -github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= +github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk= @@ -2312,6 +2310,8 @@ github.com/trivago/grok v1.0.0 h1:oV2ljyZT63tgXkmgEHg2U0jMqiKKuL0hkn49s6aRavQ= github.com/trivago/grok v1.0.0/go.mod h1:9t59xLInhrncYq9a3J7488NgiBZi5y5yC7bss+w4NHM= github.com/trivago/tgo v1.0.7 h1:uaWH/XIy9aWYWpjm2CU3RpcqZXmX2ysQ9/Go+d9gyrM= github.com/trivago/tgo v1.0.7/go.mod h1:w4dpD+3tzNIIiIfkWWa85w5/B77tlvdZckQ+6PkFnhc= +github.com/turip/ent v0.0.0-20250801121105-693f7a08bbe6 h1:2GSZqA9uKWS6eoC6LpY53W/NZTMhIAdlg0jF8y52WBI= +github.com/turip/ent v0.0.0-20250801121105-693f7a08bbe6/go.mod h1:5CzkKalOTbnRbyzdWgeOtbgfO34YP/O+sFE5JuisbMU= github.com/twmb/franz-go v1.19.5 h1:W7+o8D0RsQsedqib71OVlLeZ0zI6CbFra7yTYhZTs5Y= github.com/twmb/franz-go v1.19.5/go.mod h1:4kFJ5tmbbl7asgwAGVuyG1ZMx0NNpYk7EqflvWfPCpM= github.com/twmb/franz-go/pkg/kadm v1.16.0 h1:STMs1t5lYR5mR974PSiwNzE5TvsosByTp+rKXLOhAjE= diff --git a/openmeter/ent/db/addon.go b/openmeter/ent/db/addon.go index bd57629c69..9bcad568aa 100644 --- a/openmeter/ent/db/addon.go +++ b/openmeter/ent/db/addon.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -98,14 +97,14 @@ func (*Addon) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case addon.FieldMetadata: - values[i] = new([]byte) case addon.FieldVersion: values[i] = new(sql.NullInt64) case addon.FieldID, addon.FieldNamespace, addon.FieldName, addon.FieldDescription, addon.FieldKey, addon.FieldCurrency, addon.FieldInstanceType: values[i] = new(sql.NullString) case addon.FieldCreatedAt, addon.FieldUpdatedAt, addon.FieldDeletedAt, addon.FieldEffectiveFrom, addon.FieldEffectiveTo: values[i] = new(sql.NullTime) + case addon.FieldMetadata: + values[i] = addon.ValueScanner.Metadata.ScanValue() case addon.FieldAnnotations: values[i] = addon.ValueScanner.Annotations.ScanValue() default: @@ -136,12 +135,10 @@ func (_m *Addon) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case addon.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := addon.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case addon.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/addon/addon.go b/openmeter/ent/db/addon/addon.go index 3a2f45dada..262c0218b3 100644 --- a/openmeter/ent/db/addon/addon.go +++ b/openmeter/ent/db/addon/addon.go @@ -126,6 +126,7 @@ var ( DefaultID func() string // ValueScanner of all Addon fields. ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] Annotations field.TypeValueScanner[map[string]interface{}] } ) @@ -155,6 +156,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/addon_create.go b/openmeter/ent/db/addon_create.go index 6d2e05450e..65d6d4def2 100644 --- a/openmeter/ent/db/addon_create.go +++ b/openmeter/ent/db/addon_create.go @@ -386,7 +386,11 @@ func (_c *AddonCreate) createSpec() (*Addon, *sqlgraph.CreateSpec, error) { _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(addon.FieldMetadata, field.TypeJSON, value) + vv, err := addon.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(addon.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { diff --git a/openmeter/ent/db/addon_query.go b/openmeter/ent/db/addon_query.go index e92a6e47f5..296a397979 100644 --- a/openmeter/ent/db/addon_query.go +++ b/openmeter/ent/db/addon_query.go @@ -709,41 +709,41 @@ type AddonGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (agb *AddonGroupBy) Aggregate(fns ...AggregateFunc) *AddonGroupBy { - agb.fns = append(agb.fns, fns...) - return agb +func (_g *AddonGroupBy) Aggregate(fns ...AggregateFunc) *AddonGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (agb *AddonGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, agb.build.ctx, ent.OpQueryGroupBy) - if err := agb.build.prepareQuery(ctx); err != nil { +func (_g *AddonGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AddonQuery, *AddonGroupBy](ctx, agb.build, agb, agb.build.inters, v) + return scanWithInterceptors[*AddonQuery, *AddonGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (agb *AddonGroupBy) sqlScan(ctx context.Context, root *AddonQuery, v any) error { +func (_g *AddonGroupBy) sqlScan(ctx context.Context, root *AddonQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(agb.fns)) - for _, fn := range agb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*agb.flds)+len(agb.fns)) - for _, f := range *agb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*agb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := agb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -757,27 +757,27 @@ type AddonSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (as *AddonSelect) Aggregate(fns ...AggregateFunc) *AddonSelect { - as.fns = append(as.fns, fns...) - return as +func (_s *AddonSelect) Aggregate(fns ...AggregateFunc) *AddonSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (as *AddonSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, as.ctx, ent.OpQuerySelect) - if err := as.prepareQuery(ctx); err != nil { +func (_s *AddonSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AddonQuery, *AddonSelect](ctx, as.AddonQuery, as, as.inters, v) + return scanWithInterceptors[*AddonQuery, *AddonSelect](ctx, _s.AddonQuery, _s, _s.inters, v) } -func (as *AddonSelect) sqlScan(ctx context.Context, root *AddonQuery, v any) error { +func (_s *AddonSelect) sqlScan(ctx context.Context, root *AddonQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(as.fns)) - for _, fn := range as.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*as.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -785,7 +785,7 @@ func (as *AddonSelect) sqlScan(ctx context.Context, root *AddonQuery, v any) err } rows := &sql.Rows{} query, args := selector.Query() - if err := as.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/addon_update.go b/openmeter/ent/db/addon_update.go index 6ec8c4d0ef..f9909ad079 100644 --- a/openmeter/ent/db/addon_update.go +++ b/openmeter/ent/db/addon_update.go @@ -368,10 +368,14 @@ func (_u *AddonUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(addon.FieldMetadata, field.TypeJSON, value) + vv, err := addon.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(addon.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(addon.FieldMetadata, field.TypeJSON) + _spec.ClearField(addon.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(addon.FieldUpdatedAt, field.TypeTime, value) @@ -943,10 +947,14 @@ func (_u *AddonUpdateOne) sqlSave(ctx context.Context) (_node *Addon, err error) } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(addon.FieldMetadata, field.TypeJSON, value) + vv, err := addon.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(addon.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(addon.FieldMetadata, field.TypeJSON) + _spec.ClearField(addon.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(addon.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/addonratecard.go b/openmeter/ent/db/addonratecard.go index 59ac8021bf..a7d010ea85 100644 --- a/openmeter/ent/db/addonratecard.go +++ b/openmeter/ent/db/addonratecard.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -100,12 +99,12 @@ func (*AddonRateCard) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case addonratecard.FieldMetadata: - values[i] = new([]byte) case addonratecard.FieldID, addonratecard.FieldNamespace, addonratecard.FieldName, addonratecard.FieldDescription, addonratecard.FieldKey, addonratecard.FieldType, addonratecard.FieldFeatureKey, addonratecard.FieldBillingCadence, addonratecard.FieldAddonID, addonratecard.FieldFeatureID: values[i] = new(sql.NullString) case addonratecard.FieldCreatedAt, addonratecard.FieldUpdatedAt, addonratecard.FieldDeletedAt: values[i] = new(sql.NullTime) + case addonratecard.FieldMetadata: + values[i] = addonratecard.ValueScanner.Metadata.ScanValue() case addonratecard.FieldEntitlementTemplate: values[i] = addonratecard.ValueScanner.EntitlementTemplate.ScanValue() case addonratecard.FieldTaxConfig: @@ -142,12 +141,10 @@ func (_m *AddonRateCard) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case addonratecard.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := addonratecard.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case addonratecard.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/addonratecard/addonratecard.go b/openmeter/ent/db/addonratecard/addonratecard.go index af317c3827..94a4d91cea 100644 --- a/openmeter/ent/db/addonratecard/addonratecard.go +++ b/openmeter/ent/db/addonratecard/addonratecard.go @@ -122,6 +122,7 @@ var ( DefaultID func() string // ValueScanner of all AddonRateCard fields. ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] EntitlementTemplate field.TypeValueScanner[*productcatalog.EntitlementTemplate] TaxConfig field.TypeValueScanner[*productcatalog.TaxConfig] Price field.TypeValueScanner[*productcatalog.Price] @@ -152,6 +153,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/addonratecard_create.go b/openmeter/ent/db/addonratecard_create.go index b43a4cfcdb..9b61000037 100644 --- a/openmeter/ent/db/addonratecard_create.go +++ b/openmeter/ent/db/addonratecard_create.go @@ -382,7 +382,11 @@ func (_c *AddonRateCardCreate) createSpec() (*AddonRateCard, *sqlgraph.CreateSpe _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(addonratecard.FieldMetadata, field.TypeJSON, value) + vv, err := addonratecard.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(addonratecard.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { diff --git a/openmeter/ent/db/addonratecard_query.go b/openmeter/ent/db/addonratecard_query.go index 625b2d5cde..29e0f862ee 100644 --- a/openmeter/ent/db/addonratecard_query.go +++ b/openmeter/ent/db/addonratecard_query.go @@ -637,41 +637,41 @@ type AddonRateCardGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (arcgb *AddonRateCardGroupBy) Aggregate(fns ...AggregateFunc) *AddonRateCardGroupBy { - arcgb.fns = append(arcgb.fns, fns...) - return arcgb +func (_g *AddonRateCardGroupBy) Aggregate(fns ...AggregateFunc) *AddonRateCardGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (arcgb *AddonRateCardGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, arcgb.build.ctx, ent.OpQueryGroupBy) - if err := arcgb.build.prepareQuery(ctx); err != nil { +func (_g *AddonRateCardGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AddonRateCardQuery, *AddonRateCardGroupBy](ctx, arcgb.build, arcgb, arcgb.build.inters, v) + return scanWithInterceptors[*AddonRateCardQuery, *AddonRateCardGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (arcgb *AddonRateCardGroupBy) sqlScan(ctx context.Context, root *AddonRateCardQuery, v any) error { +func (_g *AddonRateCardGroupBy) sqlScan(ctx context.Context, root *AddonRateCardQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(arcgb.fns)) - for _, fn := range arcgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*arcgb.flds)+len(arcgb.fns)) - for _, f := range *arcgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*arcgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := arcgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -685,27 +685,27 @@ type AddonRateCardSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (arcs *AddonRateCardSelect) Aggregate(fns ...AggregateFunc) *AddonRateCardSelect { - arcs.fns = append(arcs.fns, fns...) - return arcs +func (_s *AddonRateCardSelect) Aggregate(fns ...AggregateFunc) *AddonRateCardSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (arcs *AddonRateCardSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, arcs.ctx, ent.OpQuerySelect) - if err := arcs.prepareQuery(ctx); err != nil { +func (_s *AddonRateCardSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AddonRateCardQuery, *AddonRateCardSelect](ctx, arcs.AddonRateCardQuery, arcs, arcs.inters, v) + return scanWithInterceptors[*AddonRateCardQuery, *AddonRateCardSelect](ctx, _s.AddonRateCardQuery, _s, _s.inters, v) } -func (arcs *AddonRateCardSelect) sqlScan(ctx context.Context, root *AddonRateCardQuery, v any) error { +func (_s *AddonRateCardSelect) sqlScan(ctx context.Context, root *AddonRateCardQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(arcs.fns)) - for _, fn := range arcs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*arcs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -713,7 +713,7 @@ func (arcs *AddonRateCardSelect) sqlScan(ctx context.Context, root *AddonRateCar } rows := &sql.Rows{} query, args := selector.Query() - if err := arcs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/addonratecard_update.go b/openmeter/ent/db/addonratecard_update.go index a06e82c1d7..74296239a4 100644 --- a/openmeter/ent/db/addonratecard_update.go +++ b/openmeter/ent/db/addonratecard_update.go @@ -349,10 +349,14 @@ func (_u *AddonRateCardUpdate) sqlSave(ctx context.Context) (_node int, err erro } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(addonratecard.FieldMetadata, field.TypeJSON, value) + vv, err := addonratecard.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(addonratecard.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(addonratecard.FieldMetadata, field.TypeJSON) + _spec.ClearField(addonratecard.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(addonratecard.FieldUpdatedAt, field.TypeTime, value) @@ -849,10 +853,14 @@ func (_u *AddonRateCardUpdateOne) sqlSave(ctx context.Context) (_node *AddonRate } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(addonratecard.FieldMetadata, field.TypeJSON, value) + vv, err := addonratecard.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(addonratecard.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(addonratecard.FieldMetadata, field.TypeJSON) + _spec.ClearField(addonratecard.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(addonratecard.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/app.go b/openmeter/ent/db/app.go index 2f1f6e4321..4fa1465f33 100644 --- a/openmeter/ent/db/app.go +++ b/openmeter/ent/db/app.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -132,12 +131,12 @@ func (*App) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case dbapp.FieldMetadata: - values[i] = new([]byte) case dbapp.FieldID, dbapp.FieldNamespace, dbapp.FieldName, dbapp.FieldDescription, dbapp.FieldType, dbapp.FieldStatus: values[i] = new(sql.NullString) case dbapp.FieldCreatedAt, dbapp.FieldUpdatedAt, dbapp.FieldDeletedAt: values[i] = new(sql.NullTime) + case dbapp.FieldMetadata: + values[i] = dbapp.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -166,12 +165,10 @@ func (_m *App) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case dbapp.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := dbapp.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case dbapp.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/app/app.go b/openmeter/ent/db/app/app.go index 7843f90351..b04e9faf69 100644 --- a/openmeter/ent/db/app/app.go +++ b/openmeter/ent/db/app/app.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -134,6 +135,10 @@ var ( UpdateDefaultUpdatedAt func() time.Time // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all App fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // OrderOption defines the ordering options for the App queries. @@ -149,6 +154,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/app_create.go b/openmeter/ent/db/app_create.go index bc262c50d0..594b9c0adf 100644 --- a/openmeter/ent/db/app_create.go +++ b/openmeter/ent/db/app_create.go @@ -313,7 +313,10 @@ func (_c *AppCreate) sqlSave(ctx context.Context) (*App, error) { if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -332,7 +335,7 @@ func (_c *AppCreate) sqlSave(ctx context.Context) (*App, error) { return _node, nil } -func (_c *AppCreate) createSpec() (*App, *sqlgraph.CreateSpec) { +func (_c *AppCreate) createSpec() (*App, *sqlgraph.CreateSpec, error) { var ( _node = &App{config: _c.config} _spec = sqlgraph.NewCreateSpec(dbapp.Table, sqlgraph.NewFieldSpec(dbapp.FieldID, field.TypeString)) @@ -347,7 +350,11 @@ func (_c *AppCreate) createSpec() (*App, *sqlgraph.CreateSpec) { _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(dbapp.FieldMetadata, field.TypeJSON, value) + vv, err := dbapp.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(dbapp.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -490,7 +497,7 @@ func (_c *AppCreate) createSpec() (*App, *sqlgraph.CreateSpec) { } _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -862,7 +869,10 @@ func (_c *AppCreateBulk) Save(ctx context.Context) ([]*App, error) { } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/app_query.go b/openmeter/ent/db/app_query.go index d890872131..3fb71895d2 100644 --- a/openmeter/ent/db/app_query.go +++ b/openmeter/ent/db/app_query.go @@ -1011,41 +1011,41 @@ type AppGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (agb *AppGroupBy) Aggregate(fns ...AggregateFunc) *AppGroupBy { - agb.fns = append(agb.fns, fns...) - return agb +func (_g *AppGroupBy) Aggregate(fns ...AggregateFunc) *AppGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (agb *AppGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, agb.build.ctx, ent.OpQueryGroupBy) - if err := agb.build.prepareQuery(ctx); err != nil { +func (_g *AppGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppQuery, *AppGroupBy](ctx, agb.build, agb, agb.build.inters, v) + return scanWithInterceptors[*AppQuery, *AppGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (agb *AppGroupBy) sqlScan(ctx context.Context, root *AppQuery, v any) error { +func (_g *AppGroupBy) sqlScan(ctx context.Context, root *AppQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(agb.fns)) - for _, fn := range agb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*agb.flds)+len(agb.fns)) - for _, f := range *agb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*agb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := agb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -1059,27 +1059,27 @@ type AppSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (as *AppSelect) Aggregate(fns ...AggregateFunc) *AppSelect { - as.fns = append(as.fns, fns...) - return as +func (_s *AppSelect) Aggregate(fns ...AggregateFunc) *AppSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (as *AppSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, as.ctx, ent.OpQuerySelect) - if err := as.prepareQuery(ctx); err != nil { +func (_s *AppSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppQuery, *AppSelect](ctx, as.AppQuery, as, as.inters, v) + return scanWithInterceptors[*AppQuery, *AppSelect](ctx, _s.AppQuery, _s, _s.inters, v) } -func (as *AppSelect) sqlScan(ctx context.Context, root *AppQuery, v any) error { +func (_s *AppSelect) sqlScan(ctx context.Context, root *AppQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(as.fns)) - for _, fn := range as.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*as.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -1087,7 +1087,7 @@ func (as *AppSelect) sqlScan(ctx context.Context, root *AppQuery, v any) error { } rows := &sql.Rows{} query, args := selector.Query() - if err := as.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/app_update.go b/openmeter/ent/db/app_update.go index 7e809b4647..e848d2bf31 100644 --- a/openmeter/ent/db/app_update.go +++ b/openmeter/ent/db/app_update.go @@ -421,10 +421,14 @@ func (_u *AppUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(dbapp.FieldMetadata, field.TypeJSON, value) + vv, err := dbapp.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(dbapp.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(dbapp.FieldMetadata, field.TypeJSON) + _spec.ClearField(dbapp.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(dbapp.FieldUpdatedAt, field.TypeTime, value) @@ -1201,10 +1205,14 @@ func (_u *AppUpdateOne) sqlSave(ctx context.Context) (_node *App, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(dbapp.FieldMetadata, field.TypeJSON, value) + vv, err := dbapp.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(dbapp.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(dbapp.FieldMetadata, field.TypeJSON) + _spec.ClearField(dbapp.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(dbapp.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/appcustomer_query.go b/openmeter/ent/db/appcustomer_query.go index e03642761f..fd05098a1d 100644 --- a/openmeter/ent/db/appcustomer_query.go +++ b/openmeter/ent/db/appcustomer_query.go @@ -634,41 +634,41 @@ type AppCustomerGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (acgb *AppCustomerGroupBy) Aggregate(fns ...AggregateFunc) *AppCustomerGroupBy { - acgb.fns = append(acgb.fns, fns...) - return acgb +func (_g *AppCustomerGroupBy) Aggregate(fns ...AggregateFunc) *AppCustomerGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (acgb *AppCustomerGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, acgb.build.ctx, ent.OpQueryGroupBy) - if err := acgb.build.prepareQuery(ctx); err != nil { +func (_g *AppCustomerGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppCustomerQuery, *AppCustomerGroupBy](ctx, acgb.build, acgb, acgb.build.inters, v) + return scanWithInterceptors[*AppCustomerQuery, *AppCustomerGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (acgb *AppCustomerGroupBy) sqlScan(ctx context.Context, root *AppCustomerQuery, v any) error { +func (_g *AppCustomerGroupBy) sqlScan(ctx context.Context, root *AppCustomerQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(acgb.fns)) - for _, fn := range acgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*acgb.flds)+len(acgb.fns)) - for _, f := range *acgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*acgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := acgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -682,27 +682,27 @@ type AppCustomerSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (acs *AppCustomerSelect) Aggregate(fns ...AggregateFunc) *AppCustomerSelect { - acs.fns = append(acs.fns, fns...) - return acs +func (_s *AppCustomerSelect) Aggregate(fns ...AggregateFunc) *AppCustomerSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (acs *AppCustomerSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, acs.ctx, ent.OpQuerySelect) - if err := acs.prepareQuery(ctx); err != nil { +func (_s *AppCustomerSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppCustomerQuery, *AppCustomerSelect](ctx, acs.AppCustomerQuery, acs, acs.inters, v) + return scanWithInterceptors[*AppCustomerQuery, *AppCustomerSelect](ctx, _s.AppCustomerQuery, _s, _s.inters, v) } -func (acs *AppCustomerSelect) sqlScan(ctx context.Context, root *AppCustomerQuery, v any) error { +func (_s *AppCustomerSelect) sqlScan(ctx context.Context, root *AppCustomerQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(acs.fns)) - for _, fn := range acs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*acs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -710,7 +710,7 @@ func (acs *AppCustomerSelect) sqlScan(ctx context.Context, root *AppCustomerQuer } rows := &sql.Rows{} query, args := selector.Query() - if err := acs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/appcustominvoicing_query.go b/openmeter/ent/db/appcustominvoicing_query.go index 444759c1f3..3b444f4307 100644 --- a/openmeter/ent/db/appcustominvoicing_query.go +++ b/openmeter/ent/db/appcustominvoicing_query.go @@ -633,41 +633,41 @@ type AppCustomInvoicingGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (acigb *AppCustomInvoicingGroupBy) Aggregate(fns ...AggregateFunc) *AppCustomInvoicingGroupBy { - acigb.fns = append(acigb.fns, fns...) - return acigb +func (_g *AppCustomInvoicingGroupBy) Aggregate(fns ...AggregateFunc) *AppCustomInvoicingGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (acigb *AppCustomInvoicingGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, acigb.build.ctx, ent.OpQueryGroupBy) - if err := acigb.build.prepareQuery(ctx); err != nil { +func (_g *AppCustomInvoicingGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppCustomInvoicingQuery, *AppCustomInvoicingGroupBy](ctx, acigb.build, acigb, acigb.build.inters, v) + return scanWithInterceptors[*AppCustomInvoicingQuery, *AppCustomInvoicingGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (acigb *AppCustomInvoicingGroupBy) sqlScan(ctx context.Context, root *AppCustomInvoicingQuery, v any) error { +func (_g *AppCustomInvoicingGroupBy) sqlScan(ctx context.Context, root *AppCustomInvoicingQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(acigb.fns)) - for _, fn := range acigb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*acigb.flds)+len(acigb.fns)) - for _, f := range *acigb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*acigb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := acigb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -681,27 +681,27 @@ type AppCustomInvoicingSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (acis *AppCustomInvoicingSelect) Aggregate(fns ...AggregateFunc) *AppCustomInvoicingSelect { - acis.fns = append(acis.fns, fns...) - return acis +func (_s *AppCustomInvoicingSelect) Aggregate(fns ...AggregateFunc) *AppCustomInvoicingSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (acis *AppCustomInvoicingSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, acis.ctx, ent.OpQuerySelect) - if err := acis.prepareQuery(ctx); err != nil { +func (_s *AppCustomInvoicingSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppCustomInvoicingQuery, *AppCustomInvoicingSelect](ctx, acis.AppCustomInvoicingQuery, acis, acis.inters, v) + return scanWithInterceptors[*AppCustomInvoicingQuery, *AppCustomInvoicingSelect](ctx, _s.AppCustomInvoicingQuery, _s, _s.inters, v) } -func (acis *AppCustomInvoicingSelect) sqlScan(ctx context.Context, root *AppCustomInvoicingQuery, v any) error { +func (_s *AppCustomInvoicingSelect) sqlScan(ctx context.Context, root *AppCustomInvoicingQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(acis.fns)) - for _, fn := range acis.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*acis.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -709,7 +709,7 @@ func (acis *AppCustomInvoicingSelect) sqlScan(ctx context.Context, root *AppCust } rows := &sql.Rows{} query, args := selector.Query() - if err := acis.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/appcustominvoicingcustomer.go b/openmeter/ent/db/appcustominvoicingcustomer.go index e3d76ac762..dfc01c8749 100644 --- a/openmeter/ent/db/appcustominvoicingcustomer.go +++ b/openmeter/ent/db/appcustominvoicingcustomer.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -78,14 +77,14 @@ func (*AppCustomInvoicingCustomer) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case appcustominvoicingcustomer.FieldMetadata: - values[i] = new([]byte) case appcustominvoicingcustomer.FieldID: values[i] = new(sql.NullInt64) case appcustominvoicingcustomer.FieldNamespace, appcustominvoicingcustomer.FieldAppID, appcustominvoicingcustomer.FieldCustomerID: values[i] = new(sql.NullString) case appcustominvoicingcustomer.FieldCreatedAt, appcustominvoicingcustomer.FieldUpdatedAt, appcustominvoicingcustomer.FieldDeletedAt: values[i] = new(sql.NullTime) + case appcustominvoicingcustomer.FieldMetadata: + values[i] = appcustominvoicingcustomer.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -133,12 +132,10 @@ func (_m *AppCustomInvoicingCustomer) assignValues(columns []string, values []an *_m.DeletedAt = value.Time } case appcustominvoicingcustomer.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := appcustominvoicingcustomer.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case appcustominvoicingcustomer.FieldAppID: if value, ok := values[i].(*sql.NullString); !ok { diff --git a/openmeter/ent/db/appcustominvoicingcustomer/appcustominvoicingcustomer.go b/openmeter/ent/db/appcustominvoicingcustomer/appcustominvoicingcustomer.go index 99e3e41533..91357b09a5 100644 --- a/openmeter/ent/db/appcustominvoicingcustomer/appcustominvoicingcustomer.go +++ b/openmeter/ent/db/appcustominvoicingcustomer/appcustominvoicingcustomer.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -85,6 +86,10 @@ var ( AppIDValidator func(string) error // CustomerIDValidator is a validator for the "customer_id" field. It is called by the builders before save. CustomerIDValidator func(string) error + // ValueScanner of all AppCustomInvoicingCustomer fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // OrderOption defines the ordering options for the AppCustomInvoicingCustomer queries. @@ -115,6 +120,11 @@ func ByDeletedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldDeletedAt, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByAppID orders the results by the app_id field. func ByAppID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldAppID, opts...).ToFunc() diff --git a/openmeter/ent/db/appcustominvoicingcustomer_create.go b/openmeter/ent/db/appcustominvoicingcustomer_create.go index 6e7c92447a..3635b66ee3 100644 --- a/openmeter/ent/db/appcustominvoicingcustomer_create.go +++ b/openmeter/ent/db/appcustominvoicingcustomer_create.go @@ -196,7 +196,10 @@ func (_c *AppCustomInvoicingCustomerCreate) sqlSave(ctx context.Context) (*AppCu if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -210,7 +213,7 @@ func (_c *AppCustomInvoicingCustomerCreate) sqlSave(ctx context.Context) (*AppCu return _node, nil } -func (_c *AppCustomInvoicingCustomerCreate) createSpec() (*AppCustomInvoicingCustomer, *sqlgraph.CreateSpec) { +func (_c *AppCustomInvoicingCustomerCreate) createSpec() (*AppCustomInvoicingCustomer, *sqlgraph.CreateSpec, error) { var ( _node = &AppCustomInvoicingCustomer{config: _c.config} _spec = sqlgraph.NewCreateSpec(appcustominvoicingcustomer.Table, sqlgraph.NewFieldSpec(appcustominvoicingcustomer.FieldID, field.TypeInt)) @@ -233,7 +236,11 @@ func (_c *AppCustomInvoicingCustomerCreate) createSpec() (*AppCustomInvoicingCus _node.DeletedAt = &value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(appcustominvoicingcustomer.FieldMetadata, field.TypeJSON, value) + vv, err := appcustominvoicingcustomer.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(appcustominvoicingcustomer.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if nodes := _c.mutation.CustomInvoicingAppIDs(); len(nodes) > 0 { @@ -270,7 +277,7 @@ func (_c *AppCustomInvoicingCustomerCreate) createSpec() (*AppCustomInvoicingCus _node.CustomerID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -543,7 +550,10 @@ func (_c *AppCustomInvoicingCustomerCreateBulk) Save(ctx context.Context) ([]*Ap } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/appcustominvoicingcustomer_query.go b/openmeter/ent/db/appcustominvoicingcustomer_query.go index 234735836e..5a0e492926 100644 --- a/openmeter/ent/db/appcustominvoicingcustomer_query.go +++ b/openmeter/ent/db/appcustominvoicingcustomer_query.go @@ -634,41 +634,41 @@ type AppCustomInvoicingCustomerGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (acicgb *AppCustomInvoicingCustomerGroupBy) Aggregate(fns ...AggregateFunc) *AppCustomInvoicingCustomerGroupBy { - acicgb.fns = append(acicgb.fns, fns...) - return acicgb +func (_g *AppCustomInvoicingCustomerGroupBy) Aggregate(fns ...AggregateFunc) *AppCustomInvoicingCustomerGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (acicgb *AppCustomInvoicingCustomerGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, acicgb.build.ctx, ent.OpQueryGroupBy) - if err := acicgb.build.prepareQuery(ctx); err != nil { +func (_g *AppCustomInvoicingCustomerGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppCustomInvoicingCustomerQuery, *AppCustomInvoicingCustomerGroupBy](ctx, acicgb.build, acicgb, acicgb.build.inters, v) + return scanWithInterceptors[*AppCustomInvoicingCustomerQuery, *AppCustomInvoicingCustomerGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (acicgb *AppCustomInvoicingCustomerGroupBy) sqlScan(ctx context.Context, root *AppCustomInvoicingCustomerQuery, v any) error { +func (_g *AppCustomInvoicingCustomerGroupBy) sqlScan(ctx context.Context, root *AppCustomInvoicingCustomerQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(acicgb.fns)) - for _, fn := range acicgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*acicgb.flds)+len(acicgb.fns)) - for _, f := range *acicgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*acicgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := acicgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -682,27 +682,27 @@ type AppCustomInvoicingCustomerSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (acics *AppCustomInvoicingCustomerSelect) Aggregate(fns ...AggregateFunc) *AppCustomInvoicingCustomerSelect { - acics.fns = append(acics.fns, fns...) - return acics +func (_s *AppCustomInvoicingCustomerSelect) Aggregate(fns ...AggregateFunc) *AppCustomInvoicingCustomerSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (acics *AppCustomInvoicingCustomerSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, acics.ctx, ent.OpQuerySelect) - if err := acics.prepareQuery(ctx); err != nil { +func (_s *AppCustomInvoicingCustomerSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppCustomInvoicingCustomerQuery, *AppCustomInvoicingCustomerSelect](ctx, acics.AppCustomInvoicingCustomerQuery, acics, acics.inters, v) + return scanWithInterceptors[*AppCustomInvoicingCustomerQuery, *AppCustomInvoicingCustomerSelect](ctx, _s.AppCustomInvoicingCustomerQuery, _s, _s.inters, v) } -func (acics *AppCustomInvoicingCustomerSelect) sqlScan(ctx context.Context, root *AppCustomInvoicingCustomerQuery, v any) error { +func (_s *AppCustomInvoicingCustomerSelect) sqlScan(ctx context.Context, root *AppCustomInvoicingCustomerQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(acics.fns)) - for _, fn := range acics.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*acics.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -710,7 +710,7 @@ func (acics *AppCustomInvoicingCustomerSelect) sqlScan(ctx context.Context, root } rows := &sql.Rows{} query, args := selector.Query() - if err := acics.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/appcustominvoicingcustomer_update.go b/openmeter/ent/db/appcustominvoicingcustomer_update.go index bc3f0cdd68..27c5365ce3 100644 --- a/openmeter/ent/db/appcustominvoicingcustomer_update.go +++ b/openmeter/ent/db/appcustominvoicingcustomer_update.go @@ -140,10 +140,14 @@ func (_u *AppCustomInvoicingCustomerUpdate) sqlSave(ctx context.Context) (_node _spec.ClearField(appcustominvoicingcustomer.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(appcustominvoicingcustomer.FieldMetadata, field.TypeJSON, value) + vv, err := appcustominvoicingcustomer.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(appcustominvoicingcustomer.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(appcustominvoicingcustomer.FieldMetadata, field.TypeJSON) + _spec.ClearField(appcustominvoicingcustomer.FieldMetadata, field.TypeString) } if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { @@ -307,10 +311,14 @@ func (_u *AppCustomInvoicingCustomerUpdateOne) sqlSave(ctx context.Context) (_no _spec.ClearField(appcustominvoicingcustomer.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(appcustominvoicingcustomer.FieldMetadata, field.TypeJSON, value) + vv, err := appcustominvoicingcustomer.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(appcustominvoicingcustomer.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(appcustominvoicingcustomer.FieldMetadata, field.TypeJSON) + _spec.ClearField(appcustominvoicingcustomer.FieldMetadata, field.TypeString) } _node = &AppCustomInvoicingCustomer{config: _u.config} _spec.Assign = _node.assignValues diff --git a/openmeter/ent/db/appstripe_query.go b/openmeter/ent/db/appstripe_query.go index 6a05d83a7b..9d0364bc09 100644 --- a/openmeter/ent/db/appstripe_query.go +++ b/openmeter/ent/db/appstripe_query.go @@ -631,41 +631,41 @@ type AppStripeGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (asgb *AppStripeGroupBy) Aggregate(fns ...AggregateFunc) *AppStripeGroupBy { - asgb.fns = append(asgb.fns, fns...) - return asgb +func (_g *AppStripeGroupBy) Aggregate(fns ...AggregateFunc) *AppStripeGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (asgb *AppStripeGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, asgb.build.ctx, ent.OpQueryGroupBy) - if err := asgb.build.prepareQuery(ctx); err != nil { +func (_g *AppStripeGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppStripeQuery, *AppStripeGroupBy](ctx, asgb.build, asgb, asgb.build.inters, v) + return scanWithInterceptors[*AppStripeQuery, *AppStripeGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (asgb *AppStripeGroupBy) sqlScan(ctx context.Context, root *AppStripeQuery, v any) error { +func (_g *AppStripeGroupBy) sqlScan(ctx context.Context, root *AppStripeQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(asgb.fns)) - for _, fn := range asgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*asgb.flds)+len(asgb.fns)) - for _, f := range *asgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*asgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := asgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -679,27 +679,27 @@ type AppStripeSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (ass *AppStripeSelect) Aggregate(fns ...AggregateFunc) *AppStripeSelect { - ass.fns = append(ass.fns, fns...) - return ass +func (_s *AppStripeSelect) Aggregate(fns ...AggregateFunc) *AppStripeSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (ass *AppStripeSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ass.ctx, ent.OpQuerySelect) - if err := ass.prepareQuery(ctx); err != nil { +func (_s *AppStripeSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppStripeQuery, *AppStripeSelect](ctx, ass.AppStripeQuery, ass, ass.inters, v) + return scanWithInterceptors[*AppStripeQuery, *AppStripeSelect](ctx, _s.AppStripeQuery, _s, _s.inters, v) } -func (ass *AppStripeSelect) sqlScan(ctx context.Context, root *AppStripeQuery, v any) error { +func (_s *AppStripeSelect) sqlScan(ctx context.Context, root *AppStripeQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(ass.fns)) - for _, fn := range ass.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*ass.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -707,7 +707,7 @@ func (ass *AppStripeSelect) sqlScan(ctx context.Context, root *AppStripeQuery, v } rows := &sql.Rows{} query, args := selector.Query() - if err := ass.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/appstripecustomer_query.go b/openmeter/ent/db/appstripecustomer_query.go index a6a522d602..5429ea208b 100644 --- a/openmeter/ent/db/appstripecustomer_query.go +++ b/openmeter/ent/db/appstripecustomer_query.go @@ -634,41 +634,41 @@ type AppStripeCustomerGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (ascgb *AppStripeCustomerGroupBy) Aggregate(fns ...AggregateFunc) *AppStripeCustomerGroupBy { - ascgb.fns = append(ascgb.fns, fns...) - return ascgb +func (_g *AppStripeCustomerGroupBy) Aggregate(fns ...AggregateFunc) *AppStripeCustomerGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (ascgb *AppStripeCustomerGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ascgb.build.ctx, ent.OpQueryGroupBy) - if err := ascgb.build.prepareQuery(ctx); err != nil { +func (_g *AppStripeCustomerGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppStripeCustomerQuery, *AppStripeCustomerGroupBy](ctx, ascgb.build, ascgb, ascgb.build.inters, v) + return scanWithInterceptors[*AppStripeCustomerQuery, *AppStripeCustomerGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (ascgb *AppStripeCustomerGroupBy) sqlScan(ctx context.Context, root *AppStripeCustomerQuery, v any) error { +func (_g *AppStripeCustomerGroupBy) sqlScan(ctx context.Context, root *AppStripeCustomerQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(ascgb.fns)) - for _, fn := range ascgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*ascgb.flds)+len(ascgb.fns)) - for _, f := range *ascgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*ascgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := ascgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -682,27 +682,27 @@ type AppStripeCustomerSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (ascs *AppStripeCustomerSelect) Aggregate(fns ...AggregateFunc) *AppStripeCustomerSelect { - ascs.fns = append(ascs.fns, fns...) - return ascs +func (_s *AppStripeCustomerSelect) Aggregate(fns ...AggregateFunc) *AppStripeCustomerSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (ascs *AppStripeCustomerSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ascs.ctx, ent.OpQuerySelect) - if err := ascs.prepareQuery(ctx); err != nil { +func (_s *AppStripeCustomerSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*AppStripeCustomerQuery, *AppStripeCustomerSelect](ctx, ascs.AppStripeCustomerQuery, ascs, ascs.inters, v) + return scanWithInterceptors[*AppStripeCustomerQuery, *AppStripeCustomerSelect](ctx, _s.AppStripeCustomerQuery, _s, _s.inters, v) } -func (ascs *AppStripeCustomerSelect) sqlScan(ctx context.Context, root *AppStripeCustomerQuery, v any) error { +func (_s *AppStripeCustomerSelect) sqlScan(ctx context.Context, root *AppStripeCustomerQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(ascs.fns)) - for _, fn := range ascs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*ascs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -710,7 +710,7 @@ func (ascs *AppStripeCustomerSelect) sqlScan(ctx context.Context, root *AppStrip } rows := &sql.Rows{} query, args := selector.Query() - if err := ascs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/balancesnapshot_query.go b/openmeter/ent/db/balancesnapshot_query.go index e26066d724..60e05d20c3 100644 --- a/openmeter/ent/db/balancesnapshot_query.go +++ b/openmeter/ent/db/balancesnapshot_query.go @@ -559,41 +559,41 @@ type BalanceSnapshotGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bsgb *BalanceSnapshotGroupBy) Aggregate(fns ...AggregateFunc) *BalanceSnapshotGroupBy { - bsgb.fns = append(bsgb.fns, fns...) - return bsgb +func (_g *BalanceSnapshotGroupBy) Aggregate(fns ...AggregateFunc) *BalanceSnapshotGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bsgb *BalanceSnapshotGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bsgb.build.ctx, ent.OpQueryGroupBy) - if err := bsgb.build.prepareQuery(ctx); err != nil { +func (_g *BalanceSnapshotGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BalanceSnapshotQuery, *BalanceSnapshotGroupBy](ctx, bsgb.build, bsgb, bsgb.build.inters, v) + return scanWithInterceptors[*BalanceSnapshotQuery, *BalanceSnapshotGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bsgb *BalanceSnapshotGroupBy) sqlScan(ctx context.Context, root *BalanceSnapshotQuery, v any) error { +func (_g *BalanceSnapshotGroupBy) sqlScan(ctx context.Context, root *BalanceSnapshotQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bsgb.fns)) - for _, fn := range bsgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bsgb.flds)+len(bsgb.fns)) - for _, f := range *bsgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bsgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bsgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -607,27 +607,27 @@ type BalanceSnapshotSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bss *BalanceSnapshotSelect) Aggregate(fns ...AggregateFunc) *BalanceSnapshotSelect { - bss.fns = append(bss.fns, fns...) - return bss +func (_s *BalanceSnapshotSelect) Aggregate(fns ...AggregateFunc) *BalanceSnapshotSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bss *BalanceSnapshotSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bss.ctx, ent.OpQuerySelect) - if err := bss.prepareQuery(ctx); err != nil { +func (_s *BalanceSnapshotSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BalanceSnapshotQuery, *BalanceSnapshotSelect](ctx, bss.BalanceSnapshotQuery, bss, bss.inters, v) + return scanWithInterceptors[*BalanceSnapshotQuery, *BalanceSnapshotSelect](ctx, _s.BalanceSnapshotQuery, _s, _s.inters, v) } -func (bss *BalanceSnapshotSelect) sqlScan(ctx context.Context, root *BalanceSnapshotQuery, v any) error { +func (_s *BalanceSnapshotSelect) sqlScan(ctx context.Context, root *BalanceSnapshotQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bss.fns)) - for _, fn := range bss.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bss.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -635,7 +635,7 @@ func (bss *BalanceSnapshotSelect) sqlScan(ctx context.Context, root *BalanceSnap } rows := &sql.Rows{} query, args := selector.Query() - if err := bss.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billingcustomerlock_query.go b/openmeter/ent/db/billingcustomerlock_query.go index 5913377652..b9b047c1de 100644 --- a/openmeter/ent/db/billingcustomerlock_query.go +++ b/openmeter/ent/db/billingcustomerlock_query.go @@ -480,41 +480,41 @@ type BillingCustomerLockGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bclgb *BillingCustomerLockGroupBy) Aggregate(fns ...AggregateFunc) *BillingCustomerLockGroupBy { - bclgb.fns = append(bclgb.fns, fns...) - return bclgb +func (_g *BillingCustomerLockGroupBy) Aggregate(fns ...AggregateFunc) *BillingCustomerLockGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bclgb *BillingCustomerLockGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bclgb.build.ctx, ent.OpQueryGroupBy) - if err := bclgb.build.prepareQuery(ctx); err != nil { +func (_g *BillingCustomerLockGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingCustomerLockQuery, *BillingCustomerLockGroupBy](ctx, bclgb.build, bclgb, bclgb.build.inters, v) + return scanWithInterceptors[*BillingCustomerLockQuery, *BillingCustomerLockGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bclgb *BillingCustomerLockGroupBy) sqlScan(ctx context.Context, root *BillingCustomerLockQuery, v any) error { +func (_g *BillingCustomerLockGroupBy) sqlScan(ctx context.Context, root *BillingCustomerLockQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bclgb.fns)) - for _, fn := range bclgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bclgb.flds)+len(bclgb.fns)) - for _, f := range *bclgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bclgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bclgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -528,27 +528,27 @@ type BillingCustomerLockSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bcls *BillingCustomerLockSelect) Aggregate(fns ...AggregateFunc) *BillingCustomerLockSelect { - bcls.fns = append(bcls.fns, fns...) - return bcls +func (_s *BillingCustomerLockSelect) Aggregate(fns ...AggregateFunc) *BillingCustomerLockSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bcls *BillingCustomerLockSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bcls.ctx, ent.OpQuerySelect) - if err := bcls.prepareQuery(ctx); err != nil { +func (_s *BillingCustomerLockSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingCustomerLockQuery, *BillingCustomerLockSelect](ctx, bcls.BillingCustomerLockQuery, bcls, bcls.inters, v) + return scanWithInterceptors[*BillingCustomerLockQuery, *BillingCustomerLockSelect](ctx, _s.BillingCustomerLockQuery, _s, _s.inters, v) } -func (bcls *BillingCustomerLockSelect) sqlScan(ctx context.Context, root *BillingCustomerLockQuery, v any) error { +func (_s *BillingCustomerLockSelect) sqlScan(ctx context.Context, root *BillingCustomerLockQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bcls.fns)) - for _, fn := range bcls.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bcls.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -556,7 +556,7 @@ func (bcls *BillingCustomerLockSelect) sqlScan(ctx context.Context, root *Billin } rows := &sql.Rows{} query, args := selector.Query() - if err := bcls.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billingcustomeroverride_query.go b/openmeter/ent/db/billingcustomeroverride_query.go index b345f28b63..51cf7dc925 100644 --- a/openmeter/ent/db/billingcustomeroverride_query.go +++ b/openmeter/ent/db/billingcustomeroverride_query.go @@ -637,41 +637,41 @@ type BillingCustomerOverrideGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bcogb *BillingCustomerOverrideGroupBy) Aggregate(fns ...AggregateFunc) *BillingCustomerOverrideGroupBy { - bcogb.fns = append(bcogb.fns, fns...) - return bcogb +func (_g *BillingCustomerOverrideGroupBy) Aggregate(fns ...AggregateFunc) *BillingCustomerOverrideGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bcogb *BillingCustomerOverrideGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bcogb.build.ctx, ent.OpQueryGroupBy) - if err := bcogb.build.prepareQuery(ctx); err != nil { +func (_g *BillingCustomerOverrideGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingCustomerOverrideQuery, *BillingCustomerOverrideGroupBy](ctx, bcogb.build, bcogb, bcogb.build.inters, v) + return scanWithInterceptors[*BillingCustomerOverrideQuery, *BillingCustomerOverrideGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bcogb *BillingCustomerOverrideGroupBy) sqlScan(ctx context.Context, root *BillingCustomerOverrideQuery, v any) error { +func (_g *BillingCustomerOverrideGroupBy) sqlScan(ctx context.Context, root *BillingCustomerOverrideQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bcogb.fns)) - for _, fn := range bcogb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bcogb.flds)+len(bcogb.fns)) - for _, f := range *bcogb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bcogb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bcogb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -685,27 +685,27 @@ type BillingCustomerOverrideSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bcos *BillingCustomerOverrideSelect) Aggregate(fns ...AggregateFunc) *BillingCustomerOverrideSelect { - bcos.fns = append(bcos.fns, fns...) - return bcos +func (_s *BillingCustomerOverrideSelect) Aggregate(fns ...AggregateFunc) *BillingCustomerOverrideSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bcos *BillingCustomerOverrideSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bcos.ctx, ent.OpQuerySelect) - if err := bcos.prepareQuery(ctx); err != nil { +func (_s *BillingCustomerOverrideSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingCustomerOverrideQuery, *BillingCustomerOverrideSelect](ctx, bcos.BillingCustomerOverrideQuery, bcos, bcos.inters, v) + return scanWithInterceptors[*BillingCustomerOverrideQuery, *BillingCustomerOverrideSelect](ctx, _s.BillingCustomerOverrideQuery, _s, _s.inters, v) } -func (bcos *BillingCustomerOverrideSelect) sqlScan(ctx context.Context, root *BillingCustomerOverrideQuery, v any) error { +func (_s *BillingCustomerOverrideSelect) sqlScan(ctx context.Context, root *BillingCustomerOverrideQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bcos.fns)) - for _, fn := range bcos.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bcos.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -713,7 +713,7 @@ func (bcos *BillingCustomerOverrideSelect) sqlScan(ctx context.Context, root *Bi } rows := &sql.Rows{} query, args := selector.Query() - if err := bcos.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billinginvoice.go b/openmeter/ent/db/billinginvoice.go index 84ab541998..f056420c3b 100644 --- a/openmeter/ent/db/billinginvoice.go +++ b/openmeter/ent/db/billinginvoice.go @@ -252,7 +252,7 @@ func (*BillingInvoice) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case billinginvoice.FieldMetadata, billinginvoice.FieldCustomerUsageAttribution, billinginvoice.FieldStatusDetailsCache: + case billinginvoice.FieldCustomerUsageAttribution, billinginvoice.FieldStatusDetailsCache: values[i] = new([]byte) case billinginvoice.FieldAmount, billinginvoice.FieldTaxesTotal, billinginvoice.FieldTaxesInclusiveTotal, billinginvoice.FieldTaxesExclusiveTotal, billinginvoice.FieldChargesTotal, billinginvoice.FieldDiscountsTotal, billinginvoice.FieldTotal: values[i] = new(alpacadecimal.Decimal) @@ -260,6 +260,8 @@ func (*BillingInvoice) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullString) case billinginvoice.FieldCreatedAt, billinginvoice.FieldUpdatedAt, billinginvoice.FieldDeletedAt, billinginvoice.FieldVoidedAt, billinginvoice.FieldIssuedAt, billinginvoice.FieldSentToCustomerAt, billinginvoice.FieldDraftUntil, billinginvoice.FieldQuantitySnapshotedAt, billinginvoice.FieldDueAt, billinginvoice.FieldPeriodStart, billinginvoice.FieldPeriodEnd, billinginvoice.FieldCollectionAt: values[i] = new(sql.NullTime) + case billinginvoice.FieldMetadata: + values[i] = billinginvoice.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -288,12 +290,10 @@ func (_m *BillingInvoice) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case billinginvoice.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := billinginvoice.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case billinginvoice.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/billinginvoice/billinginvoice.go b/openmeter/ent/db/billinginvoice/billinginvoice.go index 28d5246587..7524af3549 100644 --- a/openmeter/ent/db/billinginvoice/billinginvoice.go +++ b/openmeter/ent/db/billinginvoice/billinginvoice.go @@ -8,6 +8,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" "github.com/openmeterio/openmeter/openmeter/billing" ) @@ -296,6 +297,10 @@ var ( DefaultCollectionAt func() time.Time // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all BillingInvoice fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // TypeValidator is a validator for the "type" field enum values. It is called by the builders before save. @@ -331,6 +336,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/billinginvoice_create.go b/openmeter/ent/db/billinginvoice_create.go index 3662a040d8..6d6f11426c 100644 --- a/openmeter/ent/db/billinginvoice_create.go +++ b/openmeter/ent/db/billinginvoice_create.go @@ -898,7 +898,10 @@ func (_c *BillingInvoiceCreate) sqlSave(ctx context.Context) (*BillingInvoice, e if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -917,7 +920,7 @@ func (_c *BillingInvoiceCreate) sqlSave(ctx context.Context) (*BillingInvoice, e return _node, nil } -func (_c *BillingInvoiceCreate) createSpec() (*BillingInvoice, *sqlgraph.CreateSpec) { +func (_c *BillingInvoiceCreate) createSpec() (*BillingInvoice, *sqlgraph.CreateSpec, error) { var ( _node = &BillingInvoice{config: _c.config} _spec = sqlgraph.NewCreateSpec(billinginvoice.Table, sqlgraph.NewFieldSpec(billinginvoice.FieldID, field.TypeString)) @@ -932,7 +935,11 @@ func (_c *BillingInvoiceCreate) createSpec() (*BillingInvoice, *sqlgraph.CreateS _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(billinginvoice.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoice.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(billinginvoice.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -1253,7 +1260,7 @@ func (_c *BillingInvoiceCreate) createSpec() (*BillingInvoice, *sqlgraph.CreateS _node.PaymentAppID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -3044,7 +3051,10 @@ func (_c *BillingInvoiceCreateBulk) Save(ctx context.Context) ([]*BillingInvoice } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/billinginvoice_query.go b/openmeter/ent/db/billinginvoice_query.go index e3cf63f6de..c6c1ceb1d9 100644 --- a/openmeter/ent/db/billinginvoice_query.go +++ b/openmeter/ent/db/billinginvoice_query.go @@ -1086,41 +1086,41 @@ type BillingInvoiceGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bigb *BillingInvoiceGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceGroupBy { - bigb.fns = append(bigb.fns, fns...) - return bigb +func (_g *BillingInvoiceGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bigb *BillingInvoiceGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bigb.build.ctx, ent.OpQueryGroupBy) - if err := bigb.build.prepareQuery(ctx); err != nil { +func (_g *BillingInvoiceGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceQuery, *BillingInvoiceGroupBy](ctx, bigb.build, bigb, bigb.build.inters, v) + return scanWithInterceptors[*BillingInvoiceQuery, *BillingInvoiceGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bigb *BillingInvoiceGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceQuery, v any) error { +func (_g *BillingInvoiceGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bigb.fns)) - for _, fn := range bigb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bigb.flds)+len(bigb.fns)) - for _, f := range *bigb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bigb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bigb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -1134,27 +1134,27 @@ type BillingInvoiceSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bis *BillingInvoiceSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceSelect { - bis.fns = append(bis.fns, fns...) - return bis +func (_s *BillingInvoiceSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bis *BillingInvoiceSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bis.ctx, ent.OpQuerySelect) - if err := bis.prepareQuery(ctx); err != nil { +func (_s *BillingInvoiceSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceQuery, *BillingInvoiceSelect](ctx, bis.BillingInvoiceQuery, bis, bis.inters, v) + return scanWithInterceptors[*BillingInvoiceQuery, *BillingInvoiceSelect](ctx, _s.BillingInvoiceQuery, _s, _s.inters, v) } -func (bis *BillingInvoiceSelect) sqlScan(ctx context.Context, root *BillingInvoiceQuery, v any) error { +func (_s *BillingInvoiceSelect) sqlScan(ctx context.Context, root *BillingInvoiceQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bis.fns)) - for _, fn := range bis.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bis.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -1162,7 +1162,7 @@ func (bis *BillingInvoiceSelect) sqlScan(ctx context.Context, root *BillingInvoi } rows := &sql.Rows{} query, args := selector.Query() - if err := bis.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billinginvoice_update.go b/openmeter/ent/db/billinginvoice_update.go index fdf4d8c50b..ed498691d3 100644 --- a/openmeter/ent/db/billinginvoice_update.go +++ b/openmeter/ent/db/billinginvoice_update.go @@ -1036,10 +1036,14 @@ func (_u *BillingInvoiceUpdate) sqlSave(ctx context.Context) (_node int, err err } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(billinginvoice.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoice.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(billinginvoice.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(billinginvoice.FieldMetadata, field.TypeJSON) + _spec.ClearField(billinginvoice.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(billinginvoice.FieldUpdatedAt, field.TypeTime, value) @@ -2434,10 +2438,14 @@ func (_u *BillingInvoiceUpdateOne) sqlSave(ctx context.Context) (_node *BillingI } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(billinginvoice.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoice.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(billinginvoice.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(billinginvoice.FieldMetadata, field.TypeJSON) + _spec.ClearField(billinginvoice.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(billinginvoice.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/billinginvoiceflatfeelineconfig_query.go b/openmeter/ent/db/billinginvoiceflatfeelineconfig_query.go index a4620b5fc4..a52dacd0e5 100644 --- a/openmeter/ent/db/billinginvoiceflatfeelineconfig_query.go +++ b/openmeter/ent/db/billinginvoiceflatfeelineconfig_query.go @@ -480,41 +480,41 @@ type BillingInvoiceFlatFeeLineConfigGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bifflcgb *BillingInvoiceFlatFeeLineConfigGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceFlatFeeLineConfigGroupBy { - bifflcgb.fns = append(bifflcgb.fns, fns...) - return bifflcgb +func (_g *BillingInvoiceFlatFeeLineConfigGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceFlatFeeLineConfigGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bifflcgb *BillingInvoiceFlatFeeLineConfigGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bifflcgb.build.ctx, ent.OpQueryGroupBy) - if err := bifflcgb.build.prepareQuery(ctx); err != nil { +func (_g *BillingInvoiceFlatFeeLineConfigGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceFlatFeeLineConfigQuery, *BillingInvoiceFlatFeeLineConfigGroupBy](ctx, bifflcgb.build, bifflcgb, bifflcgb.build.inters, v) + return scanWithInterceptors[*BillingInvoiceFlatFeeLineConfigQuery, *BillingInvoiceFlatFeeLineConfigGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bifflcgb *BillingInvoiceFlatFeeLineConfigGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceFlatFeeLineConfigQuery, v any) error { +func (_g *BillingInvoiceFlatFeeLineConfigGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceFlatFeeLineConfigQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bifflcgb.fns)) - for _, fn := range bifflcgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bifflcgb.flds)+len(bifflcgb.fns)) - for _, f := range *bifflcgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bifflcgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bifflcgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -528,27 +528,27 @@ type BillingInvoiceFlatFeeLineConfigSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bifflcs *BillingInvoiceFlatFeeLineConfigSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceFlatFeeLineConfigSelect { - bifflcs.fns = append(bifflcs.fns, fns...) - return bifflcs +func (_s *BillingInvoiceFlatFeeLineConfigSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceFlatFeeLineConfigSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bifflcs *BillingInvoiceFlatFeeLineConfigSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bifflcs.ctx, ent.OpQuerySelect) - if err := bifflcs.prepareQuery(ctx); err != nil { +func (_s *BillingInvoiceFlatFeeLineConfigSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceFlatFeeLineConfigQuery, *BillingInvoiceFlatFeeLineConfigSelect](ctx, bifflcs.BillingInvoiceFlatFeeLineConfigQuery, bifflcs, bifflcs.inters, v) + return scanWithInterceptors[*BillingInvoiceFlatFeeLineConfigQuery, *BillingInvoiceFlatFeeLineConfigSelect](ctx, _s.BillingInvoiceFlatFeeLineConfigQuery, _s, _s.inters, v) } -func (bifflcs *BillingInvoiceFlatFeeLineConfigSelect) sqlScan(ctx context.Context, root *BillingInvoiceFlatFeeLineConfigQuery, v any) error { +func (_s *BillingInvoiceFlatFeeLineConfigSelect) sqlScan(ctx context.Context, root *BillingInvoiceFlatFeeLineConfigQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bifflcs.fns)) - for _, fn := range bifflcs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bifflcs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -556,7 +556,7 @@ func (bifflcs *BillingInvoiceFlatFeeLineConfigSelect) sqlScan(ctx context.Contex } rows := &sql.Rows{} query, args := selector.Query() - if err := bifflcs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billinginvoiceline.go b/openmeter/ent/db/billinginvoiceline.go index c9a67a3dcb..b7f1b56be4 100644 --- a/openmeter/ent/db/billinginvoiceline.go +++ b/openmeter/ent/db/billinginvoiceline.go @@ -258,7 +258,7 @@ func (*BillingInvoiceLine) scanValues(columns []string) ([]any, error) { switch columns[i] { case billinginvoiceline.FieldQuantity: values[i] = &sql.NullScanner{S: new(alpacadecimal.Decimal)} - case billinginvoiceline.FieldAnnotations, billinginvoiceline.FieldMetadata, billinginvoiceline.FieldTaxConfig: + case billinginvoiceline.FieldTaxConfig: values[i] = new([]byte) case billinginvoiceline.FieldAmount, billinginvoiceline.FieldTaxesTotal, billinginvoiceline.FieldTaxesInclusiveTotal, billinginvoiceline.FieldTaxesExclusiveTotal, billinginvoiceline.FieldChargesTotal, billinginvoiceline.FieldDiscountsTotal, billinginvoiceline.FieldTotal: values[i] = new(alpacadecimal.Decimal) @@ -266,6 +266,10 @@ func (*BillingInvoiceLine) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullString) case billinginvoiceline.FieldCreatedAt, billinginvoiceline.FieldUpdatedAt, billinginvoiceline.FieldDeletedAt, billinginvoiceline.FieldPeriodStart, billinginvoiceline.FieldPeriodEnd, billinginvoiceline.FieldInvoiceAt: values[i] = new(sql.NullTime) + case billinginvoiceline.FieldAnnotations: + values[i] = billinginvoiceline.ValueScanner.Annotations.ScanValue() + case billinginvoiceline.FieldMetadata: + values[i] = billinginvoiceline.ValueScanner.Metadata.ScanValue() case billinginvoiceline.FieldRatecardDiscounts: values[i] = billinginvoiceline.ValueScanner.RatecardDiscounts.ScanValue() case billinginvoiceline.ForeignKeys[0]: // fee_line_config_id @@ -294,12 +298,10 @@ func (_m *BillingInvoiceLine) assignValues(columns []string, values []any) error _m.ID = value.String } case billinginvoiceline.FieldAnnotations: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field annotations", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Annotations); err != nil { - return fmt.Errorf("unmarshal field annotations: %w", err) - } + if value, err := billinginvoiceline.ValueScanner.Annotations.FromValue(values[i]); err != nil { + return err + } else { + _m.Annotations = value } case billinginvoiceline.FieldNamespace: if value, ok := values[i].(*sql.NullString); !ok { @@ -308,12 +310,10 @@ func (_m *BillingInvoiceLine) assignValues(columns []string, values []any) error _m.Namespace = value.String } case billinginvoiceline.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := billinginvoiceline.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case billinginvoiceline.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/billinginvoiceline/billinginvoiceline.go b/openmeter/ent/db/billinginvoiceline/billinginvoiceline.go index c491389f74..29fe85c7ff 100644 --- a/openmeter/ent/db/billinginvoiceline/billinginvoiceline.go +++ b/openmeter/ent/db/billinginvoiceline/billinginvoiceline.go @@ -262,6 +262,8 @@ var ( DefaultID func() string // ValueScanner of all BillingInvoiceLine fields. ValueScanner struct { + Annotations field.TypeValueScanner[map[string]interface{}] + Metadata field.TypeValueScanner[map[string]string] RatecardDiscounts field.TypeValueScanner[*billing.Discounts] } ) @@ -304,11 +306,21 @@ func ByID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldID, opts...).ToFunc() } +// ByAnnotations orders the results by the annotations field. +func ByAnnotations(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldAnnotations, opts...).ToFunc() +} + // ByNamespace orders the results by the namespace field. func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/billinginvoiceline_create.go b/openmeter/ent/db/billinginvoiceline_create.go index 9227caed68..fa642162f6 100644 --- a/openmeter/ent/db/billinginvoiceline_create.go +++ b/openmeter/ent/db/billinginvoiceline_create.go @@ -666,7 +666,11 @@ func (_c *BillingInvoiceLineCreate) createSpec() (*BillingInvoiceLine, *sqlgraph _spec.ID.Value = id } if value, ok := _c.mutation.Annotations(); ok { - _spec.SetField(billinginvoiceline.FieldAnnotations, field.TypeJSON, value) + vv, err := billinginvoiceline.ValueScanner.Annotations.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(billinginvoiceline.FieldAnnotations, field.TypeString, vv) _node.Annotations = value } if value, ok := _c.mutation.Namespace(); ok { @@ -674,7 +678,11 @@ func (_c *BillingInvoiceLineCreate) createSpec() (*BillingInvoiceLine, *sqlgraph _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(billinginvoiceline.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoiceline.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(billinginvoiceline.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { diff --git a/openmeter/ent/db/billinginvoiceline_query.go b/openmeter/ent/db/billinginvoiceline_query.go index e1d70b9512..62d272eac8 100644 --- a/openmeter/ent/db/billinginvoiceline_query.go +++ b/openmeter/ent/db/billinginvoiceline_query.go @@ -1338,41 +1338,41 @@ type BillingInvoiceLineGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bilgb *BillingInvoiceLineGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineGroupBy { - bilgb.fns = append(bilgb.fns, fns...) - return bilgb +func (_g *BillingInvoiceLineGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bilgb *BillingInvoiceLineGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bilgb.build.ctx, ent.OpQueryGroupBy) - if err := bilgb.build.prepareQuery(ctx); err != nil { +func (_g *BillingInvoiceLineGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceLineQuery, *BillingInvoiceLineGroupBy](ctx, bilgb.build, bilgb, bilgb.build.inters, v) + return scanWithInterceptors[*BillingInvoiceLineQuery, *BillingInvoiceLineGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bilgb *BillingInvoiceLineGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceLineQuery, v any) error { +func (_g *BillingInvoiceLineGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceLineQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bilgb.fns)) - for _, fn := range bilgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bilgb.flds)+len(bilgb.fns)) - for _, f := range *bilgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bilgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bilgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -1386,27 +1386,27 @@ type BillingInvoiceLineSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bils *BillingInvoiceLineSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineSelect { - bils.fns = append(bils.fns, fns...) - return bils +func (_s *BillingInvoiceLineSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bils *BillingInvoiceLineSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bils.ctx, ent.OpQuerySelect) - if err := bils.prepareQuery(ctx); err != nil { +func (_s *BillingInvoiceLineSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceLineQuery, *BillingInvoiceLineSelect](ctx, bils.BillingInvoiceLineQuery, bils, bils.inters, v) + return scanWithInterceptors[*BillingInvoiceLineQuery, *BillingInvoiceLineSelect](ctx, _s.BillingInvoiceLineQuery, _s, _s.inters, v) } -func (bils *BillingInvoiceLineSelect) sqlScan(ctx context.Context, root *BillingInvoiceLineQuery, v any) error { +func (_s *BillingInvoiceLineSelect) sqlScan(ctx context.Context, root *BillingInvoiceLineQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bils.fns)) - for _, fn := range bils.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bils.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -1414,7 +1414,7 @@ func (bils *BillingInvoiceLineSelect) sqlScan(ctx context.Context, root *Billing } rows := &sql.Rows{} query, args := selector.Query() - if err := bils.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billinginvoiceline_update.go b/openmeter/ent/db/billinginvoiceline_update.go index c7049362b7..8af023e26f 100644 --- a/openmeter/ent/db/billinginvoiceline_update.go +++ b/openmeter/ent/db/billinginvoiceline_update.go @@ -825,16 +825,24 @@ func (_u *BillingInvoiceLineUpdate) sqlSave(ctx context.Context) (_node int, err } } if value, ok := _u.mutation.Annotations(); ok { - _spec.SetField(billinginvoiceline.FieldAnnotations, field.TypeJSON, value) + vv, err := billinginvoiceline.ValueScanner.Annotations.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(billinginvoiceline.FieldAnnotations, field.TypeString, vv) } if _u.mutation.AnnotationsCleared() { - _spec.ClearField(billinginvoiceline.FieldAnnotations, field.TypeJSON) + _spec.ClearField(billinginvoiceline.FieldAnnotations, field.TypeString) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(billinginvoiceline.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoiceline.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(billinginvoiceline.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(billinginvoiceline.FieldMetadata, field.TypeJSON) + _spec.ClearField(billinginvoiceline.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(billinginvoiceline.FieldUpdatedAt, field.TypeTime, value) @@ -2132,16 +2140,24 @@ func (_u *BillingInvoiceLineUpdateOne) sqlSave(ctx context.Context) (_node *Bill } } if value, ok := _u.mutation.Annotations(); ok { - _spec.SetField(billinginvoiceline.FieldAnnotations, field.TypeJSON, value) + vv, err := billinginvoiceline.ValueScanner.Annotations.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(billinginvoiceline.FieldAnnotations, field.TypeString, vv) } if _u.mutation.AnnotationsCleared() { - _spec.ClearField(billinginvoiceline.FieldAnnotations, field.TypeJSON) + _spec.ClearField(billinginvoiceline.FieldAnnotations, field.TypeString) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(billinginvoiceline.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoiceline.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(billinginvoiceline.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(billinginvoiceline.FieldMetadata, field.TypeJSON) + _spec.ClearField(billinginvoiceline.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(billinginvoiceline.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/billinginvoicelinediscount_query.go b/openmeter/ent/db/billinginvoicelinediscount_query.go index 2c239cebbd..fc23af6df6 100644 --- a/openmeter/ent/db/billinginvoicelinediscount_query.go +++ b/openmeter/ent/db/billinginvoicelinediscount_query.go @@ -559,41 +559,41 @@ type BillingInvoiceLineDiscountGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bildgb *BillingInvoiceLineDiscountGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineDiscountGroupBy { - bildgb.fns = append(bildgb.fns, fns...) - return bildgb +func (_g *BillingInvoiceLineDiscountGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineDiscountGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bildgb *BillingInvoiceLineDiscountGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bildgb.build.ctx, ent.OpQueryGroupBy) - if err := bildgb.build.prepareQuery(ctx); err != nil { +func (_g *BillingInvoiceLineDiscountGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceLineDiscountQuery, *BillingInvoiceLineDiscountGroupBy](ctx, bildgb.build, bildgb, bildgb.build.inters, v) + return scanWithInterceptors[*BillingInvoiceLineDiscountQuery, *BillingInvoiceLineDiscountGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bildgb *BillingInvoiceLineDiscountGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceLineDiscountQuery, v any) error { +func (_g *BillingInvoiceLineDiscountGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceLineDiscountQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bildgb.fns)) - for _, fn := range bildgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bildgb.flds)+len(bildgb.fns)) - for _, f := range *bildgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bildgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bildgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -607,27 +607,27 @@ type BillingInvoiceLineDiscountSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bilds *BillingInvoiceLineDiscountSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineDiscountSelect { - bilds.fns = append(bilds.fns, fns...) - return bilds +func (_s *BillingInvoiceLineDiscountSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineDiscountSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bilds *BillingInvoiceLineDiscountSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bilds.ctx, ent.OpQuerySelect) - if err := bilds.prepareQuery(ctx); err != nil { +func (_s *BillingInvoiceLineDiscountSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceLineDiscountQuery, *BillingInvoiceLineDiscountSelect](ctx, bilds.BillingInvoiceLineDiscountQuery, bilds, bilds.inters, v) + return scanWithInterceptors[*BillingInvoiceLineDiscountQuery, *BillingInvoiceLineDiscountSelect](ctx, _s.BillingInvoiceLineDiscountQuery, _s, _s.inters, v) } -func (bilds *BillingInvoiceLineDiscountSelect) sqlScan(ctx context.Context, root *BillingInvoiceLineDiscountQuery, v any) error { +func (_s *BillingInvoiceLineDiscountSelect) sqlScan(ctx context.Context, root *BillingInvoiceLineDiscountQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bilds.fns)) - for _, fn := range bilds.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bilds.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -635,7 +635,7 @@ func (bilds *BillingInvoiceLineDiscountSelect) sqlScan(ctx context.Context, root } rows := &sql.Rows{} query, args := selector.Query() - if err := bilds.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billinginvoicelineusagediscount_query.go b/openmeter/ent/db/billinginvoicelineusagediscount_query.go index f6dc3cbc82..162ad5b04d 100644 --- a/openmeter/ent/db/billinginvoicelineusagediscount_query.go +++ b/openmeter/ent/db/billinginvoicelineusagediscount_query.go @@ -559,41 +559,41 @@ type BillingInvoiceLineUsageDiscountGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (biludgb *BillingInvoiceLineUsageDiscountGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineUsageDiscountGroupBy { - biludgb.fns = append(biludgb.fns, fns...) - return biludgb +func (_g *BillingInvoiceLineUsageDiscountGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineUsageDiscountGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (biludgb *BillingInvoiceLineUsageDiscountGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, biludgb.build.ctx, ent.OpQueryGroupBy) - if err := biludgb.build.prepareQuery(ctx); err != nil { +func (_g *BillingInvoiceLineUsageDiscountGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceLineUsageDiscountQuery, *BillingInvoiceLineUsageDiscountGroupBy](ctx, biludgb.build, biludgb, biludgb.build.inters, v) + return scanWithInterceptors[*BillingInvoiceLineUsageDiscountQuery, *BillingInvoiceLineUsageDiscountGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (biludgb *BillingInvoiceLineUsageDiscountGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceLineUsageDiscountQuery, v any) error { +func (_g *BillingInvoiceLineUsageDiscountGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceLineUsageDiscountQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(biludgb.fns)) - for _, fn := range biludgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*biludgb.flds)+len(biludgb.fns)) - for _, f := range *biludgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*biludgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := biludgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -607,27 +607,27 @@ type BillingInvoiceLineUsageDiscountSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (biluds *BillingInvoiceLineUsageDiscountSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineUsageDiscountSelect { - biluds.fns = append(biluds.fns, fns...) - return biluds +func (_s *BillingInvoiceLineUsageDiscountSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceLineUsageDiscountSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (biluds *BillingInvoiceLineUsageDiscountSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, biluds.ctx, ent.OpQuerySelect) - if err := biluds.prepareQuery(ctx); err != nil { +func (_s *BillingInvoiceLineUsageDiscountSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceLineUsageDiscountQuery, *BillingInvoiceLineUsageDiscountSelect](ctx, biluds.BillingInvoiceLineUsageDiscountQuery, biluds, biluds.inters, v) + return scanWithInterceptors[*BillingInvoiceLineUsageDiscountQuery, *BillingInvoiceLineUsageDiscountSelect](ctx, _s.BillingInvoiceLineUsageDiscountQuery, _s, _s.inters, v) } -func (biluds *BillingInvoiceLineUsageDiscountSelect) sqlScan(ctx context.Context, root *BillingInvoiceLineUsageDiscountQuery, v any) error { +func (_s *BillingInvoiceLineUsageDiscountSelect) sqlScan(ctx context.Context, root *BillingInvoiceLineUsageDiscountQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(biluds.fns)) - for _, fn := range biluds.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*biluds.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -635,7 +635,7 @@ func (biluds *BillingInvoiceLineUsageDiscountSelect) sqlScan(ctx context.Context } rows := &sql.Rows{} query, args := selector.Query() - if err := biluds.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billinginvoicesplitlinegroup.go b/openmeter/ent/db/billinginvoicesplitlinegroup.go index 37a00776a3..12b7d5b17b 100644 --- a/openmeter/ent/db/billinginvoicesplitlinegroup.go +++ b/openmeter/ent/db/billinginvoicesplitlinegroup.go @@ -128,12 +128,14 @@ func (*BillingInvoiceSplitLineGroup) scanValues(columns []string) ([]any, error) values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case billinginvoicesplitlinegroup.FieldMetadata, billinginvoicesplitlinegroup.FieldTaxConfig: + case billinginvoicesplitlinegroup.FieldTaxConfig: values[i] = new([]byte) case billinginvoicesplitlinegroup.FieldID, billinginvoicesplitlinegroup.FieldNamespace, billinginvoicesplitlinegroup.FieldName, billinginvoicesplitlinegroup.FieldDescription, billinginvoicesplitlinegroup.FieldCurrency, billinginvoicesplitlinegroup.FieldUniqueReferenceID, billinginvoicesplitlinegroup.FieldFeatureKey, billinginvoicesplitlinegroup.FieldSubscriptionID, billinginvoicesplitlinegroup.FieldSubscriptionPhaseID, billinginvoicesplitlinegroup.FieldSubscriptionItemID: values[i] = new(sql.NullString) case billinginvoicesplitlinegroup.FieldCreatedAt, billinginvoicesplitlinegroup.FieldUpdatedAt, billinginvoicesplitlinegroup.FieldDeletedAt, billinginvoicesplitlinegroup.FieldServicePeriodStart, billinginvoicesplitlinegroup.FieldServicePeriodEnd: values[i] = new(sql.NullTime) + case billinginvoicesplitlinegroup.FieldMetadata: + values[i] = billinginvoicesplitlinegroup.ValueScanner.Metadata.ScanValue() case billinginvoicesplitlinegroup.FieldRatecardDiscounts: values[i] = billinginvoicesplitlinegroup.ValueScanner.RatecardDiscounts.ScanValue() case billinginvoicesplitlinegroup.FieldPrice: @@ -166,12 +168,10 @@ func (_m *BillingInvoiceSplitLineGroup) assignValues(columns []string, values [] _m.Namespace = value.String } case billinginvoicesplitlinegroup.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := billinginvoicesplitlinegroup.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case billinginvoicesplitlinegroup.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/billinginvoicesplitlinegroup/billinginvoicesplitlinegroup.go b/openmeter/ent/db/billinginvoicesplitlinegroup/billinginvoicesplitlinegroup.go index 17848ccd6c..92201d8d36 100644 --- a/openmeter/ent/db/billinginvoicesplitlinegroup/billinginvoicesplitlinegroup.go +++ b/openmeter/ent/db/billinginvoicesplitlinegroup/billinginvoicesplitlinegroup.go @@ -141,6 +141,7 @@ var ( DefaultID func() string // ValueScanner of all BillingInvoiceSplitLineGroup fields. ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] RatecardDiscounts field.TypeValueScanner[*billing.Discounts] Price field.TypeValueScanner[*productcatalog.Price] } @@ -159,6 +160,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/billinginvoicesplitlinegroup_create.go b/openmeter/ent/db/billinginvoicesplitlinegroup_create.go index e380ded638..39ed30ff4a 100644 --- a/openmeter/ent/db/billinginvoicesplitlinegroup_create.go +++ b/openmeter/ent/db/billinginvoicesplitlinegroup_create.go @@ -401,7 +401,11 @@ func (_c *BillingInvoiceSplitLineGroupCreate) createSpec() (*BillingInvoiceSplit _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoicesplitlinegroup.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { diff --git a/openmeter/ent/db/billinginvoicesplitlinegroup_query.go b/openmeter/ent/db/billinginvoicesplitlinegroup_query.go index d6b180757d..d45f8ca8b3 100644 --- a/openmeter/ent/db/billinginvoicesplitlinegroup_query.go +++ b/openmeter/ent/db/billinginvoicesplitlinegroup_query.go @@ -799,41 +799,41 @@ type BillingInvoiceSplitLineGroupGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bislggb *BillingInvoiceSplitLineGroupGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceSplitLineGroupGroupBy { - bislggb.fns = append(bislggb.fns, fns...) - return bislggb +func (_g *BillingInvoiceSplitLineGroupGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceSplitLineGroupGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bislggb *BillingInvoiceSplitLineGroupGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bislggb.build.ctx, ent.OpQueryGroupBy) - if err := bislggb.build.prepareQuery(ctx); err != nil { +func (_g *BillingInvoiceSplitLineGroupGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceSplitLineGroupQuery, *BillingInvoiceSplitLineGroupGroupBy](ctx, bislggb.build, bislggb, bislggb.build.inters, v) + return scanWithInterceptors[*BillingInvoiceSplitLineGroupQuery, *BillingInvoiceSplitLineGroupGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bislggb *BillingInvoiceSplitLineGroupGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceSplitLineGroupQuery, v any) error { +func (_g *BillingInvoiceSplitLineGroupGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceSplitLineGroupQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bislggb.fns)) - for _, fn := range bislggb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bislggb.flds)+len(bislggb.fns)) - for _, f := range *bislggb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bislggb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bislggb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -847,27 +847,27 @@ type BillingInvoiceSplitLineGroupSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bislgs *BillingInvoiceSplitLineGroupSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceSplitLineGroupSelect { - bislgs.fns = append(bislgs.fns, fns...) - return bislgs +func (_s *BillingInvoiceSplitLineGroupSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceSplitLineGroupSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bislgs *BillingInvoiceSplitLineGroupSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bislgs.ctx, ent.OpQuerySelect) - if err := bislgs.prepareQuery(ctx); err != nil { +func (_s *BillingInvoiceSplitLineGroupSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceSplitLineGroupQuery, *BillingInvoiceSplitLineGroupSelect](ctx, bislgs.BillingInvoiceSplitLineGroupQuery, bislgs, bislgs.inters, v) + return scanWithInterceptors[*BillingInvoiceSplitLineGroupQuery, *BillingInvoiceSplitLineGroupSelect](ctx, _s.BillingInvoiceSplitLineGroupQuery, _s, _s.inters, v) } -func (bislgs *BillingInvoiceSplitLineGroupSelect) sqlScan(ctx context.Context, root *BillingInvoiceSplitLineGroupQuery, v any) error { +func (_s *BillingInvoiceSplitLineGroupSelect) sqlScan(ctx context.Context, root *BillingInvoiceSplitLineGroupQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bislgs.fns)) - for _, fn := range bislgs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bislgs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -875,7 +875,7 @@ func (bislgs *BillingInvoiceSplitLineGroupSelect) sqlScan(ctx context.Context, r } rows := &sql.Rows{} query, args := selector.Query() - if err := bislgs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billinginvoicesplitlinegroup_update.go b/openmeter/ent/db/billinginvoicesplitlinegroup_update.go index 667552d6c9..639ebc7d7d 100644 --- a/openmeter/ent/db/billinginvoicesplitlinegroup_update.go +++ b/openmeter/ent/db/billinginvoicesplitlinegroup_update.go @@ -250,10 +250,14 @@ func (_u *BillingInvoiceSplitLineGroupUpdate) sqlSave(ctx context.Context) (_nod } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoicesplitlinegroup.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeJSON) + _spec.ClearField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(billinginvoicesplitlinegroup.FieldUpdatedAt, field.TypeTime, value) @@ -615,10 +619,14 @@ func (_u *BillingInvoiceSplitLineGroupUpdateOne) sqlSave(ctx context.Context) (_ } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeJSON, value) + vv, err := billinginvoicesplitlinegroup.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeJSON) + _spec.ClearField(billinginvoicesplitlinegroup.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(billinginvoicesplitlinegroup.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/billinginvoiceusagebasedlineconfig_query.go b/openmeter/ent/db/billinginvoiceusagebasedlineconfig_query.go index 66e084fc3d..56657400e7 100644 --- a/openmeter/ent/db/billinginvoiceusagebasedlineconfig_query.go +++ b/openmeter/ent/db/billinginvoiceusagebasedlineconfig_query.go @@ -480,41 +480,41 @@ type BillingInvoiceUsageBasedLineConfigGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (biublcgb *BillingInvoiceUsageBasedLineConfigGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceUsageBasedLineConfigGroupBy { - biublcgb.fns = append(biublcgb.fns, fns...) - return biublcgb +func (_g *BillingInvoiceUsageBasedLineConfigGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceUsageBasedLineConfigGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (biublcgb *BillingInvoiceUsageBasedLineConfigGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, biublcgb.build.ctx, ent.OpQueryGroupBy) - if err := biublcgb.build.prepareQuery(ctx); err != nil { +func (_g *BillingInvoiceUsageBasedLineConfigGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceUsageBasedLineConfigQuery, *BillingInvoiceUsageBasedLineConfigGroupBy](ctx, biublcgb.build, biublcgb, biublcgb.build.inters, v) + return scanWithInterceptors[*BillingInvoiceUsageBasedLineConfigQuery, *BillingInvoiceUsageBasedLineConfigGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (biublcgb *BillingInvoiceUsageBasedLineConfigGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceUsageBasedLineConfigQuery, v any) error { +func (_g *BillingInvoiceUsageBasedLineConfigGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceUsageBasedLineConfigQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(biublcgb.fns)) - for _, fn := range biublcgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*biublcgb.flds)+len(biublcgb.fns)) - for _, f := range *biublcgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*biublcgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := biublcgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -528,27 +528,27 @@ type BillingInvoiceUsageBasedLineConfigSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (biublcs *BillingInvoiceUsageBasedLineConfigSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceUsageBasedLineConfigSelect { - biublcs.fns = append(biublcs.fns, fns...) - return biublcs +func (_s *BillingInvoiceUsageBasedLineConfigSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceUsageBasedLineConfigSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (biublcs *BillingInvoiceUsageBasedLineConfigSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, biublcs.ctx, ent.OpQuerySelect) - if err := biublcs.prepareQuery(ctx); err != nil { +func (_s *BillingInvoiceUsageBasedLineConfigSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceUsageBasedLineConfigQuery, *BillingInvoiceUsageBasedLineConfigSelect](ctx, biublcs.BillingInvoiceUsageBasedLineConfigQuery, biublcs, biublcs.inters, v) + return scanWithInterceptors[*BillingInvoiceUsageBasedLineConfigQuery, *BillingInvoiceUsageBasedLineConfigSelect](ctx, _s.BillingInvoiceUsageBasedLineConfigQuery, _s, _s.inters, v) } -func (biublcs *BillingInvoiceUsageBasedLineConfigSelect) sqlScan(ctx context.Context, root *BillingInvoiceUsageBasedLineConfigQuery, v any) error { +func (_s *BillingInvoiceUsageBasedLineConfigSelect) sqlScan(ctx context.Context, root *BillingInvoiceUsageBasedLineConfigQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(biublcs.fns)) - for _, fn := range biublcs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*biublcs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -556,7 +556,7 @@ func (biublcs *BillingInvoiceUsageBasedLineConfigSelect) sqlScan(ctx context.Con } rows := &sql.Rows{} query, args := selector.Query() - if err := biublcs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billinginvoicevalidationissue_query.go b/openmeter/ent/db/billinginvoicevalidationissue_query.go index 53e31dd199..ee66da2566 100644 --- a/openmeter/ent/db/billinginvoicevalidationissue_query.go +++ b/openmeter/ent/db/billinginvoicevalidationissue_query.go @@ -559,41 +559,41 @@ type BillingInvoiceValidationIssueGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bivigb *BillingInvoiceValidationIssueGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceValidationIssueGroupBy { - bivigb.fns = append(bivigb.fns, fns...) - return bivigb +func (_g *BillingInvoiceValidationIssueGroupBy) Aggregate(fns ...AggregateFunc) *BillingInvoiceValidationIssueGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bivigb *BillingInvoiceValidationIssueGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bivigb.build.ctx, ent.OpQueryGroupBy) - if err := bivigb.build.prepareQuery(ctx); err != nil { +func (_g *BillingInvoiceValidationIssueGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceValidationIssueQuery, *BillingInvoiceValidationIssueGroupBy](ctx, bivigb.build, bivigb, bivigb.build.inters, v) + return scanWithInterceptors[*BillingInvoiceValidationIssueQuery, *BillingInvoiceValidationIssueGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bivigb *BillingInvoiceValidationIssueGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceValidationIssueQuery, v any) error { +func (_g *BillingInvoiceValidationIssueGroupBy) sqlScan(ctx context.Context, root *BillingInvoiceValidationIssueQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bivigb.fns)) - for _, fn := range bivigb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bivigb.flds)+len(bivigb.fns)) - for _, f := range *bivigb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bivigb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bivigb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -607,27 +607,27 @@ type BillingInvoiceValidationIssueSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bivis *BillingInvoiceValidationIssueSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceValidationIssueSelect { - bivis.fns = append(bivis.fns, fns...) - return bivis +func (_s *BillingInvoiceValidationIssueSelect) Aggregate(fns ...AggregateFunc) *BillingInvoiceValidationIssueSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bivis *BillingInvoiceValidationIssueSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bivis.ctx, ent.OpQuerySelect) - if err := bivis.prepareQuery(ctx); err != nil { +func (_s *BillingInvoiceValidationIssueSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingInvoiceValidationIssueQuery, *BillingInvoiceValidationIssueSelect](ctx, bivis.BillingInvoiceValidationIssueQuery, bivis, bivis.inters, v) + return scanWithInterceptors[*BillingInvoiceValidationIssueQuery, *BillingInvoiceValidationIssueSelect](ctx, _s.BillingInvoiceValidationIssueQuery, _s, _s.inters, v) } -func (bivis *BillingInvoiceValidationIssueSelect) sqlScan(ctx context.Context, root *BillingInvoiceValidationIssueQuery, v any) error { +func (_s *BillingInvoiceValidationIssueSelect) sqlScan(ctx context.Context, root *BillingInvoiceValidationIssueQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bivis.fns)) - for _, fn := range bivis.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bivis.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -635,7 +635,7 @@ func (bivis *BillingInvoiceValidationIssueSelect) sqlScan(ctx context.Context, r } rows := &sql.Rows{} query, args := selector.Query() - if err := bivis.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billingprofile.go b/openmeter/ent/db/billingprofile.go index 3dafec499d..2bafbbe95e 100644 --- a/openmeter/ent/db/billingprofile.go +++ b/openmeter/ent/db/billingprofile.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -155,14 +154,14 @@ func (*BillingProfile) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case billingprofile.FieldMetadata: - values[i] = new([]byte) case billingprofile.FieldDefault: values[i] = new(sql.NullBool) case billingprofile.FieldID, billingprofile.FieldNamespace, billingprofile.FieldName, billingprofile.FieldDescription, billingprofile.FieldSupplierAddressCountry, billingprofile.FieldSupplierAddressPostalCode, billingprofile.FieldSupplierAddressState, billingprofile.FieldSupplierAddressCity, billingprofile.FieldSupplierAddressLine1, billingprofile.FieldSupplierAddressLine2, billingprofile.FieldSupplierAddressPhoneNumber, billingprofile.FieldTaxAppID, billingprofile.FieldInvoicingAppID, billingprofile.FieldPaymentAppID, billingprofile.FieldWorkflowConfigID, billingprofile.FieldSupplierName, billingprofile.FieldSupplierTaxCode: values[i] = new(sql.NullString) case billingprofile.FieldCreatedAt, billingprofile.FieldUpdatedAt, billingprofile.FieldDeletedAt: values[i] = new(sql.NullTime) + case billingprofile.FieldMetadata: + values[i] = billingprofile.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -191,12 +190,10 @@ func (_m *BillingProfile) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case billingprofile.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := billingprofile.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case billingprofile.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/billingprofile/billingprofile.go b/openmeter/ent/db/billingprofile/billingprofile.go index a6b48752a1..ce427714c9 100644 --- a/openmeter/ent/db/billingprofile/billingprofile.go +++ b/openmeter/ent/db/billingprofile/billingprofile.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -169,6 +170,10 @@ var ( SupplierNameValidator func(string) error // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all BillingProfile fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // OrderOption defines the ordering options for the BillingProfile queries. @@ -184,6 +189,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/billingprofile_create.go b/openmeter/ent/db/billingprofile_create.go index 8fa5e8e560..33658a9d21 100644 --- a/openmeter/ent/db/billingprofile_create.go +++ b/openmeter/ent/db/billingprofile_create.go @@ -446,7 +446,10 @@ func (_c *BillingProfileCreate) sqlSave(ctx context.Context) (*BillingProfile, e if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -465,7 +468,7 @@ func (_c *BillingProfileCreate) sqlSave(ctx context.Context) (*BillingProfile, e return _node, nil } -func (_c *BillingProfileCreate) createSpec() (*BillingProfile, *sqlgraph.CreateSpec) { +func (_c *BillingProfileCreate) createSpec() (*BillingProfile, *sqlgraph.CreateSpec, error) { var ( _node = &BillingProfile{config: _c.config} _spec = sqlgraph.NewCreateSpec(billingprofile.Table, sqlgraph.NewFieldSpec(billingprofile.FieldID, field.TypeString)) @@ -480,7 +483,11 @@ func (_c *BillingProfileCreate) createSpec() (*BillingProfile, *sqlgraph.CreateS _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(billingprofile.FieldMetadata, field.TypeJSON, value) + vv, err := billingprofile.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(billingprofile.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -643,7 +650,7 @@ func (_c *BillingProfileCreate) createSpec() (*BillingProfile, *sqlgraph.CreateS _node.PaymentAppID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -1385,7 +1392,10 @@ func (_c *BillingProfileCreateBulk) Save(ctx context.Context) ([]*BillingProfile } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/billingprofile_query.go b/openmeter/ent/db/billingprofile_query.go index 92017c832a..85d64c1ebc 100644 --- a/openmeter/ent/db/billingprofile_query.go +++ b/openmeter/ent/db/billingprofile_query.go @@ -938,41 +938,41 @@ type BillingProfileGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bpgb *BillingProfileGroupBy) Aggregate(fns ...AggregateFunc) *BillingProfileGroupBy { - bpgb.fns = append(bpgb.fns, fns...) - return bpgb +func (_g *BillingProfileGroupBy) Aggregate(fns ...AggregateFunc) *BillingProfileGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bpgb *BillingProfileGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bpgb.build.ctx, ent.OpQueryGroupBy) - if err := bpgb.build.prepareQuery(ctx); err != nil { +func (_g *BillingProfileGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingProfileQuery, *BillingProfileGroupBy](ctx, bpgb.build, bpgb, bpgb.build.inters, v) + return scanWithInterceptors[*BillingProfileQuery, *BillingProfileGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bpgb *BillingProfileGroupBy) sqlScan(ctx context.Context, root *BillingProfileQuery, v any) error { +func (_g *BillingProfileGroupBy) sqlScan(ctx context.Context, root *BillingProfileQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bpgb.fns)) - for _, fn := range bpgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bpgb.flds)+len(bpgb.fns)) - for _, f := range *bpgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bpgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bpgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -986,27 +986,27 @@ type BillingProfileSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bps *BillingProfileSelect) Aggregate(fns ...AggregateFunc) *BillingProfileSelect { - bps.fns = append(bps.fns, fns...) - return bps +func (_s *BillingProfileSelect) Aggregate(fns ...AggregateFunc) *BillingProfileSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bps *BillingProfileSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bps.ctx, ent.OpQuerySelect) - if err := bps.prepareQuery(ctx); err != nil { +func (_s *BillingProfileSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingProfileQuery, *BillingProfileSelect](ctx, bps.BillingProfileQuery, bps, bps.inters, v) + return scanWithInterceptors[*BillingProfileQuery, *BillingProfileSelect](ctx, _s.BillingProfileQuery, _s, _s.inters, v) } -func (bps *BillingProfileSelect) sqlScan(ctx context.Context, root *BillingProfileQuery, v any) error { +func (_s *BillingProfileSelect) sqlScan(ctx context.Context, root *BillingProfileQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bps.fns)) - for _, fn := range bps.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bps.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -1014,7 +1014,7 @@ func (bps *BillingProfileSelect) sqlScan(ctx context.Context, root *BillingProfi } rows := &sql.Rows{} query, args := selector.Query() - if err := bps.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billingprofile_update.go b/openmeter/ent/db/billingprofile_update.go index 93c82b68b6..e351061827 100644 --- a/openmeter/ent/db/billingprofile_update.go +++ b/openmeter/ent/db/billingprofile_update.go @@ -475,10 +475,14 @@ func (_u *BillingProfileUpdate) sqlSave(ctx context.Context) (_node int, err err } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(billingprofile.FieldMetadata, field.TypeJSON, value) + vv, err := billingprofile.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(billingprofile.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(billingprofile.FieldMetadata, field.TypeJSON) + _spec.ClearField(billingprofile.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(billingprofile.FieldUpdatedAt, field.TypeTime, value) @@ -1164,10 +1168,14 @@ func (_u *BillingProfileUpdateOne) sqlSave(ctx context.Context) (_node *BillingP } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(billingprofile.FieldMetadata, field.TypeJSON, value) + vv, err := billingprofile.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(billingprofile.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(billingprofile.FieldMetadata, field.TypeJSON) + _spec.ClearField(billingprofile.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(billingprofile.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/billingsequencenumbers_query.go b/openmeter/ent/db/billingsequencenumbers_query.go index 9ac2dff62c..534fd7a942 100644 --- a/openmeter/ent/db/billingsequencenumbers_query.go +++ b/openmeter/ent/db/billingsequencenumbers_query.go @@ -480,41 +480,41 @@ type BillingSequenceNumbersGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bsngb *BillingSequenceNumbersGroupBy) Aggregate(fns ...AggregateFunc) *BillingSequenceNumbersGroupBy { - bsngb.fns = append(bsngb.fns, fns...) - return bsngb +func (_g *BillingSequenceNumbersGroupBy) Aggregate(fns ...AggregateFunc) *BillingSequenceNumbersGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bsngb *BillingSequenceNumbersGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bsngb.build.ctx, ent.OpQueryGroupBy) - if err := bsngb.build.prepareQuery(ctx); err != nil { +func (_g *BillingSequenceNumbersGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingSequenceNumbersQuery, *BillingSequenceNumbersGroupBy](ctx, bsngb.build, bsngb, bsngb.build.inters, v) + return scanWithInterceptors[*BillingSequenceNumbersQuery, *BillingSequenceNumbersGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bsngb *BillingSequenceNumbersGroupBy) sqlScan(ctx context.Context, root *BillingSequenceNumbersQuery, v any) error { +func (_g *BillingSequenceNumbersGroupBy) sqlScan(ctx context.Context, root *BillingSequenceNumbersQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bsngb.fns)) - for _, fn := range bsngb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bsngb.flds)+len(bsngb.fns)) - for _, f := range *bsngb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bsngb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bsngb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -528,27 +528,27 @@ type BillingSequenceNumbersSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bsns *BillingSequenceNumbersSelect) Aggregate(fns ...AggregateFunc) *BillingSequenceNumbersSelect { - bsns.fns = append(bsns.fns, fns...) - return bsns +func (_s *BillingSequenceNumbersSelect) Aggregate(fns ...AggregateFunc) *BillingSequenceNumbersSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bsns *BillingSequenceNumbersSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bsns.ctx, ent.OpQuerySelect) - if err := bsns.prepareQuery(ctx); err != nil { +func (_s *BillingSequenceNumbersSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingSequenceNumbersQuery, *BillingSequenceNumbersSelect](ctx, bsns.BillingSequenceNumbersQuery, bsns, bsns.inters, v) + return scanWithInterceptors[*BillingSequenceNumbersQuery, *BillingSequenceNumbersSelect](ctx, _s.BillingSequenceNumbersQuery, _s, _s.inters, v) } -func (bsns *BillingSequenceNumbersSelect) sqlScan(ctx context.Context, root *BillingSequenceNumbersQuery, v any) error { +func (_s *BillingSequenceNumbersSelect) sqlScan(ctx context.Context, root *BillingSequenceNumbersQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bsns.fns)) - for _, fn := range bsns.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bsns.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -556,7 +556,7 @@ func (bsns *BillingSequenceNumbersSelect) sqlScan(ctx context.Context, root *Bil } rows := &sql.Rows{} query, args := selector.Query() - if err := bsns.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/billingworkflowconfig_query.go b/openmeter/ent/db/billingworkflowconfig_query.go index 7ba20e93f3..15fe0dfda7 100644 --- a/openmeter/ent/db/billingworkflowconfig_query.go +++ b/openmeter/ent/db/billingworkflowconfig_query.go @@ -625,41 +625,41 @@ type BillingWorkflowConfigGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (bwcgb *BillingWorkflowConfigGroupBy) Aggregate(fns ...AggregateFunc) *BillingWorkflowConfigGroupBy { - bwcgb.fns = append(bwcgb.fns, fns...) - return bwcgb +func (_g *BillingWorkflowConfigGroupBy) Aggregate(fns ...AggregateFunc) *BillingWorkflowConfigGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (bwcgb *BillingWorkflowConfigGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bwcgb.build.ctx, ent.OpQueryGroupBy) - if err := bwcgb.build.prepareQuery(ctx); err != nil { +func (_g *BillingWorkflowConfigGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingWorkflowConfigQuery, *BillingWorkflowConfigGroupBy](ctx, bwcgb.build, bwcgb, bwcgb.build.inters, v) + return scanWithInterceptors[*BillingWorkflowConfigQuery, *BillingWorkflowConfigGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (bwcgb *BillingWorkflowConfigGroupBy) sqlScan(ctx context.Context, root *BillingWorkflowConfigQuery, v any) error { +func (_g *BillingWorkflowConfigGroupBy) sqlScan(ctx context.Context, root *BillingWorkflowConfigQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(bwcgb.fns)) - for _, fn := range bwcgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*bwcgb.flds)+len(bwcgb.fns)) - for _, f := range *bwcgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*bwcgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := bwcgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -673,27 +673,27 @@ type BillingWorkflowConfigSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (bwcs *BillingWorkflowConfigSelect) Aggregate(fns ...AggregateFunc) *BillingWorkflowConfigSelect { - bwcs.fns = append(bwcs.fns, fns...) - return bwcs +func (_s *BillingWorkflowConfigSelect) Aggregate(fns ...AggregateFunc) *BillingWorkflowConfigSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (bwcs *BillingWorkflowConfigSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, bwcs.ctx, ent.OpQuerySelect) - if err := bwcs.prepareQuery(ctx); err != nil { +func (_s *BillingWorkflowConfigSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*BillingWorkflowConfigQuery, *BillingWorkflowConfigSelect](ctx, bwcs.BillingWorkflowConfigQuery, bwcs, bwcs.inters, v) + return scanWithInterceptors[*BillingWorkflowConfigQuery, *BillingWorkflowConfigSelect](ctx, _s.BillingWorkflowConfigQuery, _s, _s.inters, v) } -func (bwcs *BillingWorkflowConfigSelect) sqlScan(ctx context.Context, root *BillingWorkflowConfigQuery, v any) error { +func (_s *BillingWorkflowConfigSelect) sqlScan(ctx context.Context, root *BillingWorkflowConfigQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(bwcs.fns)) - for _, fn := range bwcs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*bwcs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -701,7 +701,7 @@ func (bwcs *BillingWorkflowConfigSelect) sqlScan(ctx context.Context, root *Bill } rows := &sql.Rows{} query, args := selector.Query() - if err := bwcs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/customer.go b/openmeter/ent/db/customer.go index d0aa2268c9..c6a19501e2 100644 --- a/openmeter/ent/db/customer.go +++ b/openmeter/ent/db/customer.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -132,12 +131,14 @@ func (*Customer) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case customer.FieldMetadata, customer.FieldAnnotations: - values[i] = new([]byte) case customer.FieldID, customer.FieldNamespace, customer.FieldName, customer.FieldDescription, customer.FieldBillingAddressCountry, customer.FieldBillingAddressPostalCode, customer.FieldBillingAddressState, customer.FieldBillingAddressCity, customer.FieldBillingAddressLine1, customer.FieldBillingAddressLine2, customer.FieldBillingAddressPhoneNumber, customer.FieldKey, customer.FieldPrimaryEmail, customer.FieldCurrency: values[i] = new(sql.NullString) case customer.FieldCreatedAt, customer.FieldUpdatedAt, customer.FieldDeletedAt: values[i] = new(sql.NullTime) + case customer.FieldMetadata: + values[i] = customer.ValueScanner.Metadata.ScanValue() + case customer.FieldAnnotations: + values[i] = customer.ValueScanner.Annotations.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -166,12 +167,10 @@ func (_m *Customer) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case customer.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := customer.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case customer.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { @@ -255,12 +254,10 @@ func (_m *Customer) assignValues(columns []string, values []any) error { *_m.BillingAddressPhoneNumber = value.String } case customer.FieldAnnotations: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field annotations", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Annotations); err != nil { - return fmt.Errorf("unmarshal field annotations: %w", err) - } + if value, err := customer.ValueScanner.Annotations.FromValue(values[i]); err != nil { + return err + } else { + _m.Annotations = value } case customer.FieldKey: if value, ok := values[i].(*sql.NullString); !ok { diff --git a/openmeter/ent/db/customer/customer.go b/openmeter/ent/db/customer/customer.go index 5b688e24d8..4bb39eb1f5 100644 --- a/openmeter/ent/db/customer/customer.go +++ b/openmeter/ent/db/customer/customer.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -147,6 +148,11 @@ var ( CurrencyValidator func(string) error // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all Customer fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + Annotations field.TypeValueScanner[map[string]interface{}] + } ) // OrderOption defines the ordering options for the Customer queries. @@ -162,6 +168,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() @@ -222,6 +233,11 @@ func ByBillingAddressPhoneNumber(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldBillingAddressPhoneNumber, opts...).ToFunc() } +// ByAnnotations orders the results by the annotations field. +func ByAnnotations(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldAnnotations, opts...).ToFunc() +} + // ByKey orders the results by the key field. func ByKey(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldKey, opts...).ToFunc() diff --git a/openmeter/ent/db/customer_create.go b/openmeter/ent/db/customer_create.go index 526d775a14..22ccb453d3 100644 --- a/openmeter/ent/db/customer_create.go +++ b/openmeter/ent/db/customer_create.go @@ -428,7 +428,10 @@ func (_c *CustomerCreate) sqlSave(ctx context.Context) (*Customer, error) { if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -447,7 +450,7 @@ func (_c *CustomerCreate) sqlSave(ctx context.Context) (*Customer, error) { return _node, nil } -func (_c *CustomerCreate) createSpec() (*Customer, *sqlgraph.CreateSpec) { +func (_c *CustomerCreate) createSpec() (*Customer, *sqlgraph.CreateSpec, error) { var ( _node = &Customer{config: _c.config} _spec = sqlgraph.NewCreateSpec(customer.Table, sqlgraph.NewFieldSpec(customer.FieldID, field.TypeString)) @@ -462,7 +465,11 @@ func (_c *CustomerCreate) createSpec() (*Customer, *sqlgraph.CreateSpec) { _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(customer.FieldMetadata, field.TypeJSON, value) + vv, err := customer.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(customer.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -514,7 +521,11 @@ func (_c *CustomerCreate) createSpec() (*Customer, *sqlgraph.CreateSpec) { _node.BillingAddressPhoneNumber = &value } if value, ok := _c.mutation.Annotations(); ok { - _spec.SetField(customer.FieldAnnotations, field.TypeJSON, value) + vv, err := customer.ValueScanner.Annotations.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(customer.FieldAnnotations, field.TypeString, vv) _node.Annotations = value } if value, ok := _c.mutation.Key(); ok { @@ -609,7 +620,7 @@ func (_c *CustomerCreate) createSpec() (*Customer, *sqlgraph.CreateSpec) { } _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -1381,7 +1392,10 @@ func (_c *CustomerCreateBulk) Save(ctx context.Context) ([]*Customer, error) { } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/customer_query.go b/openmeter/ent/db/customer_query.go index f0a13f8486..b4da920ce1 100644 --- a/openmeter/ent/db/customer_query.go +++ b/openmeter/ent/db/customer_query.go @@ -851,41 +851,41 @@ type CustomerGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (cgb *CustomerGroupBy) Aggregate(fns ...AggregateFunc) *CustomerGroupBy { - cgb.fns = append(cgb.fns, fns...) - return cgb +func (_g *CustomerGroupBy) Aggregate(fns ...AggregateFunc) *CustomerGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (cgb *CustomerGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, cgb.build.ctx, ent.OpQueryGroupBy) - if err := cgb.build.prepareQuery(ctx); err != nil { +func (_g *CustomerGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*CustomerQuery, *CustomerGroupBy](ctx, cgb.build, cgb, cgb.build.inters, v) + return scanWithInterceptors[*CustomerQuery, *CustomerGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (cgb *CustomerGroupBy) sqlScan(ctx context.Context, root *CustomerQuery, v any) error { +func (_g *CustomerGroupBy) sqlScan(ctx context.Context, root *CustomerQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(cgb.fns)) - for _, fn := range cgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*cgb.flds)+len(cgb.fns)) - for _, f := range *cgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*cgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := cgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -899,27 +899,27 @@ type CustomerSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (cs *CustomerSelect) Aggregate(fns ...AggregateFunc) *CustomerSelect { - cs.fns = append(cs.fns, fns...) - return cs +func (_s *CustomerSelect) Aggregate(fns ...AggregateFunc) *CustomerSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (cs *CustomerSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, cs.ctx, ent.OpQuerySelect) - if err := cs.prepareQuery(ctx); err != nil { +func (_s *CustomerSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*CustomerQuery, *CustomerSelect](ctx, cs.CustomerQuery, cs, cs.inters, v) + return scanWithInterceptors[*CustomerQuery, *CustomerSelect](ctx, _s.CustomerQuery, _s, _s.inters, v) } -func (cs *CustomerSelect) sqlScan(ctx context.Context, root *CustomerQuery, v any) error { +func (_s *CustomerSelect) sqlScan(ctx context.Context, root *CustomerQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(cs.fns)) - for _, fn := range cs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*cs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -927,7 +927,7 @@ func (cs *CustomerSelect) sqlScan(ctx context.Context, root *CustomerQuery, v an } rows := &sql.Rows{} query, args := selector.Query() - if err := cs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/customer_update.go b/openmeter/ent/db/customer_update.go index 959a27e68c..9fce660bea 100644 --- a/openmeter/ent/db/customer_update.go +++ b/openmeter/ent/db/customer_update.go @@ -557,10 +557,14 @@ func (_u *CustomerUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(customer.FieldMetadata, field.TypeJSON, value) + vv, err := customer.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(customer.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(customer.FieldMetadata, field.TypeJSON) + _spec.ClearField(customer.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(customer.FieldUpdatedAt, field.TypeTime, value) @@ -623,10 +627,14 @@ func (_u *CustomerUpdate) sqlSave(ctx context.Context) (_node int, err error) { _spec.ClearField(customer.FieldBillingAddressPhoneNumber, field.TypeString) } if value, ok := _u.mutation.Annotations(); ok { - _spec.SetField(customer.FieldAnnotations, field.TypeJSON, value) + vv, err := customer.ValueScanner.Annotations.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(customer.FieldAnnotations, field.TypeString, vv) } if _u.mutation.AnnotationsCleared() { - _spec.ClearField(customer.FieldAnnotations, field.TypeJSON) + _spec.ClearField(customer.FieldAnnotations, field.TypeString) } if value, ok := _u.mutation.Key(); ok { _spec.SetField(customer.FieldKey, field.TypeString, value) @@ -1427,10 +1435,14 @@ func (_u *CustomerUpdateOne) sqlSave(ctx context.Context) (_node *Customer, err } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(customer.FieldMetadata, field.TypeJSON, value) + vv, err := customer.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(customer.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(customer.FieldMetadata, field.TypeJSON) + _spec.ClearField(customer.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(customer.FieldUpdatedAt, field.TypeTime, value) @@ -1493,10 +1505,14 @@ func (_u *CustomerUpdateOne) sqlSave(ctx context.Context) (_node *Customer, err _spec.ClearField(customer.FieldBillingAddressPhoneNumber, field.TypeString) } if value, ok := _u.mutation.Annotations(); ok { - _spec.SetField(customer.FieldAnnotations, field.TypeJSON, value) + vv, err := customer.ValueScanner.Annotations.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(customer.FieldAnnotations, field.TypeString, vv) } if _u.mutation.AnnotationsCleared() { - _spec.ClearField(customer.FieldAnnotations, field.TypeJSON) + _spec.ClearField(customer.FieldAnnotations, field.TypeString) } if value, ok := _u.mutation.Key(); ok { _spec.SetField(customer.FieldKey, field.TypeString, value) diff --git a/openmeter/ent/db/customersubjects_query.go b/openmeter/ent/db/customersubjects_query.go index a3b4aa3cc7..d9cc978695 100644 --- a/openmeter/ent/db/customersubjects_query.go +++ b/openmeter/ent/db/customersubjects_query.go @@ -559,41 +559,41 @@ type CustomerSubjectsGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (csgb *CustomerSubjectsGroupBy) Aggregate(fns ...AggregateFunc) *CustomerSubjectsGroupBy { - csgb.fns = append(csgb.fns, fns...) - return csgb +func (_g *CustomerSubjectsGroupBy) Aggregate(fns ...AggregateFunc) *CustomerSubjectsGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (csgb *CustomerSubjectsGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, csgb.build.ctx, ent.OpQueryGroupBy) - if err := csgb.build.prepareQuery(ctx); err != nil { +func (_g *CustomerSubjectsGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*CustomerSubjectsQuery, *CustomerSubjectsGroupBy](ctx, csgb.build, csgb, csgb.build.inters, v) + return scanWithInterceptors[*CustomerSubjectsQuery, *CustomerSubjectsGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (csgb *CustomerSubjectsGroupBy) sqlScan(ctx context.Context, root *CustomerSubjectsQuery, v any) error { +func (_g *CustomerSubjectsGroupBy) sqlScan(ctx context.Context, root *CustomerSubjectsQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(csgb.fns)) - for _, fn := range csgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*csgb.flds)+len(csgb.fns)) - for _, f := range *csgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*csgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := csgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -607,27 +607,27 @@ type CustomerSubjectsSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (css *CustomerSubjectsSelect) Aggregate(fns ...AggregateFunc) *CustomerSubjectsSelect { - css.fns = append(css.fns, fns...) - return css +func (_s *CustomerSubjectsSelect) Aggregate(fns ...AggregateFunc) *CustomerSubjectsSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (css *CustomerSubjectsSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, css.ctx, ent.OpQuerySelect) - if err := css.prepareQuery(ctx); err != nil { +func (_s *CustomerSubjectsSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*CustomerSubjectsQuery, *CustomerSubjectsSelect](ctx, css.CustomerSubjectsQuery, css, css.inters, v) + return scanWithInterceptors[*CustomerSubjectsQuery, *CustomerSubjectsSelect](ctx, _s.CustomerSubjectsQuery, _s, _s.inters, v) } -func (css *CustomerSubjectsSelect) sqlScan(ctx context.Context, root *CustomerSubjectsQuery, v any) error { +func (_s *CustomerSubjectsSelect) sqlScan(ctx context.Context, root *CustomerSubjectsQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(css.fns)) - for _, fn := range css.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*css.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -635,7 +635,7 @@ func (css *CustomerSubjectsSelect) sqlScan(ctx context.Context, root *CustomerSu } rows := &sql.Rows{} query, args := selector.Query() - if err := css.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/entitlement.go b/openmeter/ent/db/entitlement.go index be23ff7445..d99457033c 100644 --- a/openmeter/ent/db/entitlement.go +++ b/openmeter/ent/db/entitlement.go @@ -139,7 +139,7 @@ func (*Entitlement) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case entitlement.FieldMetadata, entitlement.FieldConfig: + case entitlement.FieldConfig: values[i] = new([]byte) case entitlement.FieldIsSoftLimit, entitlement.FieldPreserveOverageAtReset: values[i] = new(sql.NullBool) @@ -151,6 +151,8 @@ func (*Entitlement) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullString) case entitlement.FieldCreatedAt, entitlement.FieldUpdatedAt, entitlement.FieldDeletedAt, entitlement.FieldActiveFrom, entitlement.FieldActiveTo, entitlement.FieldMeasureUsageFrom, entitlement.FieldUsagePeriodAnchor, entitlement.FieldCurrentUsagePeriodStart, entitlement.FieldCurrentUsagePeriodEnd: values[i] = new(sql.NullTime) + case entitlement.FieldMetadata: + values[i] = entitlement.ValueScanner.Metadata.ScanValue() case entitlement.FieldAnnotations: values[i] = entitlement.ValueScanner.Annotations.ScanValue() default: @@ -181,12 +183,10 @@ func (_m *Entitlement) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case entitlement.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := entitlement.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case entitlement.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/entitlement/entitlement.go b/openmeter/ent/db/entitlement/entitlement.go index e57e6b5b7c..3ff0c4462f 100644 --- a/openmeter/ent/db/entitlement/entitlement.go +++ b/openmeter/ent/db/entitlement/entitlement.go @@ -163,6 +163,7 @@ var ( DefaultID func() string // ValueScanner of all Entitlement fields. ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] Annotations field.TypeValueScanner[map[string]interface{}] } ) @@ -204,6 +205,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/entitlement_create.go b/openmeter/ent/db/entitlement_create.go index ac378396c2..b3c0a69aca 100644 --- a/openmeter/ent/db/entitlement_create.go +++ b/openmeter/ent/db/entitlement_create.go @@ -491,7 +491,11 @@ func (_c *EntitlementCreate) createSpec() (*Entitlement, *sqlgraph.CreateSpec, e _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(entitlement.FieldMetadata, field.TypeJSON, value) + vv, err := entitlement.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(entitlement.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { diff --git a/openmeter/ent/db/entitlement_query.go b/openmeter/ent/db/entitlement_query.go index 75b9ab0ae0..b356c4b961 100644 --- a/openmeter/ent/db/entitlement_query.go +++ b/openmeter/ent/db/entitlement_query.go @@ -861,41 +861,41 @@ type EntitlementGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (egb *EntitlementGroupBy) Aggregate(fns ...AggregateFunc) *EntitlementGroupBy { - egb.fns = append(egb.fns, fns...) - return egb +func (_g *EntitlementGroupBy) Aggregate(fns ...AggregateFunc) *EntitlementGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (egb *EntitlementGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, egb.build.ctx, ent.OpQueryGroupBy) - if err := egb.build.prepareQuery(ctx); err != nil { +func (_g *EntitlementGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*EntitlementQuery, *EntitlementGroupBy](ctx, egb.build, egb, egb.build.inters, v) + return scanWithInterceptors[*EntitlementQuery, *EntitlementGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (egb *EntitlementGroupBy) sqlScan(ctx context.Context, root *EntitlementQuery, v any) error { +func (_g *EntitlementGroupBy) sqlScan(ctx context.Context, root *EntitlementQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(egb.fns)) - for _, fn := range egb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*egb.flds)+len(egb.fns)) - for _, f := range *egb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*egb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := egb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -909,27 +909,27 @@ type EntitlementSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (es *EntitlementSelect) Aggregate(fns ...AggregateFunc) *EntitlementSelect { - es.fns = append(es.fns, fns...) - return es +func (_s *EntitlementSelect) Aggregate(fns ...AggregateFunc) *EntitlementSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (es *EntitlementSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, es.ctx, ent.OpQuerySelect) - if err := es.prepareQuery(ctx); err != nil { +func (_s *EntitlementSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*EntitlementQuery, *EntitlementSelect](ctx, es.EntitlementQuery, es, es.inters, v) + return scanWithInterceptors[*EntitlementQuery, *EntitlementSelect](ctx, _s.EntitlementQuery, _s, _s.inters, v) } -func (es *EntitlementSelect) sqlScan(ctx context.Context, root *EntitlementQuery, v any) error { +func (_s *EntitlementSelect) sqlScan(ctx context.Context, root *EntitlementQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(es.fns)) - for _, fn := range es.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*es.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -937,7 +937,7 @@ func (es *EntitlementSelect) sqlScan(ctx context.Context, root *EntitlementQuery } rows := &sql.Rows{} query, args := selector.Query() - if err := es.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/entitlement_update.go b/openmeter/ent/db/entitlement_update.go index d1492707f7..02ae1835fe 100644 --- a/openmeter/ent/db/entitlement_update.go +++ b/openmeter/ent/db/entitlement_update.go @@ -387,10 +387,14 @@ func (_u *EntitlementUpdate) sqlSave(ctx context.Context) (_node int, err error) } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(entitlement.FieldMetadata, field.TypeJSON, value) + vv, err := entitlement.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(entitlement.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(entitlement.FieldMetadata, field.TypeJSON) + _spec.ClearField(entitlement.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(entitlement.FieldUpdatedAt, field.TypeTime, value) @@ -1051,10 +1055,14 @@ func (_u *EntitlementUpdateOne) sqlSave(ctx context.Context) (_node *Entitlement } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(entitlement.FieldMetadata, field.TypeJSON, value) + vv, err := entitlement.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(entitlement.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(entitlement.FieldMetadata, field.TypeJSON) + _spec.ClearField(entitlement.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(entitlement.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/feature.go b/openmeter/ent/db/feature.go index 370eb425a2..13047dd13c 100644 --- a/openmeter/ent/db/feature.go +++ b/openmeter/ent/db/feature.go @@ -89,12 +89,14 @@ func (*Feature) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case feature.FieldMetadata, feature.FieldMeterGroupByFilters: + case feature.FieldMeterGroupByFilters: values[i] = new([]byte) case feature.FieldID, feature.FieldNamespace, feature.FieldName, feature.FieldKey, feature.FieldMeterSlug: values[i] = new(sql.NullString) case feature.FieldCreatedAt, feature.FieldUpdatedAt, feature.FieldDeletedAt, feature.FieldArchivedAt: values[i] = new(sql.NullTime) + case feature.FieldMetadata: + values[i] = feature.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -136,12 +138,10 @@ func (_m *Feature) assignValues(columns []string, values []any) error { *_m.DeletedAt = value.Time } case feature.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := feature.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case feature.FieldNamespace: if value, ok := values[i].(*sql.NullString); !ok { diff --git a/openmeter/ent/db/feature/feature.go b/openmeter/ent/db/feature/feature.go index 1d3572b97b..5daf35de39 100644 --- a/openmeter/ent/db/feature/feature.go +++ b/openmeter/ent/db/feature/feature.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -105,6 +106,10 @@ var ( KeyValidator func(string) error // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all Feature fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // OrderOption defines the ordering options for the Feature queries. @@ -130,6 +135,11 @@ func ByDeletedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldDeletedAt, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByNamespace orders the results by the namespace field. func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() diff --git a/openmeter/ent/db/feature_create.go b/openmeter/ent/db/feature_create.go index a5ed9cc3f5..2a091cf39d 100644 --- a/openmeter/ent/db/feature_create.go +++ b/openmeter/ent/db/feature_create.go @@ -273,7 +273,10 @@ func (_c *FeatureCreate) sqlSave(ctx context.Context) (*Feature, error) { if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -292,7 +295,7 @@ func (_c *FeatureCreate) sqlSave(ctx context.Context) (*Feature, error) { return _node, nil } -func (_c *FeatureCreate) createSpec() (*Feature, *sqlgraph.CreateSpec) { +func (_c *FeatureCreate) createSpec() (*Feature, *sqlgraph.CreateSpec, error) { var ( _node = &Feature{config: _c.config} _spec = sqlgraph.NewCreateSpec(feature.Table, sqlgraph.NewFieldSpec(feature.FieldID, field.TypeString)) @@ -315,7 +318,11 @@ func (_c *FeatureCreate) createSpec() (*Feature, *sqlgraph.CreateSpec) { _node.DeletedAt = &value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(feature.FieldMetadata, field.TypeJSON, value) + vv, err := feature.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(feature.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.Namespace(); ok { @@ -390,7 +397,7 @@ func (_c *FeatureCreate) createSpec() (*Feature, *sqlgraph.CreateSpec) { } _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -778,7 +785,10 @@ func (_c *FeatureCreateBulk) Save(ctx context.Context) ([]*Feature, error) { } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/feature_query.go b/openmeter/ent/db/feature_query.go index e60bac1ed0..f756833d93 100644 --- a/openmeter/ent/db/feature_query.go +++ b/openmeter/ent/db/feature_query.go @@ -713,41 +713,41 @@ type FeatureGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (fgb *FeatureGroupBy) Aggregate(fns ...AggregateFunc) *FeatureGroupBy { - fgb.fns = append(fgb.fns, fns...) - return fgb +func (_g *FeatureGroupBy) Aggregate(fns ...AggregateFunc) *FeatureGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (fgb *FeatureGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, fgb.build.ctx, ent.OpQueryGroupBy) - if err := fgb.build.prepareQuery(ctx); err != nil { +func (_g *FeatureGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*FeatureQuery, *FeatureGroupBy](ctx, fgb.build, fgb, fgb.build.inters, v) + return scanWithInterceptors[*FeatureQuery, *FeatureGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (fgb *FeatureGroupBy) sqlScan(ctx context.Context, root *FeatureQuery, v any) error { +func (_g *FeatureGroupBy) sqlScan(ctx context.Context, root *FeatureQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(fgb.fns)) - for _, fn := range fgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*fgb.flds)+len(fgb.fns)) - for _, f := range *fgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*fgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := fgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -761,27 +761,27 @@ type FeatureSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (fs *FeatureSelect) Aggregate(fns ...AggregateFunc) *FeatureSelect { - fs.fns = append(fs.fns, fns...) - return fs +func (_s *FeatureSelect) Aggregate(fns ...AggregateFunc) *FeatureSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (fs *FeatureSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, fs.ctx, ent.OpQuerySelect) - if err := fs.prepareQuery(ctx); err != nil { +func (_s *FeatureSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*FeatureQuery, *FeatureSelect](ctx, fs.FeatureQuery, fs, fs.inters, v) + return scanWithInterceptors[*FeatureQuery, *FeatureSelect](ctx, _s.FeatureQuery, _s, _s.inters, v) } -func (fs *FeatureSelect) sqlScan(ctx context.Context, root *FeatureQuery, v any) error { +func (_s *FeatureSelect) sqlScan(ctx context.Context, root *FeatureQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(fs.fns)) - for _, fn := range fs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*fs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -789,7 +789,7 @@ func (fs *FeatureSelect) sqlScan(ctx context.Context, root *FeatureQuery, v any) } rows := &sql.Rows{} query, args := selector.Query() - if err := fs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/feature_update.go b/openmeter/ent/db/feature_update.go index 959e49375a..496e4db5ee 100644 --- a/openmeter/ent/db/feature_update.go +++ b/openmeter/ent/db/feature_update.go @@ -296,10 +296,14 @@ func (_u *FeatureUpdate) sqlSave(ctx context.Context) (_node int, err error) { _spec.ClearField(feature.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(feature.FieldMetadata, field.TypeJSON, value) + vv, err := feature.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(feature.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(feature.FieldMetadata, field.TypeJSON) + _spec.ClearField(feature.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.Name(); ok { _spec.SetField(feature.FieldName, field.TypeString, value) @@ -769,10 +773,14 @@ func (_u *FeatureUpdateOne) sqlSave(ctx context.Context) (_node *Feature, err er _spec.ClearField(feature.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(feature.FieldMetadata, field.TypeJSON, value) + vv, err := feature.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(feature.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(feature.FieldMetadata, field.TypeJSON) + _spec.ClearField(feature.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.Name(); ok { _spec.SetField(feature.FieldName, field.TypeString, value) diff --git a/openmeter/ent/db/grant.go b/openmeter/ent/db/grant.go index 126212d8c0..9e0bcd8843 100644 --- a/openmeter/ent/db/grant.go +++ b/openmeter/ent/db/grant.go @@ -84,7 +84,7 @@ func (*Grant) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case dbgrant.FieldMetadata, dbgrant.FieldExpiration: + case dbgrant.FieldExpiration: values[i] = new([]byte) case dbgrant.FieldAmount, dbgrant.FieldResetMaxRollover, dbgrant.FieldResetMinRollover: values[i] = new(sql.NullFloat64) @@ -94,6 +94,8 @@ func (*Grant) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullString) case dbgrant.FieldCreatedAt, dbgrant.FieldUpdatedAt, dbgrant.FieldDeletedAt, dbgrant.FieldEffectiveAt, dbgrant.FieldExpiresAt, dbgrant.FieldVoidedAt, dbgrant.FieldRecurrenceAnchor: values[i] = new(sql.NullTime) + case dbgrant.FieldMetadata: + values[i] = dbgrant.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -122,12 +124,10 @@ func (_m *Grant) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case dbgrant.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := dbgrant.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case dbgrant.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/grant/grant.go b/openmeter/ent/db/grant/grant.go index 6d326a2a37..4a1b42699c 100644 --- a/openmeter/ent/db/grant/grant.go +++ b/openmeter/ent/db/grant/grant.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -103,6 +104,10 @@ var ( DefaultPriority uint8 // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all Grant fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // OrderOption defines the ordering options for the Grant queries. @@ -118,6 +123,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/grant_create.go b/openmeter/ent/db/grant_create.go index 58599be122..d04771fbc9 100644 --- a/openmeter/ent/db/grant_create.go +++ b/openmeter/ent/db/grant_create.go @@ -306,7 +306,10 @@ func (_c *GrantCreate) sqlSave(ctx context.Context) (*Grant, error) { if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -325,7 +328,7 @@ func (_c *GrantCreate) sqlSave(ctx context.Context) (*Grant, error) { return _node, nil } -func (_c *GrantCreate) createSpec() (*Grant, *sqlgraph.CreateSpec) { +func (_c *GrantCreate) createSpec() (*Grant, *sqlgraph.CreateSpec, error) { var ( _node = &Grant{config: _c.config} _spec = sqlgraph.NewCreateSpec(dbgrant.Table, sqlgraph.NewFieldSpec(dbgrant.FieldID, field.TypeString)) @@ -340,7 +343,11 @@ func (_c *GrantCreate) createSpec() (*Grant, *sqlgraph.CreateSpec) { _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(dbgrant.FieldMetadata, field.TypeJSON, value) + vv, err := dbgrant.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(dbgrant.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -412,7 +419,7 @@ func (_c *GrantCreate) createSpec() (*Grant, *sqlgraph.CreateSpec) { _node.OwnerID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -759,7 +766,10 @@ func (_c *GrantCreateBulk) Save(ctx context.Context) ([]*Grant, error) { } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/grant_query.go b/openmeter/ent/db/grant_query.go index ac9927278a..2a24bf0d1f 100644 --- a/openmeter/ent/db/grant_query.go +++ b/openmeter/ent/db/grant_query.go @@ -559,41 +559,41 @@ type GrantGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (ggb *GrantGroupBy) Aggregate(fns ...AggregateFunc) *GrantGroupBy { - ggb.fns = append(ggb.fns, fns...) - return ggb +func (_g *GrantGroupBy) Aggregate(fns ...AggregateFunc) *GrantGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (ggb *GrantGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ggb.build.ctx, ent.OpQueryGroupBy) - if err := ggb.build.prepareQuery(ctx); err != nil { +func (_g *GrantGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*GrantQuery, *GrantGroupBy](ctx, ggb.build, ggb, ggb.build.inters, v) + return scanWithInterceptors[*GrantQuery, *GrantGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (ggb *GrantGroupBy) sqlScan(ctx context.Context, root *GrantQuery, v any) error { +func (_g *GrantGroupBy) sqlScan(ctx context.Context, root *GrantQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(ggb.fns)) - for _, fn := range ggb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*ggb.flds)+len(ggb.fns)) - for _, f := range *ggb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*ggb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := ggb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -607,27 +607,27 @@ type GrantSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (gs *GrantSelect) Aggregate(fns ...AggregateFunc) *GrantSelect { - gs.fns = append(gs.fns, fns...) - return gs +func (_s *GrantSelect) Aggregate(fns ...AggregateFunc) *GrantSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (gs *GrantSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, gs.ctx, ent.OpQuerySelect) - if err := gs.prepareQuery(ctx); err != nil { +func (_s *GrantSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*GrantQuery, *GrantSelect](ctx, gs.GrantQuery, gs, gs.inters, v) + return scanWithInterceptors[*GrantQuery, *GrantSelect](ctx, _s.GrantQuery, _s, _s.inters, v) } -func (gs *GrantSelect) sqlScan(ctx context.Context, root *GrantQuery, v any) error { +func (_s *GrantSelect) sqlScan(ctx context.Context, root *GrantQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(gs.fns)) - for _, fn := range gs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*gs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -635,7 +635,7 @@ func (gs *GrantSelect) sqlScan(ctx context.Context, root *GrantQuery, v any) err } rows := &sql.Rows{} query, args := selector.Query() - if err := gs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/grant_update.go b/openmeter/ent/db/grant_update.go index b4e8902441..5aef6992f3 100644 --- a/openmeter/ent/db/grant_update.go +++ b/openmeter/ent/db/grant_update.go @@ -148,10 +148,14 @@ func (_u *GrantUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(dbgrant.FieldMetadata, field.TypeJSON, value) + vv, err := dbgrant.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(dbgrant.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(dbgrant.FieldMetadata, field.TypeJSON) + _spec.ClearField(dbgrant.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(dbgrant.FieldUpdatedAt, field.TypeTime, value) @@ -344,10 +348,14 @@ func (_u *GrantUpdateOne) sqlSave(ctx context.Context) (_node *Grant, err error) } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(dbgrant.FieldMetadata, field.TypeJSON, value) + vv, err := dbgrant.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(dbgrant.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(dbgrant.FieldMetadata, field.TypeJSON) + _spec.ClearField(dbgrant.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(dbgrant.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/meter.go b/openmeter/ent/db/meter.go index 420cc73ae6..f1a4f418e2 100644 --- a/openmeter/ent/db/meter.go +++ b/openmeter/ent/db/meter.go @@ -53,12 +53,14 @@ func (*Meter) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case dbmeter.FieldMetadata, dbmeter.FieldGroupBy: + case dbmeter.FieldGroupBy: values[i] = new([]byte) case dbmeter.FieldID, dbmeter.FieldNamespace, dbmeter.FieldName, dbmeter.FieldDescription, dbmeter.FieldKey, dbmeter.FieldEventType, dbmeter.FieldValueProperty, dbmeter.FieldAggregation: values[i] = new(sql.NullString) case dbmeter.FieldCreatedAt, dbmeter.FieldUpdatedAt, dbmeter.FieldDeletedAt, dbmeter.FieldEventFrom: values[i] = new(sql.NullTime) + case dbmeter.FieldMetadata: + values[i] = dbmeter.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -87,12 +89,10 @@ func (_m *Meter) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case dbmeter.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := dbmeter.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case dbmeter.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/meter/meter.go b/openmeter/ent/db/meter/meter.go index db90c7e96c..b116da5599 100644 --- a/openmeter/ent/db/meter/meter.go +++ b/openmeter/ent/db/meter/meter.go @@ -7,6 +7,7 @@ import ( "time" "entgo.io/ent/dialect/sql" + "entgo.io/ent/schema/field" "github.com/openmeterio/openmeter/openmeter/meter" ) @@ -88,6 +89,10 @@ var ( EventTypeValidator func(string) error // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all Meter fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // AggregationValidator is a validator for the "aggregation" field enum values. It is called by the builders before save. @@ -113,6 +118,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/meter_create.go b/openmeter/ent/db/meter_create.go index 52570760da..0e3ed0f370 100644 --- a/openmeter/ent/db/meter_create.go +++ b/openmeter/ent/db/meter_create.go @@ -263,7 +263,10 @@ func (_c *MeterCreate) sqlSave(ctx context.Context) (*Meter, error) { if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -282,7 +285,7 @@ func (_c *MeterCreate) sqlSave(ctx context.Context) (*Meter, error) { return _node, nil } -func (_c *MeterCreate) createSpec() (*Meter, *sqlgraph.CreateSpec) { +func (_c *MeterCreate) createSpec() (*Meter, *sqlgraph.CreateSpec, error) { var ( _node = &Meter{config: _c.config} _spec = sqlgraph.NewCreateSpec(dbmeter.Table, sqlgraph.NewFieldSpec(dbmeter.FieldID, field.TypeString)) @@ -297,7 +300,11 @@ func (_c *MeterCreate) createSpec() (*Meter, *sqlgraph.CreateSpec) { _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(dbmeter.FieldMetadata, field.TypeJSON, value) + vv, err := dbmeter.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(dbmeter.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -344,7 +351,7 @@ func (_c *MeterCreate) createSpec() (*Meter, *sqlgraph.CreateSpec) { _spec.SetField(dbmeter.FieldEventFrom, field.TypeTime, value) _node.EventFrom = &value } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -813,7 +820,10 @@ func (_c *MeterCreateBulk) Save(ctx context.Context) ([]*Meter, error) { } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/meter_query.go b/openmeter/ent/db/meter_query.go index 0f2158dcdc..317c7045db 100644 --- a/openmeter/ent/db/meter_query.go +++ b/openmeter/ent/db/meter_query.go @@ -480,41 +480,41 @@ type MeterGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (mgb *MeterGroupBy) Aggregate(fns ...AggregateFunc) *MeterGroupBy { - mgb.fns = append(mgb.fns, fns...) - return mgb +func (_g *MeterGroupBy) Aggregate(fns ...AggregateFunc) *MeterGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (mgb *MeterGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, mgb.build.ctx, ent.OpQueryGroupBy) - if err := mgb.build.prepareQuery(ctx); err != nil { +func (_g *MeterGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*MeterQuery, *MeterGroupBy](ctx, mgb.build, mgb, mgb.build.inters, v) + return scanWithInterceptors[*MeterQuery, *MeterGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (mgb *MeterGroupBy) sqlScan(ctx context.Context, root *MeterQuery, v any) error { +func (_g *MeterGroupBy) sqlScan(ctx context.Context, root *MeterQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(mgb.fns)) - for _, fn := range mgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*mgb.flds)+len(mgb.fns)) - for _, f := range *mgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*mgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := mgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -528,27 +528,27 @@ type MeterSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (ms *MeterSelect) Aggregate(fns ...AggregateFunc) *MeterSelect { - ms.fns = append(ms.fns, fns...) - return ms +func (_s *MeterSelect) Aggregate(fns ...AggregateFunc) *MeterSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (ms *MeterSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ms.ctx, ent.OpQuerySelect) - if err := ms.prepareQuery(ctx); err != nil { +func (_s *MeterSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*MeterQuery, *MeterSelect](ctx, ms.MeterQuery, ms, ms.inters, v) + return scanWithInterceptors[*MeterQuery, *MeterSelect](ctx, _s.MeterQuery, _s, _s.inters, v) } -func (ms *MeterSelect) sqlScan(ctx context.Context, root *MeterQuery, v any) error { +func (_s *MeterSelect) sqlScan(ctx context.Context, root *MeterQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(ms.fns)) - for _, fn := range ms.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*ms.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -556,7 +556,7 @@ func (ms *MeterSelect) sqlScan(ctx context.Context, root *MeterQuery, v any) err } rows := &sql.Rows{} query, args := selector.Query() - if err := ms.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/meter_update.go b/openmeter/ent/db/meter_update.go index 0634d2740d..68a49da86f 100644 --- a/openmeter/ent/db/meter_update.go +++ b/openmeter/ent/db/meter_update.go @@ -203,10 +203,14 @@ func (_u *MeterUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(dbmeter.FieldMetadata, field.TypeJSON, value) + vv, err := dbmeter.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(dbmeter.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(dbmeter.FieldMetadata, field.TypeJSON) + _spec.ClearField(dbmeter.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(dbmeter.FieldUpdatedAt, field.TypeTime, value) @@ -469,10 +473,14 @@ func (_u *MeterUpdateOne) sqlSave(ctx context.Context) (_node *Meter, err error) } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(dbmeter.FieldMetadata, field.TypeJSON, value) + vv, err := dbmeter.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(dbmeter.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(dbmeter.FieldMetadata, field.TypeJSON) + _spec.ClearField(dbmeter.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(dbmeter.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/migrate/schema.go b/openmeter/ent/db/migrate/schema.go index 4a1e63a6aa..d5a4eb8c12 100644 --- a/openmeter/ent/db/migrate/schema.go +++ b/openmeter/ent/db/migrate/schema.go @@ -13,7 +13,7 @@ var ( AddonsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -77,7 +77,7 @@ var ( AddonRateCardsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -156,7 +156,7 @@ var ( AppsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -241,7 +241,7 @@ var ( {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "app_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "customer_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, } @@ -552,7 +552,7 @@ var ( BillingInvoicesColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -717,9 +717,9 @@ var ( // BillingInvoiceLinesColumns holds the columns for the "billing_invoice_lines" table. BillingInvoiceLinesColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, - {Name: "annotations", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "annotations", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -977,7 +977,7 @@ var ( BillingInvoiceSplitLineGroupsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -1125,7 +1125,7 @@ var ( BillingProfilesColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -1273,7 +1273,7 @@ var ( CustomersColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -1286,7 +1286,7 @@ var ( {Name: "billing_address_line1", Type: field.TypeString, Nullable: true}, {Name: "billing_address_line2", Type: field.TypeString, Nullable: true}, {Name: "billing_address_phone_number", Type: field.TypeString, Nullable: true}, - {Name: "annotations", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "annotations", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "key", Type: field.TypeString, Nullable: true}, {Name: "primary_email", Type: field.TypeString, Nullable: true}, {Name: "currency", Type: field.TypeString, Nullable: true, Size: 3}, @@ -1394,7 +1394,7 @@ var ( EntitlementsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -1483,7 +1483,7 @@ var ( {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "namespace", Type: field.TypeString}, {Name: "name", Type: field.TypeString}, {Name: "key", Type: field.TypeString}, @@ -1521,7 +1521,7 @@ var ( GrantsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -1577,7 +1577,7 @@ var ( MetersColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -1810,7 +1810,7 @@ var ( PlansColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -1864,8 +1864,8 @@ var ( PlanAddonsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, - {Name: "annotations", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "annotations", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -1928,7 +1928,7 @@ var ( PlanPhasesColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -2000,7 +2000,7 @@ var ( PlanRateCardsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -2121,7 +2121,7 @@ var ( {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "active_from", Type: field.TypeTime}, {Name: "active_to", Type: field.TypeTime, Nullable: true}, {Name: "name", Type: field.TypeString, Default: "Subscription"}, @@ -2179,7 +2179,7 @@ var ( SubscriptionAddonsColumns = []*schema.Column{ {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, {Name: "namespace", Type: field.TypeString}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -2267,7 +2267,7 @@ var ( {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "annotations", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "active_from", Type: field.TypeTime}, {Name: "active_to", Type: field.TypeTime, Nullable: true}, @@ -2335,7 +2335,7 @@ var ( {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, - {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, + {Name: "metadata", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "key", Type: field.TypeString}, {Name: "name", Type: field.TypeString}, {Name: "description", Type: field.TypeString, Nullable: true}, diff --git a/openmeter/ent/db/notificationchannel_query.go b/openmeter/ent/db/notificationchannel_query.go index 72cf3a885f..1b6a9caf64 100644 --- a/openmeter/ent/db/notificationchannel_query.go +++ b/openmeter/ent/db/notificationchannel_query.go @@ -590,41 +590,41 @@ type NotificationChannelGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (ncgb *NotificationChannelGroupBy) Aggregate(fns ...AggregateFunc) *NotificationChannelGroupBy { - ncgb.fns = append(ncgb.fns, fns...) - return ncgb +func (_g *NotificationChannelGroupBy) Aggregate(fns ...AggregateFunc) *NotificationChannelGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (ncgb *NotificationChannelGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ncgb.build.ctx, ent.OpQueryGroupBy) - if err := ncgb.build.prepareQuery(ctx); err != nil { +func (_g *NotificationChannelGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*NotificationChannelQuery, *NotificationChannelGroupBy](ctx, ncgb.build, ncgb, ncgb.build.inters, v) + return scanWithInterceptors[*NotificationChannelQuery, *NotificationChannelGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (ncgb *NotificationChannelGroupBy) sqlScan(ctx context.Context, root *NotificationChannelQuery, v any) error { +func (_g *NotificationChannelGroupBy) sqlScan(ctx context.Context, root *NotificationChannelQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(ncgb.fns)) - for _, fn := range ncgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*ncgb.flds)+len(ncgb.fns)) - for _, f := range *ncgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*ncgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := ncgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -638,27 +638,27 @@ type NotificationChannelSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (ncs *NotificationChannelSelect) Aggregate(fns ...AggregateFunc) *NotificationChannelSelect { - ncs.fns = append(ncs.fns, fns...) - return ncs +func (_s *NotificationChannelSelect) Aggregate(fns ...AggregateFunc) *NotificationChannelSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (ncs *NotificationChannelSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ncs.ctx, ent.OpQuerySelect) - if err := ncs.prepareQuery(ctx); err != nil { +func (_s *NotificationChannelSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*NotificationChannelQuery, *NotificationChannelSelect](ctx, ncs.NotificationChannelQuery, ncs, ncs.inters, v) + return scanWithInterceptors[*NotificationChannelQuery, *NotificationChannelSelect](ctx, _s.NotificationChannelQuery, _s, _s.inters, v) } -func (ncs *NotificationChannelSelect) sqlScan(ctx context.Context, root *NotificationChannelQuery, v any) error { +func (_s *NotificationChannelSelect) sqlScan(ctx context.Context, root *NotificationChannelQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(ncs.fns)) - for _, fn := range ncs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*ncs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -666,7 +666,7 @@ func (ncs *NotificationChannelSelect) sqlScan(ctx context.Context, root *Notific } rows := &sql.Rows{} query, args := selector.Query() - if err := ncs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/notificationevent_query.go b/openmeter/ent/db/notificationevent_query.go index e19c1a877c..4e547c0bcb 100644 --- a/openmeter/ent/db/notificationevent_query.go +++ b/openmeter/ent/db/notificationevent_query.go @@ -667,41 +667,41 @@ type NotificationEventGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (negb *NotificationEventGroupBy) Aggregate(fns ...AggregateFunc) *NotificationEventGroupBy { - negb.fns = append(negb.fns, fns...) - return negb +func (_g *NotificationEventGroupBy) Aggregate(fns ...AggregateFunc) *NotificationEventGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (negb *NotificationEventGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, negb.build.ctx, ent.OpQueryGroupBy) - if err := negb.build.prepareQuery(ctx); err != nil { +func (_g *NotificationEventGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*NotificationEventQuery, *NotificationEventGroupBy](ctx, negb.build, negb, negb.build.inters, v) + return scanWithInterceptors[*NotificationEventQuery, *NotificationEventGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (negb *NotificationEventGroupBy) sqlScan(ctx context.Context, root *NotificationEventQuery, v any) error { +func (_g *NotificationEventGroupBy) sqlScan(ctx context.Context, root *NotificationEventQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(negb.fns)) - for _, fn := range negb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*negb.flds)+len(negb.fns)) - for _, f := range *negb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*negb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := negb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -715,27 +715,27 @@ type NotificationEventSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (nes *NotificationEventSelect) Aggregate(fns ...AggregateFunc) *NotificationEventSelect { - nes.fns = append(nes.fns, fns...) - return nes +func (_s *NotificationEventSelect) Aggregate(fns ...AggregateFunc) *NotificationEventSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (nes *NotificationEventSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, nes.ctx, ent.OpQuerySelect) - if err := nes.prepareQuery(ctx); err != nil { +func (_s *NotificationEventSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*NotificationEventQuery, *NotificationEventSelect](ctx, nes.NotificationEventQuery, nes, nes.inters, v) + return scanWithInterceptors[*NotificationEventQuery, *NotificationEventSelect](ctx, _s.NotificationEventQuery, _s, _s.inters, v) } -func (nes *NotificationEventSelect) sqlScan(ctx context.Context, root *NotificationEventQuery, v any) error { +func (_s *NotificationEventSelect) sqlScan(ctx context.Context, root *NotificationEventQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(nes.fns)) - for _, fn := range nes.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*nes.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -743,7 +743,7 @@ func (nes *NotificationEventSelect) sqlScan(ctx context.Context, root *Notificat } rows := &sql.Rows{} query, args := selector.Query() - if err := nes.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/notificationeventdeliverystatus_query.go b/openmeter/ent/db/notificationeventdeliverystatus_query.go index 73db74e5ee..710831f07f 100644 --- a/openmeter/ent/db/notificationeventdeliverystatus_query.go +++ b/openmeter/ent/db/notificationeventdeliverystatus_query.go @@ -592,41 +592,41 @@ type NotificationEventDeliveryStatusGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (nedsgb *NotificationEventDeliveryStatusGroupBy) Aggregate(fns ...AggregateFunc) *NotificationEventDeliveryStatusGroupBy { - nedsgb.fns = append(nedsgb.fns, fns...) - return nedsgb +func (_g *NotificationEventDeliveryStatusGroupBy) Aggregate(fns ...AggregateFunc) *NotificationEventDeliveryStatusGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (nedsgb *NotificationEventDeliveryStatusGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, nedsgb.build.ctx, ent.OpQueryGroupBy) - if err := nedsgb.build.prepareQuery(ctx); err != nil { +func (_g *NotificationEventDeliveryStatusGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*NotificationEventDeliveryStatusQuery, *NotificationEventDeliveryStatusGroupBy](ctx, nedsgb.build, nedsgb, nedsgb.build.inters, v) + return scanWithInterceptors[*NotificationEventDeliveryStatusQuery, *NotificationEventDeliveryStatusGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (nedsgb *NotificationEventDeliveryStatusGroupBy) sqlScan(ctx context.Context, root *NotificationEventDeliveryStatusQuery, v any) error { +func (_g *NotificationEventDeliveryStatusGroupBy) sqlScan(ctx context.Context, root *NotificationEventDeliveryStatusQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(nedsgb.fns)) - for _, fn := range nedsgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*nedsgb.flds)+len(nedsgb.fns)) - for _, f := range *nedsgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*nedsgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := nedsgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -640,27 +640,27 @@ type NotificationEventDeliveryStatusSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (nedss *NotificationEventDeliveryStatusSelect) Aggregate(fns ...AggregateFunc) *NotificationEventDeliveryStatusSelect { - nedss.fns = append(nedss.fns, fns...) - return nedss +func (_s *NotificationEventDeliveryStatusSelect) Aggregate(fns ...AggregateFunc) *NotificationEventDeliveryStatusSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (nedss *NotificationEventDeliveryStatusSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, nedss.ctx, ent.OpQuerySelect) - if err := nedss.prepareQuery(ctx); err != nil { +func (_s *NotificationEventDeliveryStatusSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*NotificationEventDeliveryStatusQuery, *NotificationEventDeliveryStatusSelect](ctx, nedss.NotificationEventDeliveryStatusQuery, nedss, nedss.inters, v) + return scanWithInterceptors[*NotificationEventDeliveryStatusQuery, *NotificationEventDeliveryStatusSelect](ctx, _s.NotificationEventDeliveryStatusQuery, _s, _s.inters, v) } -func (nedss *NotificationEventDeliveryStatusSelect) sqlScan(ctx context.Context, root *NotificationEventDeliveryStatusQuery, v any) error { +func (_s *NotificationEventDeliveryStatusSelect) sqlScan(ctx context.Context, root *NotificationEventDeliveryStatusQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(nedss.fns)) - for _, fn := range nedss.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*nedss.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -668,7 +668,7 @@ func (nedss *NotificationEventDeliveryStatusSelect) sqlScan(ctx context.Context, } rows := &sql.Rows{} query, args := selector.Query() - if err := nedss.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/notificationrule_query.go b/openmeter/ent/db/notificationrule_query.go index 6294272811..d87f0609ef 100644 --- a/openmeter/ent/db/notificationrule_query.go +++ b/openmeter/ent/db/notificationrule_query.go @@ -664,41 +664,41 @@ type NotificationRuleGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (nrgb *NotificationRuleGroupBy) Aggregate(fns ...AggregateFunc) *NotificationRuleGroupBy { - nrgb.fns = append(nrgb.fns, fns...) - return nrgb +func (_g *NotificationRuleGroupBy) Aggregate(fns ...AggregateFunc) *NotificationRuleGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (nrgb *NotificationRuleGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, nrgb.build.ctx, ent.OpQueryGroupBy) - if err := nrgb.build.prepareQuery(ctx); err != nil { +func (_g *NotificationRuleGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*NotificationRuleQuery, *NotificationRuleGroupBy](ctx, nrgb.build, nrgb, nrgb.build.inters, v) + return scanWithInterceptors[*NotificationRuleQuery, *NotificationRuleGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (nrgb *NotificationRuleGroupBy) sqlScan(ctx context.Context, root *NotificationRuleQuery, v any) error { +func (_g *NotificationRuleGroupBy) sqlScan(ctx context.Context, root *NotificationRuleQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(nrgb.fns)) - for _, fn := range nrgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*nrgb.flds)+len(nrgb.fns)) - for _, f := range *nrgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*nrgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := nrgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -712,27 +712,27 @@ type NotificationRuleSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (nrs *NotificationRuleSelect) Aggregate(fns ...AggregateFunc) *NotificationRuleSelect { - nrs.fns = append(nrs.fns, fns...) - return nrs +func (_s *NotificationRuleSelect) Aggregate(fns ...AggregateFunc) *NotificationRuleSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (nrs *NotificationRuleSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, nrs.ctx, ent.OpQuerySelect) - if err := nrs.prepareQuery(ctx); err != nil { +func (_s *NotificationRuleSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*NotificationRuleQuery, *NotificationRuleSelect](ctx, nrs.NotificationRuleQuery, nrs, nrs.inters, v) + return scanWithInterceptors[*NotificationRuleQuery, *NotificationRuleSelect](ctx, _s.NotificationRuleQuery, _s, _s.inters, v) } -func (nrs *NotificationRuleSelect) sqlScan(ctx context.Context, root *NotificationRuleQuery, v any) error { +func (_s *NotificationRuleSelect) sqlScan(ctx context.Context, root *NotificationRuleQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(nrs.fns)) - for _, fn := range nrs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*nrs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -740,7 +740,7 @@ func (nrs *NotificationRuleSelect) sqlScan(ctx context.Context, root *Notificati } rows := &sql.Rows{} query, args := selector.Query() - if err := nrs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/plan.go b/openmeter/ent/db/plan.go index 1bb762a7fb..6f8c637fca 100644 --- a/openmeter/ent/db/plan.go +++ b/openmeter/ent/db/plan.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -99,14 +98,14 @@ func (*Plan) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case plan.FieldMetadata: - values[i] = new([]byte) case plan.FieldVersion: values[i] = new(sql.NullInt64) case plan.FieldID, plan.FieldNamespace, plan.FieldName, plan.FieldDescription, plan.FieldKey, plan.FieldCurrency, plan.FieldBillingCadence: values[i] = new(sql.NullString) case plan.FieldCreatedAt, plan.FieldUpdatedAt, plan.FieldDeletedAt, plan.FieldEffectiveFrom, plan.FieldEffectiveTo: values[i] = new(sql.NullTime) + case plan.FieldMetadata: + values[i] = plan.ValueScanner.Metadata.ScanValue() case plan.FieldProRatingConfig: values[i] = plan.ValueScanner.ProRatingConfig.ScanValue() default: @@ -137,12 +136,10 @@ func (_m *Plan) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case plan.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := plan.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case plan.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/plan/plan.go b/openmeter/ent/db/plan/plan.go index b2b2cc05fa..4d93009ac3 100644 --- a/openmeter/ent/db/plan/plan.go +++ b/openmeter/ent/db/plan/plan.go @@ -127,6 +127,7 @@ var ( DefaultID func() string // ValueScanner of all Plan fields. ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] ProRatingConfig field.TypeValueScanner[productcatalog.ProRatingConfig] } ) @@ -144,6 +145,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/plan_create.go b/openmeter/ent/db/plan_create.go index fd8623e7c5..f6170dda15 100644 --- a/openmeter/ent/db/plan_create.go +++ b/openmeter/ent/db/plan_create.go @@ -390,7 +390,11 @@ func (_c *PlanCreate) createSpec() (*Plan, *sqlgraph.CreateSpec, error) { _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(plan.FieldMetadata, field.TypeJSON, value) + vv, err := plan.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(plan.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { diff --git a/openmeter/ent/db/plan_query.go b/openmeter/ent/db/plan_query.go index b20704ed96..dbe0a1393c 100644 --- a/openmeter/ent/db/plan_query.go +++ b/openmeter/ent/db/plan_query.go @@ -710,41 +710,41 @@ type PlanGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (pgb *PlanGroupBy) Aggregate(fns ...AggregateFunc) *PlanGroupBy { - pgb.fns = append(pgb.fns, fns...) - return pgb +func (_g *PlanGroupBy) Aggregate(fns ...AggregateFunc) *PlanGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (pgb *PlanGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, pgb.build.ctx, ent.OpQueryGroupBy) - if err := pgb.build.prepareQuery(ctx); err != nil { +func (_g *PlanGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*PlanQuery, *PlanGroupBy](ctx, pgb.build, pgb, pgb.build.inters, v) + return scanWithInterceptors[*PlanQuery, *PlanGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (pgb *PlanGroupBy) sqlScan(ctx context.Context, root *PlanQuery, v any) error { +func (_g *PlanGroupBy) sqlScan(ctx context.Context, root *PlanQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(pgb.fns)) - for _, fn := range pgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*pgb.flds)+len(pgb.fns)) - for _, f := range *pgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*pgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := pgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -758,27 +758,27 @@ type PlanSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (ps *PlanSelect) Aggregate(fns ...AggregateFunc) *PlanSelect { - ps.fns = append(ps.fns, fns...) - return ps +func (_s *PlanSelect) Aggregate(fns ...AggregateFunc) *PlanSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (ps *PlanSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ps.ctx, ent.OpQuerySelect) - if err := ps.prepareQuery(ctx); err != nil { +func (_s *PlanSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*PlanQuery, *PlanSelect](ctx, ps.PlanQuery, ps, ps.inters, v) + return scanWithInterceptors[*PlanQuery, *PlanSelect](ctx, _s.PlanQuery, _s, _s.inters, v) } -func (ps *PlanSelect) sqlScan(ctx context.Context, root *PlanQuery, v any) error { +func (_s *PlanSelect) sqlScan(ctx context.Context, root *PlanQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(ps.fns)) - for _, fn := range ps.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*ps.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -786,7 +786,7 @@ func (ps *PlanSelect) sqlScan(ctx context.Context, root *PlanQuery, v any) error } rows := &sql.Rows{} query, args := selector.Query() - if err := ps.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/plan_update.go b/openmeter/ent/db/plan_update.go index 206329b3b3..4c8fd5107d 100644 --- a/openmeter/ent/db/plan_update.go +++ b/openmeter/ent/db/plan_update.go @@ -371,10 +371,14 @@ func (_u *PlanUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(plan.FieldMetadata, field.TypeJSON, value) + vv, err := plan.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(plan.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(plan.FieldMetadata, field.TypeJSON) + _spec.ClearField(plan.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(plan.FieldUpdatedAt, field.TypeTime, value) @@ -945,10 +949,14 @@ func (_u *PlanUpdateOne) sqlSave(ctx context.Context) (_node *Plan, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(plan.FieldMetadata, field.TypeJSON, value) + vv, err := plan.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(plan.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(plan.FieldMetadata, field.TypeJSON) + _spec.ClearField(plan.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(plan.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/planaddon.go b/openmeter/ent/db/planaddon.go index 26d8da56d3..473612ffc6 100644 --- a/openmeter/ent/db/planaddon.go +++ b/openmeter/ent/db/planaddon.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -84,14 +83,16 @@ func (*PlanAddon) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case planaddon.FieldMetadata, planaddon.FieldAnnotations: - values[i] = new([]byte) case planaddon.FieldMaxQuantity: values[i] = new(sql.NullInt64) case planaddon.FieldID, planaddon.FieldNamespace, planaddon.FieldPlanID, planaddon.FieldAddonID, planaddon.FieldFromPlanPhase: values[i] = new(sql.NullString) case planaddon.FieldCreatedAt, planaddon.FieldUpdatedAt, planaddon.FieldDeletedAt: values[i] = new(sql.NullTime) + case planaddon.FieldMetadata: + values[i] = planaddon.ValueScanner.Metadata.ScanValue() + case planaddon.FieldAnnotations: + values[i] = planaddon.ValueScanner.Annotations.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -120,20 +121,16 @@ func (_m *PlanAddon) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case planaddon.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := planaddon.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case planaddon.FieldAnnotations: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field annotations", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Annotations); err != nil { - return fmt.Errorf("unmarshal field annotations: %w", err) - } + if value, err := planaddon.ValueScanner.Annotations.FromValue(values[i]); err != nil { + return err + } else { + _m.Annotations = value } case planaddon.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/planaddon/planaddon.go b/openmeter/ent/db/planaddon/planaddon.go index 8c77569fe6..80efc6563a 100644 --- a/openmeter/ent/db/planaddon/planaddon.go +++ b/openmeter/ent/db/planaddon/planaddon.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -96,6 +97,11 @@ var ( AddonIDValidator func(string) error // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all PlanAddon fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + Annotations field.TypeValueScanner[map[string]interface{}] + } ) // OrderOption defines the ordering options for the PlanAddon queries. @@ -111,6 +117,16 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + +// ByAnnotations orders the results by the annotations field. +func ByAnnotations(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldAnnotations, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/planaddon_create.go b/openmeter/ent/db/planaddon_create.go index ea347dea82..165116bab5 100644 --- a/openmeter/ent/db/planaddon_create.go +++ b/openmeter/ent/db/planaddon_create.go @@ -238,7 +238,10 @@ func (_c *PlanAddonCreate) sqlSave(ctx context.Context) (*PlanAddon, error) { if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -257,7 +260,7 @@ func (_c *PlanAddonCreate) sqlSave(ctx context.Context) (*PlanAddon, error) { return _node, nil } -func (_c *PlanAddonCreate) createSpec() (*PlanAddon, *sqlgraph.CreateSpec) { +func (_c *PlanAddonCreate) createSpec() (*PlanAddon, *sqlgraph.CreateSpec, error) { var ( _node = &PlanAddon{config: _c.config} _spec = sqlgraph.NewCreateSpec(planaddon.Table, sqlgraph.NewFieldSpec(planaddon.FieldID, field.TypeString)) @@ -272,11 +275,19 @@ func (_c *PlanAddonCreate) createSpec() (*PlanAddon, *sqlgraph.CreateSpec) { _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(planaddon.FieldMetadata, field.TypeJSON, value) + vv, err := planaddon.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(planaddon.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.Annotations(); ok { - _spec.SetField(planaddon.FieldAnnotations, field.TypeJSON, value) + vv, err := planaddon.ValueScanner.Annotations.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(planaddon.FieldAnnotations, field.TypeString, vv) _node.Annotations = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -333,7 +344,7 @@ func (_c *PlanAddonCreate) createSpec() (*PlanAddon, *sqlgraph.CreateSpec) { _node.AddonID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -734,7 +745,10 @@ func (_c *PlanAddonCreateBulk) Save(ctx context.Context) ([]*PlanAddon, error) { } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/planaddon_query.go b/openmeter/ent/db/planaddon_query.go index 31a2982900..fedab2e685 100644 --- a/openmeter/ent/db/planaddon_query.go +++ b/openmeter/ent/db/planaddon_query.go @@ -634,41 +634,41 @@ type PlanAddonGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (pagb *PlanAddonGroupBy) Aggregate(fns ...AggregateFunc) *PlanAddonGroupBy { - pagb.fns = append(pagb.fns, fns...) - return pagb +func (_g *PlanAddonGroupBy) Aggregate(fns ...AggregateFunc) *PlanAddonGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (pagb *PlanAddonGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, pagb.build.ctx, ent.OpQueryGroupBy) - if err := pagb.build.prepareQuery(ctx); err != nil { +func (_g *PlanAddonGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*PlanAddonQuery, *PlanAddonGroupBy](ctx, pagb.build, pagb, pagb.build.inters, v) + return scanWithInterceptors[*PlanAddonQuery, *PlanAddonGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (pagb *PlanAddonGroupBy) sqlScan(ctx context.Context, root *PlanAddonQuery, v any) error { +func (_g *PlanAddonGroupBy) sqlScan(ctx context.Context, root *PlanAddonQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(pagb.fns)) - for _, fn := range pagb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*pagb.flds)+len(pagb.fns)) - for _, f := range *pagb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*pagb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := pagb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -682,27 +682,27 @@ type PlanAddonSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (pas *PlanAddonSelect) Aggregate(fns ...AggregateFunc) *PlanAddonSelect { - pas.fns = append(pas.fns, fns...) - return pas +func (_s *PlanAddonSelect) Aggregate(fns ...AggregateFunc) *PlanAddonSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (pas *PlanAddonSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, pas.ctx, ent.OpQuerySelect) - if err := pas.prepareQuery(ctx); err != nil { +func (_s *PlanAddonSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*PlanAddonQuery, *PlanAddonSelect](ctx, pas.PlanAddonQuery, pas, pas.inters, v) + return scanWithInterceptors[*PlanAddonQuery, *PlanAddonSelect](ctx, _s.PlanAddonQuery, _s, _s.inters, v) } -func (pas *PlanAddonSelect) sqlScan(ctx context.Context, root *PlanAddonQuery, v any) error { +func (_s *PlanAddonSelect) sqlScan(ctx context.Context, root *PlanAddonQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(pas.fns)) - for _, fn := range pas.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*pas.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -710,7 +710,7 @@ func (pas *PlanAddonSelect) sqlScan(ctx context.Context, root *PlanAddonQuery, v } rows := &sql.Rows{} query, args := selector.Query() - if err := pas.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/planaddon_update.go b/openmeter/ent/db/planaddon_update.go index a731a189f4..440187af72 100644 --- a/openmeter/ent/db/planaddon_update.go +++ b/openmeter/ent/db/planaddon_update.go @@ -184,16 +184,24 @@ func (_u *PlanAddonUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(planaddon.FieldMetadata, field.TypeJSON, value) + vv, err := planaddon.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(planaddon.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(planaddon.FieldMetadata, field.TypeJSON) + _spec.ClearField(planaddon.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.Annotations(); ok { - _spec.SetField(planaddon.FieldAnnotations, field.TypeJSON, value) + vv, err := planaddon.ValueScanner.Annotations.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(planaddon.FieldAnnotations, field.TypeString, vv) } if _u.mutation.AnnotationsCleared() { - _spec.ClearField(planaddon.FieldAnnotations, field.TypeJSON) + _spec.ClearField(planaddon.FieldAnnotations, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(planaddon.FieldUpdatedAt, field.TypeTime, value) @@ -422,16 +430,24 @@ func (_u *PlanAddonUpdateOne) sqlSave(ctx context.Context) (_node *PlanAddon, er } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(planaddon.FieldMetadata, field.TypeJSON, value) + vv, err := planaddon.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(planaddon.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(planaddon.FieldMetadata, field.TypeJSON) + _spec.ClearField(planaddon.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.Annotations(); ok { - _spec.SetField(planaddon.FieldAnnotations, field.TypeJSON, value) + vv, err := planaddon.ValueScanner.Annotations.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(planaddon.FieldAnnotations, field.TypeString, vv) } if _u.mutation.AnnotationsCleared() { - _spec.ClearField(planaddon.FieldAnnotations, field.TypeJSON) + _spec.ClearField(planaddon.FieldAnnotations, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(planaddon.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/planphase.go b/openmeter/ent/db/planphase.go index e156706431..6914a04955 100644 --- a/openmeter/ent/db/planphase.go +++ b/openmeter/ent/db/planphase.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -84,14 +83,14 @@ func (*PlanPhase) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case planphase.FieldMetadata: - values[i] = new([]byte) case planphase.FieldIndex: values[i] = new(sql.NullInt64) case planphase.FieldID, planphase.FieldNamespace, planphase.FieldName, planphase.FieldDescription, planphase.FieldKey, planphase.FieldPlanID, planphase.FieldDuration: values[i] = new(sql.NullString) case planphase.FieldCreatedAt, planphase.FieldUpdatedAt, planphase.FieldDeletedAt: values[i] = new(sql.NullTime) + case planphase.FieldMetadata: + values[i] = planphase.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -120,12 +119,10 @@ func (_m *PlanPhase) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case planphase.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := planphase.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case planphase.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/planphase/planphase.go b/openmeter/ent/db/planphase/planphase.go index c136273520..d84727c5ef 100644 --- a/openmeter/ent/db/planphase/planphase.go +++ b/openmeter/ent/db/planphase/planphase.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -99,6 +100,10 @@ var ( PlanIDValidator func(string) error // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all PlanPhase fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // OrderOption defines the ordering options for the PlanPhase queries. @@ -114,6 +119,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/planphase_create.go b/openmeter/ent/db/planphase_create.go index 1d3a7bdf1b..8a86457856 100644 --- a/openmeter/ent/db/planphase_create.go +++ b/openmeter/ent/db/planphase_create.go @@ -263,7 +263,10 @@ func (_c *PlanPhaseCreate) sqlSave(ctx context.Context) (*PlanPhase, error) { if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -282,7 +285,7 @@ func (_c *PlanPhaseCreate) sqlSave(ctx context.Context) (*PlanPhase, error) { return _node, nil } -func (_c *PlanPhaseCreate) createSpec() (*PlanPhase, *sqlgraph.CreateSpec) { +func (_c *PlanPhaseCreate) createSpec() (*PlanPhase, *sqlgraph.CreateSpec, error) { var ( _node = &PlanPhase{config: _c.config} _spec = sqlgraph.NewCreateSpec(planphase.Table, sqlgraph.NewFieldSpec(planphase.FieldID, field.TypeString)) @@ -297,7 +300,11 @@ func (_c *PlanPhaseCreate) createSpec() (*PlanPhase, *sqlgraph.CreateSpec) { _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(planphase.FieldMetadata, field.TypeJSON, value) + vv, err := planphase.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(planphase.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -365,7 +372,7 @@ func (_c *PlanPhaseCreate) createSpec() (*PlanPhase, *sqlgraph.CreateSpec) { } _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -815,7 +822,10 @@ func (_c *PlanPhaseCreateBulk) Save(ctx context.Context) ([]*PlanPhase, error) { } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/planphase_query.go b/openmeter/ent/db/planphase_query.go index d2c7d4976b..98f3beb917 100644 --- a/openmeter/ent/db/planphase_query.go +++ b/openmeter/ent/db/planphase_query.go @@ -634,41 +634,41 @@ type PlanPhaseGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (ppgb *PlanPhaseGroupBy) Aggregate(fns ...AggregateFunc) *PlanPhaseGroupBy { - ppgb.fns = append(ppgb.fns, fns...) - return ppgb +func (_g *PlanPhaseGroupBy) Aggregate(fns ...AggregateFunc) *PlanPhaseGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (ppgb *PlanPhaseGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ppgb.build.ctx, ent.OpQueryGroupBy) - if err := ppgb.build.prepareQuery(ctx); err != nil { +func (_g *PlanPhaseGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*PlanPhaseQuery, *PlanPhaseGroupBy](ctx, ppgb.build, ppgb, ppgb.build.inters, v) + return scanWithInterceptors[*PlanPhaseQuery, *PlanPhaseGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (ppgb *PlanPhaseGroupBy) sqlScan(ctx context.Context, root *PlanPhaseQuery, v any) error { +func (_g *PlanPhaseGroupBy) sqlScan(ctx context.Context, root *PlanPhaseQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(ppgb.fns)) - for _, fn := range ppgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*ppgb.flds)+len(ppgb.fns)) - for _, f := range *ppgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*ppgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := ppgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -682,27 +682,27 @@ type PlanPhaseSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (pps *PlanPhaseSelect) Aggregate(fns ...AggregateFunc) *PlanPhaseSelect { - pps.fns = append(pps.fns, fns...) - return pps +func (_s *PlanPhaseSelect) Aggregate(fns ...AggregateFunc) *PlanPhaseSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (pps *PlanPhaseSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, pps.ctx, ent.OpQuerySelect) - if err := pps.prepareQuery(ctx); err != nil { +func (_s *PlanPhaseSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*PlanPhaseQuery, *PlanPhaseSelect](ctx, pps.PlanPhaseQuery, pps, pps.inters, v) + return scanWithInterceptors[*PlanPhaseQuery, *PlanPhaseSelect](ctx, _s.PlanPhaseQuery, _s, _s.inters, v) } -func (pps *PlanPhaseSelect) sqlScan(ctx context.Context, root *PlanPhaseQuery, v any) error { +func (_s *PlanPhaseSelect) sqlScan(ctx context.Context, root *PlanPhaseQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(pps.fns)) - for _, fn := range pps.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*pps.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -710,7 +710,7 @@ func (pps *PlanPhaseSelect) sqlScan(ctx context.Context, root *PlanPhaseQuery, v } rows := &sql.Rows{} query, args := selector.Query() - if err := pps.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/planphase_update.go b/openmeter/ent/db/planphase_update.go index 8dfc06ec06..51708bbfd5 100644 --- a/openmeter/ent/db/planphase_update.go +++ b/openmeter/ent/db/planphase_update.go @@ -272,10 +272,14 @@ func (_u *PlanPhaseUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(planphase.FieldMetadata, field.TypeJSON, value) + vv, err := planphase.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(planphase.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(planphase.FieldMetadata, field.TypeJSON) + _spec.ClearField(planphase.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(planphase.FieldUpdatedAt, field.TypeTime, value) @@ -672,10 +676,14 @@ func (_u *PlanPhaseUpdateOne) sqlSave(ctx context.Context) (_node *PlanPhase, er } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(planphase.FieldMetadata, field.TypeJSON, value) + vv, err := planphase.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(planphase.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(planphase.FieldMetadata, field.TypeJSON) + _spec.ClearField(planphase.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(planphase.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/planratecard.go b/openmeter/ent/db/planratecard.go index 8f4ca9dc5f..9820dfdb89 100644 --- a/openmeter/ent/db/planratecard.go +++ b/openmeter/ent/db/planratecard.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -100,12 +99,12 @@ func (*PlanRateCard) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case planratecard.FieldMetadata: - values[i] = new([]byte) case planratecard.FieldID, planratecard.FieldNamespace, planratecard.FieldName, planratecard.FieldDescription, planratecard.FieldKey, planratecard.FieldType, planratecard.FieldFeatureKey, planratecard.FieldBillingCadence, planratecard.FieldPhaseID, planratecard.FieldFeatureID: values[i] = new(sql.NullString) case planratecard.FieldCreatedAt, planratecard.FieldUpdatedAt, planratecard.FieldDeletedAt: values[i] = new(sql.NullTime) + case planratecard.FieldMetadata: + values[i] = planratecard.ValueScanner.Metadata.ScanValue() case planratecard.FieldEntitlementTemplate: values[i] = planratecard.ValueScanner.EntitlementTemplate.ScanValue() case planratecard.FieldTaxConfig: @@ -142,12 +141,10 @@ func (_m *PlanRateCard) assignValues(columns []string, values []any) error { _m.Namespace = value.String } case planratecard.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := planratecard.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case planratecard.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/planratecard/planratecard.go b/openmeter/ent/db/planratecard/planratecard.go index d093b5cfb2..bb30a53e0d 100644 --- a/openmeter/ent/db/planratecard/planratecard.go +++ b/openmeter/ent/db/planratecard/planratecard.go @@ -122,6 +122,7 @@ var ( DefaultID func() string // ValueScanner of all PlanRateCard fields. ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] EntitlementTemplate field.TypeValueScanner[*productcatalog.EntitlementTemplate] TaxConfig field.TypeValueScanner[*productcatalog.TaxConfig] Price field.TypeValueScanner[*productcatalog.Price] @@ -152,6 +153,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/planratecard_create.go b/openmeter/ent/db/planratecard_create.go index d52d59e89a..ec2dadc571 100644 --- a/openmeter/ent/db/planratecard_create.go +++ b/openmeter/ent/db/planratecard_create.go @@ -382,7 +382,11 @@ func (_c *PlanRateCardCreate) createSpec() (*PlanRateCard, *sqlgraph.CreateSpec, _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(planratecard.FieldMetadata, field.TypeJSON, value) + vv, err := planratecard.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(planratecard.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { diff --git a/openmeter/ent/db/planratecard_query.go b/openmeter/ent/db/planratecard_query.go index a79fe69597..7f78588785 100644 --- a/openmeter/ent/db/planratecard_query.go +++ b/openmeter/ent/db/planratecard_query.go @@ -637,41 +637,41 @@ type PlanRateCardGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (prcgb *PlanRateCardGroupBy) Aggregate(fns ...AggregateFunc) *PlanRateCardGroupBy { - prcgb.fns = append(prcgb.fns, fns...) - return prcgb +func (_g *PlanRateCardGroupBy) Aggregate(fns ...AggregateFunc) *PlanRateCardGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (prcgb *PlanRateCardGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, prcgb.build.ctx, ent.OpQueryGroupBy) - if err := prcgb.build.prepareQuery(ctx); err != nil { +func (_g *PlanRateCardGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*PlanRateCardQuery, *PlanRateCardGroupBy](ctx, prcgb.build, prcgb, prcgb.build.inters, v) + return scanWithInterceptors[*PlanRateCardQuery, *PlanRateCardGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (prcgb *PlanRateCardGroupBy) sqlScan(ctx context.Context, root *PlanRateCardQuery, v any) error { +func (_g *PlanRateCardGroupBy) sqlScan(ctx context.Context, root *PlanRateCardQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(prcgb.fns)) - for _, fn := range prcgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*prcgb.flds)+len(prcgb.fns)) - for _, f := range *prcgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*prcgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := prcgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -685,27 +685,27 @@ type PlanRateCardSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (prcs *PlanRateCardSelect) Aggregate(fns ...AggregateFunc) *PlanRateCardSelect { - prcs.fns = append(prcs.fns, fns...) - return prcs +func (_s *PlanRateCardSelect) Aggregate(fns ...AggregateFunc) *PlanRateCardSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (prcs *PlanRateCardSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, prcs.ctx, ent.OpQuerySelect) - if err := prcs.prepareQuery(ctx); err != nil { +func (_s *PlanRateCardSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*PlanRateCardQuery, *PlanRateCardSelect](ctx, prcs.PlanRateCardQuery, prcs, prcs.inters, v) + return scanWithInterceptors[*PlanRateCardQuery, *PlanRateCardSelect](ctx, _s.PlanRateCardQuery, _s, _s.inters, v) } -func (prcs *PlanRateCardSelect) sqlScan(ctx context.Context, root *PlanRateCardQuery, v any) error { +func (_s *PlanRateCardSelect) sqlScan(ctx context.Context, root *PlanRateCardQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(prcs.fns)) - for _, fn := range prcs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*prcs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -713,7 +713,7 @@ func (prcs *PlanRateCardSelect) sqlScan(ctx context.Context, root *PlanRateCardQ } rows := &sql.Rows{} query, args := selector.Query() - if err := prcs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/planratecard_update.go b/openmeter/ent/db/planratecard_update.go index fec14be836..6b9561be3b 100644 --- a/openmeter/ent/db/planratecard_update.go +++ b/openmeter/ent/db/planratecard_update.go @@ -349,10 +349,14 @@ func (_u *PlanRateCardUpdate) sqlSave(ctx context.Context) (_node int, err error } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(planratecard.FieldMetadata, field.TypeJSON, value) + vv, err := planratecard.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(planratecard.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(planratecard.FieldMetadata, field.TypeJSON) + _spec.ClearField(planratecard.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(planratecard.FieldUpdatedAt, field.TypeTime, value) @@ -849,10 +853,14 @@ func (_u *PlanRateCardUpdateOne) sqlSave(ctx context.Context) (_node *PlanRateCa } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(planratecard.FieldMetadata, field.TypeJSON, value) + vv, err := planratecard.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(planratecard.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(planratecard.FieldMetadata, field.TypeJSON) + _spec.ClearField(planratecard.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(planratecard.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/predicate/predicate.go b/openmeter/ent/db/predicate/predicate.go index dd8c5e99b1..6dfc6fd995 100644 --- a/openmeter/ent/db/predicate/predicate.go +++ b/openmeter/ent/db/predicate/predicate.go @@ -37,12 +37,34 @@ func AddonRateCardOrErr(p AddonRateCard, err error) AddonRateCard { // App is the predicate function for dbapp builders. type App func(*sql.Selector) +// AppOrErr calls the predicate only if the error is not nit. +func AppOrErr(p App, err error) App { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // AppCustomInvoicing is the predicate function for appcustominvoicing builders. type AppCustomInvoicing func(*sql.Selector) // AppCustomInvoicingCustomer is the predicate function for appcustominvoicingcustomer builders. type AppCustomInvoicingCustomer func(*sql.Selector) +// AppCustomInvoicingCustomerOrErr calls the predicate only if the error is not nit. +func AppCustomInvoicingCustomerOrErr(p AppCustomInvoicingCustomer, err error) AppCustomInvoicingCustomer { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // AppCustomer is the predicate function for appcustomer builders. type AppCustomer func(*sql.Selector) @@ -64,6 +86,17 @@ type BillingCustomerOverride func(*sql.Selector) // BillingInvoice is the predicate function for billinginvoice builders. type BillingInvoice func(*sql.Selector) +// BillingInvoiceOrErr calls the predicate only if the error is not nit. +func BillingInvoiceOrErr(p BillingInvoice, err error) BillingInvoice { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // BillingInvoiceFlatFeeLineConfig is the predicate function for billinginvoiceflatfeelineconfig builders. type BillingInvoiceFlatFeeLineConfig func(*sql.Selector) @@ -143,6 +176,17 @@ type BillingInvoiceValidationIssue func(*sql.Selector) // BillingProfile is the predicate function for billingprofile builders. type BillingProfile func(*sql.Selector) +// BillingProfileOrErr calls the predicate only if the error is not nit. +func BillingProfileOrErr(p BillingProfile, err error) BillingProfile { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // BillingSequenceNumbers is the predicate function for billingsequencenumbers builders. type BillingSequenceNumbers func(*sql.Selector) @@ -152,6 +196,17 @@ type BillingWorkflowConfig func(*sql.Selector) // Customer is the predicate function for customer builders. type Customer func(*sql.Selector) +// CustomerOrErr calls the predicate only if the error is not nit. +func CustomerOrErr(p Customer, err error) Customer { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // CustomerSubjects is the predicate function for customersubjects builders. type CustomerSubjects func(*sql.Selector) @@ -172,12 +227,45 @@ func EntitlementOrErr(p Entitlement, err error) Entitlement { // Feature is the predicate function for feature builders. type Feature func(*sql.Selector) +// FeatureOrErr calls the predicate only if the error is not nit. +func FeatureOrErr(p Feature, err error) Feature { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // Grant is the predicate function for dbgrant builders. type Grant func(*sql.Selector) +// GrantOrErr calls the predicate only if the error is not nit. +func GrantOrErr(p Grant, err error) Grant { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // Meter is the predicate function for dbmeter builders. type Meter func(*sql.Selector) +// MeterOrErr calls the predicate only if the error is not nit. +func MeterOrErr(p Meter, err error) Meter { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // NotificationChannel is the predicate function for notificationchannel builders. type NotificationChannel func(*sql.Selector) @@ -240,9 +328,31 @@ func PlanOrErr(p Plan, err error) Plan { // PlanAddon is the predicate function for planaddon builders. type PlanAddon func(*sql.Selector) +// PlanAddonOrErr calls the predicate only if the error is not nit. +func PlanAddonOrErr(p PlanAddon, err error) PlanAddon { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // PlanPhase is the predicate function for planphase builders. type PlanPhase func(*sql.Selector) +// PlanPhaseOrErr calls the predicate only if the error is not nit. +func PlanPhaseOrErr(p PlanPhase, err error) PlanPhase { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // PlanRateCard is the predicate function for planratecard builders. type PlanRateCard func(*sql.Selector) @@ -277,6 +387,17 @@ func SubscriptionOrErr(p Subscription, err error) Subscription { // SubscriptionAddon is the predicate function for subscriptionaddon builders. type SubscriptionAddon func(*sql.Selector) +// SubscriptionAddonOrErr calls the predicate only if the error is not nit. +func SubscriptionAddonOrErr(p SubscriptionAddon, err error) SubscriptionAddon { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // SubscriptionAddonQuantity is the predicate function for subscriptionaddonquantity builders. type SubscriptionAddonQuantity func(*sql.Selector) @@ -297,5 +418,16 @@ func SubscriptionItemOrErr(p SubscriptionItem, err error) SubscriptionItem { // SubscriptionPhase is the predicate function for subscriptionphase builders. type SubscriptionPhase func(*sql.Selector) +// SubscriptionPhaseOrErr calls the predicate only if the error is not nit. +func SubscriptionPhaseOrErr(p SubscriptionPhase, err error) SubscriptionPhase { + return func(s *sql.Selector) { + if err != nil { + s.AddError(err) + return + } + p(s) + } +} + // UsageReset is the predicate function for usagereset builders. type UsageReset func(*sql.Selector) diff --git a/openmeter/ent/db/runtime.go b/openmeter/ent/db/runtime.go index 80fa1dcf14..067324d12f 100644 --- a/openmeter/ent/db/runtime.go +++ b/openmeter/ent/db/runtime.go @@ -69,6 +69,9 @@ func init() { addonDescNamespace := addonMixinFields0[1].Descriptor() // addon.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. addon.NamespaceValidator = addonDescNamespace.Validators[0].(func(string) error) + // addonDescMetadata is the schema descriptor for metadata field. + addonDescMetadata := addonMixinFields0[2].Descriptor() + addon.ValueScanner.Metadata = addonDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // addonDescCreatedAt is the schema descriptor for created_at field. addonDescCreatedAt := addonMixinFields0[3].Descriptor() // addon.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -109,6 +112,9 @@ func init() { addonratecardDescNamespace := addonratecardMixinFields0[1].Descriptor() // addonratecard.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. addonratecard.NamespaceValidator = addonratecardDescNamespace.Validators[0].(func(string) error) + // addonratecardDescMetadata is the schema descriptor for metadata field. + addonratecardDescMetadata := addonratecardMixinFields0[2].Descriptor() + addonratecard.ValueScanner.Metadata = addonratecardDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // addonratecardDescCreatedAt is the schema descriptor for created_at field. addonratecardDescCreatedAt := addonratecardMixinFields0[3].Descriptor() // addonratecard.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -152,6 +158,9 @@ func init() { dbappDescNamespace := dbappMixinFields0[1].Descriptor() // dbapp.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. dbapp.NamespaceValidator = dbappDescNamespace.Validators[0].(func(string) error) + // dbappDescMetadata is the schema descriptor for metadata field. + dbappDescMetadata := dbappMixinFields0[2].Descriptor() + dbapp.ValueScanner.Metadata = dbappDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // dbappDescCreatedAt is the schema descriptor for created_at field. dbappDescCreatedAt := dbappMixinFields0[3].Descriptor() // dbapp.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -206,6 +215,8 @@ func init() { _ = appcustominvoicingcustomerMixinFields0 appcustominvoicingcustomerMixinFields1 := appcustominvoicingcustomerMixin[1].Fields() _ = appcustominvoicingcustomerMixinFields1 + appcustominvoicingcustomerMixinFields2 := appcustominvoicingcustomerMixin[2].Fields() + _ = appcustominvoicingcustomerMixinFields2 appcustominvoicingcustomerFields := schema.AppCustomInvoicingCustomer{}.Fields() _ = appcustominvoicingcustomerFields // appcustominvoicingcustomerDescNamespace is the schema descriptor for namespace field. @@ -222,6 +233,9 @@ func init() { appcustominvoicingcustomer.DefaultUpdatedAt = appcustominvoicingcustomerDescUpdatedAt.Default.(func() time.Time) // appcustominvoicingcustomer.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. appcustominvoicingcustomer.UpdateDefaultUpdatedAt = appcustominvoicingcustomerDescUpdatedAt.UpdateDefault.(func() time.Time) + // appcustominvoicingcustomerDescMetadata is the schema descriptor for metadata field. + appcustominvoicingcustomerDescMetadata := appcustominvoicingcustomerMixinFields2[0].Descriptor() + appcustominvoicingcustomer.ValueScanner.Metadata = appcustominvoicingcustomerDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // appcustominvoicingcustomerDescAppID is the schema descriptor for app_id field. appcustominvoicingcustomerDescAppID := appcustominvoicingcustomerFields[0].Descriptor() // appcustominvoicingcustomer.AppIDValidator is a validator for the "app_id" field. It is called by the builders before save. @@ -403,6 +417,8 @@ func init() { _ = billinginvoiceMixinFields0 billinginvoiceMixinFields1 := billinginvoiceMixin[1].Fields() _ = billinginvoiceMixinFields1 + billinginvoiceMixinFields2 := billinginvoiceMixin[2].Fields() + _ = billinginvoiceMixinFields2 billinginvoiceMixinFields3 := billinginvoiceMixin[3].Fields() _ = billinginvoiceMixinFields3 billinginvoiceMixinFields4 := billinginvoiceMixin[4].Fields() @@ -415,6 +431,9 @@ func init() { billinginvoiceDescNamespace := billinginvoiceMixinFields1[0].Descriptor() // billinginvoice.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. billinginvoice.NamespaceValidator = billinginvoiceDescNamespace.Validators[0].(func(string) error) + // billinginvoiceDescMetadata is the schema descriptor for metadata field. + billinginvoiceDescMetadata := billinginvoiceMixinFields2[0].Descriptor() + billinginvoice.ValueScanner.Metadata = billinginvoiceDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // billinginvoiceDescCreatedAt is the schema descriptor for created_at field. billinginvoiceDescCreatedAt := billinginvoiceMixinFields3[0].Descriptor() // billinginvoice.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -505,16 +524,24 @@ func init() { // billinginvoiceflatfeelineconfig.DefaultID holds the default value on creation for the id field. billinginvoiceflatfeelineconfig.DefaultID = billinginvoiceflatfeelineconfigDescID.Default.(func() string) billinginvoicelineMixin := schema.BillingInvoiceLine{}.Mixin() + billinginvoicelineMixinFields0 := billinginvoicelineMixin[0].Fields() + _ = billinginvoicelineMixinFields0 billinginvoicelineMixinFields1 := billinginvoicelineMixin[1].Fields() _ = billinginvoicelineMixinFields1 billinginvoicelineMixinFields2 := billinginvoicelineMixin[2].Fields() _ = billinginvoicelineMixinFields2 billinginvoicelineFields := schema.BillingInvoiceLine{}.Fields() _ = billinginvoicelineFields + // billinginvoicelineDescAnnotations is the schema descriptor for annotations field. + billinginvoicelineDescAnnotations := billinginvoicelineMixinFields0[0].Descriptor() + billinginvoiceline.ValueScanner.Annotations = billinginvoicelineDescAnnotations.ValueScanner.(field.TypeValueScanner[map[string]interface{}]) // billinginvoicelineDescNamespace is the schema descriptor for namespace field. billinginvoicelineDescNamespace := billinginvoicelineMixinFields1[1].Descriptor() // billinginvoiceline.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. billinginvoiceline.NamespaceValidator = billinginvoicelineDescNamespace.Validators[0].(func(string) error) + // billinginvoicelineDescMetadata is the schema descriptor for metadata field. + billinginvoicelineDescMetadata := billinginvoicelineMixinFields1[2].Descriptor() + billinginvoiceline.ValueScanner.Metadata = billinginvoicelineDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // billinginvoicelineDescCreatedAt is the schema descriptor for created_at field. billinginvoicelineDescCreatedAt := billinginvoicelineMixinFields1[3].Descriptor() // billinginvoiceline.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -607,6 +634,9 @@ func init() { billinginvoicesplitlinegroupDescNamespace := billinginvoicesplitlinegroupMixinFields0[1].Descriptor() // billinginvoicesplitlinegroup.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. billinginvoicesplitlinegroup.NamespaceValidator = billinginvoicesplitlinegroupDescNamespace.Validators[0].(func(string) error) + // billinginvoicesplitlinegroupDescMetadata is the schema descriptor for metadata field. + billinginvoicesplitlinegroupDescMetadata := billinginvoicesplitlinegroupMixinFields0[2].Descriptor() + billinginvoicesplitlinegroup.ValueScanner.Metadata = billinginvoicesplitlinegroupDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // billinginvoicesplitlinegroupDescCreatedAt is the schema descriptor for created_at field. billinginvoicesplitlinegroupDescCreatedAt := billinginvoicesplitlinegroupMixinFields0[3].Descriptor() // billinginvoicesplitlinegroup.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -713,6 +743,9 @@ func init() { billingprofileDescNamespace := billingprofileMixinFields0[1].Descriptor() // billingprofile.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. billingprofile.NamespaceValidator = billingprofileDescNamespace.Validators[0].(func(string) error) + // billingprofileDescMetadata is the schema descriptor for metadata field. + billingprofileDescMetadata := billingprofileMixinFields0[2].Descriptor() + billingprofile.ValueScanner.Metadata = billingprofileDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // billingprofileDescCreatedAt is the schema descriptor for created_at field. billingprofileDescCreatedAt := billingprofileMixinFields0[3].Descriptor() // billingprofile.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -806,12 +839,17 @@ func init() { _ = customerMixinFields0 customerMixinFields1 := customerMixin[1].Fields() _ = customerMixinFields1 + customerMixinFields2 := customerMixin[2].Fields() + _ = customerMixinFields2 customerFields := schema.Customer{}.Fields() _ = customerFields // customerDescNamespace is the schema descriptor for namespace field. customerDescNamespace := customerMixinFields0[1].Descriptor() // customer.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. customer.NamespaceValidator = customerDescNamespace.Validators[0].(func(string) error) + // customerDescMetadata is the schema descriptor for metadata field. + customerDescMetadata := customerMixinFields0[2].Descriptor() + customer.ValueScanner.Metadata = customerDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // customerDescCreatedAt is the schema descriptor for created_at field. customerDescCreatedAt := customerMixinFields0[3].Descriptor() // customer.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -840,6 +878,9 @@ func init() { return nil } }() + // customerDescAnnotations is the schema descriptor for annotations field. + customerDescAnnotations := customerMixinFields2[0].Descriptor() + customer.ValueScanner.Annotations = customerDescAnnotations.ValueScanner.(field.TypeValueScanner[map[string]interface{}]) // customerDescCurrency is the schema descriptor for currency field. customerDescCurrency := customerFields[2].Descriptor() // customer.CurrencyValidator is a validator for the "currency" field. It is called by the builders before save. @@ -888,6 +929,8 @@ func init() { _ = entitlementMixinFields0 entitlementMixinFields1 := entitlementMixin[1].Fields() _ = entitlementMixinFields1 + entitlementMixinFields2 := entitlementMixin[2].Fields() + _ = entitlementMixinFields2 entitlementMixinFields3 := entitlementMixin[3].Fields() _ = entitlementMixinFields3 entitlementFields := schema.Entitlement{}.Fields() @@ -896,6 +939,9 @@ func init() { entitlementDescNamespace := entitlementMixinFields1[0].Descriptor() // entitlement.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. entitlement.NamespaceValidator = entitlementDescNamespace.Validators[0].(func(string) error) + // entitlementDescMetadata is the schema descriptor for metadata field. + entitlementDescMetadata := entitlementMixinFields2[0].Descriptor() + entitlement.ValueScanner.Metadata = entitlementDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // entitlementDescCreatedAt is the schema descriptor for created_at field. entitlementDescCreatedAt := entitlementMixinFields3[0].Descriptor() // entitlement.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -940,6 +986,8 @@ func init() { _ = featureMixinFields0 featureMixinFields1 := featureMixin[1].Fields() _ = featureMixinFields1 + featureMixinFields2 := featureMixin[2].Fields() + _ = featureMixinFields2 featureFields := schema.Feature{}.Fields() _ = featureFields // featureDescCreatedAt is the schema descriptor for created_at field. @@ -952,6 +1000,9 @@ func init() { feature.DefaultUpdatedAt = featureDescUpdatedAt.Default.(func() time.Time) // feature.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. feature.UpdateDefaultUpdatedAt = featureDescUpdatedAt.UpdateDefault.(func() time.Time) + // featureDescMetadata is the schema descriptor for metadata field. + featureDescMetadata := featureMixinFields2[0].Descriptor() + feature.ValueScanner.Metadata = featureDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // featureDescNamespace is the schema descriptor for namespace field. featureDescNamespace := featureFields[0].Descriptor() // feature.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. @@ -973,6 +1024,8 @@ func init() { _ = dbgrantMixinFields0 dbgrantMixinFields1 := dbgrantMixin[1].Fields() _ = dbgrantMixinFields1 + dbgrantMixinFields2 := dbgrantMixin[2].Fields() + _ = dbgrantMixinFields2 dbgrantMixinFields3 := dbgrantMixin[3].Fields() _ = dbgrantMixinFields3 dbgrantFields := schema.Grant{}.Fields() @@ -981,6 +1034,9 @@ func init() { dbgrantDescNamespace := dbgrantMixinFields1[0].Descriptor() // dbgrant.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. dbgrant.NamespaceValidator = dbgrantDescNamespace.Validators[0].(func(string) error) + // dbgrantDescMetadata is the schema descriptor for metadata field. + dbgrantDescMetadata := dbgrantMixinFields2[0].Descriptor() + dbgrant.ValueScanner.Metadata = dbgrantDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // dbgrantDescCreatedAt is the schema descriptor for created_at field. dbgrantDescCreatedAt := dbgrantMixinFields3[0].Descriptor() // dbgrant.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -1008,6 +1064,9 @@ func init() { dbmeterDescNamespace := dbmeterMixinFields0[1].Descriptor() // dbmeter.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. dbmeter.NamespaceValidator = dbmeterDescNamespace.Validators[0].(func(string) error) + // dbmeterDescMetadata is the schema descriptor for metadata field. + dbmeterDescMetadata := dbmeterMixinFields0[2].Descriptor() + dbmeter.ValueScanner.Metadata = dbmeterDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // dbmeterDescCreatedAt is the schema descriptor for created_at field. dbmeterDescCreatedAt := dbmeterMixinFields0[3].Descriptor() // dbmeter.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -1170,6 +1229,9 @@ func init() { planDescNamespace := planMixinFields0[1].Descriptor() // plan.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. plan.NamespaceValidator = planDescNamespace.Validators[0].(func(string) error) + // planDescMetadata is the schema descriptor for metadata field. + planDescMetadata := planMixinFields0[2].Descriptor() + plan.ValueScanner.Metadata = planDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // planDescCreatedAt is the schema descriptor for created_at field. planDescCreatedAt := planMixinFields0[3].Descriptor() // plan.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -1208,6 +1270,10 @@ func init() { _ = planaddonMixinFields0 planaddonMixinFields1 := planaddonMixin[1].Fields() _ = planaddonMixinFields1 + planaddonMixinFields2 := planaddonMixin[2].Fields() + _ = planaddonMixinFields2 + planaddonMixinFields3 := planaddonMixin[3].Fields() + _ = planaddonMixinFields3 planaddonMixinFields4 := planaddonMixin[4].Fields() _ = planaddonMixinFields4 planaddonFields := schema.PlanAddon{}.Fields() @@ -1216,6 +1282,12 @@ func init() { planaddonDescNamespace := planaddonMixinFields1[0].Descriptor() // planaddon.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. planaddon.NamespaceValidator = planaddonDescNamespace.Validators[0].(func(string) error) + // planaddonDescMetadata is the schema descriptor for metadata field. + planaddonDescMetadata := planaddonMixinFields2[0].Descriptor() + planaddon.ValueScanner.Metadata = planaddonDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) + // planaddonDescAnnotations is the schema descriptor for annotations field. + planaddonDescAnnotations := planaddonMixinFields3[0].Descriptor() + planaddon.ValueScanner.Annotations = planaddonDescAnnotations.ValueScanner.(field.TypeValueScanner[map[string]interface{}]) // planaddonDescCreatedAt is the schema descriptor for created_at field. planaddonDescCreatedAt := planaddonMixinFields4[0].Descriptor() // planaddon.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -1247,6 +1319,9 @@ func init() { planphaseDescNamespace := planphaseMixinFields0[1].Descriptor() // planphase.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. planphase.NamespaceValidator = planphaseDescNamespace.Validators[0].(func(string) error) + // planphaseDescMetadata is the schema descriptor for metadata field. + planphaseDescMetadata := planphaseMixinFields0[2].Descriptor() + planphase.ValueScanner.Metadata = planphaseDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // planphaseDescCreatedAt is the schema descriptor for created_at field. planphaseDescCreatedAt := planphaseMixinFields0[3].Descriptor() // planphase.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -1278,6 +1353,9 @@ func init() { planratecardDescNamespace := planratecardMixinFields0[1].Descriptor() // planratecard.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. planratecard.NamespaceValidator = planratecardDescNamespace.Validators[0].(func(string) error) + // planratecardDescMetadata is the schema descriptor for metadata field. + planratecardDescMetadata := planratecardMixinFields0[2].Descriptor() + planratecard.ValueScanner.Metadata = planratecardDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // planratecardDescCreatedAt is the schema descriptor for created_at field. planratecardDescCreatedAt := planratecardMixinFields0[3].Descriptor() // planratecard.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -1348,6 +1426,8 @@ func init() { _ = subscriptionMixinFields1 subscriptionMixinFields2 := subscriptionMixin[2].Fields() _ = subscriptionMixinFields2 + subscriptionMixinFields3 := subscriptionMixin[3].Fields() + _ = subscriptionMixinFields3 subscriptionFields := schema.Subscription{}.Fields() _ = subscriptionFields // subscriptionDescNamespace is the schema descriptor for namespace field. @@ -1364,6 +1444,9 @@ func init() { subscription.DefaultUpdatedAt = subscriptionDescUpdatedAt.Default.(func() time.Time) // subscription.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. subscription.UpdateDefaultUpdatedAt = subscriptionDescUpdatedAt.UpdateDefault.(func() time.Time) + // subscriptionDescMetadata is the schema descriptor for metadata field. + subscriptionDescMetadata := subscriptionMixinFields3[0].Descriptor() + subscription.ValueScanner.Metadata = subscriptionDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // subscriptionDescName is the schema descriptor for name field. subscriptionDescName := subscriptionFields[0].Descriptor() // subscription.DefaultName holds the default value on creation for the name field. @@ -1407,6 +1490,8 @@ func init() { _ = subscriptionaddonMixinFields0 subscriptionaddonMixinFields1 := subscriptionaddonMixin[1].Fields() _ = subscriptionaddonMixinFields1 + subscriptionaddonMixinFields2 := subscriptionaddonMixin[2].Fields() + _ = subscriptionaddonMixinFields2 subscriptionaddonMixinFields3 := subscriptionaddonMixin[3].Fields() _ = subscriptionaddonMixinFields3 subscriptionaddonFields := schema.SubscriptionAddon{}.Fields() @@ -1415,6 +1500,9 @@ func init() { subscriptionaddonDescNamespace := subscriptionaddonMixinFields1[0].Descriptor() // subscriptionaddon.NamespaceValidator is a validator for the "namespace" field. It is called by the builders before save. subscriptionaddon.NamespaceValidator = subscriptionaddonDescNamespace.Validators[0].(func(string) error) + // subscriptionaddonDescMetadata is the schema descriptor for metadata field. + subscriptionaddonDescMetadata := subscriptionaddonMixinFields2[0].Descriptor() + subscriptionaddon.ValueScanner.Metadata = subscriptionaddonDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // subscriptionaddonDescCreatedAt is the schema descriptor for created_at field. subscriptionaddonDescCreatedAt := subscriptionaddonMixinFields3[0].Descriptor() // subscriptionaddon.DefaultCreatedAt holds the default value on creation for the created_at field. @@ -1485,6 +1573,8 @@ func init() { _ = subscriptionitemMixinFields1 subscriptionitemMixinFields2 := subscriptionitemMixin[2].Fields() _ = subscriptionitemMixinFields2 + subscriptionitemMixinFields3 := subscriptionitemMixin[3].Fields() + _ = subscriptionitemMixinFields3 subscriptionitemFields := schema.SubscriptionItem{}.Fields() _ = subscriptionitemFields // subscriptionitemDescNamespace is the schema descriptor for namespace field. @@ -1501,6 +1591,9 @@ func init() { subscriptionitem.DefaultUpdatedAt = subscriptionitemDescUpdatedAt.Default.(func() time.Time) // subscriptionitem.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. subscriptionitem.UpdateDefaultUpdatedAt = subscriptionitemDescUpdatedAt.UpdateDefault.(func() time.Time) + // subscriptionitemDescMetadata is the schema descriptor for metadata field. + subscriptionitemDescMetadata := subscriptionitemMixinFields3[0].Descriptor() + subscriptionitem.ValueScanner.Metadata = subscriptionitemDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // subscriptionitemDescAnnotations is the schema descriptor for annotations field. subscriptionitemDescAnnotations := subscriptionitemFields[0].Descriptor() subscriptionitem.ValueScanner.Annotations = subscriptionitemDescAnnotations.ValueScanner.(field.TypeValueScanner[map[string]interface{}]) @@ -1539,6 +1632,8 @@ func init() { _ = subscriptionphaseMixinFields1 subscriptionphaseMixinFields2 := subscriptionphaseMixin[2].Fields() _ = subscriptionphaseMixinFields2 + subscriptionphaseMixinFields3 := subscriptionphaseMixin[3].Fields() + _ = subscriptionphaseMixinFields3 subscriptionphaseFields := schema.SubscriptionPhase{}.Fields() _ = subscriptionphaseFields // subscriptionphaseDescNamespace is the schema descriptor for namespace field. @@ -1555,6 +1650,9 @@ func init() { subscriptionphase.DefaultUpdatedAt = subscriptionphaseDescUpdatedAt.Default.(func() time.Time) // subscriptionphase.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. subscriptionphase.UpdateDefaultUpdatedAt = subscriptionphaseDescUpdatedAt.UpdateDefault.(func() time.Time) + // subscriptionphaseDescMetadata is the schema descriptor for metadata field. + subscriptionphaseDescMetadata := subscriptionphaseMixinFields3[0].Descriptor() + subscriptionphase.ValueScanner.Metadata = subscriptionphaseDescMetadata.ValueScanner.(field.TypeValueScanner[map[string]string]) // subscriptionphaseDescSubscriptionID is the schema descriptor for subscription_id field. subscriptionphaseDescSubscriptionID := subscriptionphaseFields[0].Descriptor() // subscriptionphase.SubscriptionIDValidator is a validator for the "subscription_id" field. It is called by the builders before save. diff --git a/openmeter/ent/db/runtime/runtime.go b/openmeter/ent/db/runtime/runtime.go index 3766cd42ec..d13440d053 100644 --- a/openmeter/ent/db/runtime/runtime.go +++ b/openmeter/ent/db/runtime/runtime.go @@ -5,6 +5,5 @@ package runtime // The schema-stitching logic is generated in github.com/openmeterio/openmeter/openmeter/ent/db/runtime.go const ( - Version = "v0.14.5-0.20250325141242-9db6f4df431f" // Version of ent codegen. - Sum = "h1:AAfmRfBzbqJtKJ/eY31atgFcnFt5Hzf1EaXTQA8ICrw=" // Sum of ent codegen. + Version = "v0.14.6-0.20250731081001-175d96f7ecc2" // Version of ent codegen. ) diff --git a/openmeter/ent/db/subject_query.go b/openmeter/ent/db/subject_query.go index 6d4d2b1879..429f305936 100644 --- a/openmeter/ent/db/subject_query.go +++ b/openmeter/ent/db/subject_query.go @@ -480,41 +480,41 @@ type SubjectGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (sgb *SubjectGroupBy) Aggregate(fns ...AggregateFunc) *SubjectGroupBy { - sgb.fns = append(sgb.fns, fns...) - return sgb +func (_g *SubjectGroupBy) Aggregate(fns ...AggregateFunc) *SubjectGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (sgb *SubjectGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, sgb.build.ctx, ent.OpQueryGroupBy) - if err := sgb.build.prepareQuery(ctx); err != nil { +func (_g *SubjectGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubjectQuery, *SubjectGroupBy](ctx, sgb.build, sgb, sgb.build.inters, v) + return scanWithInterceptors[*SubjectQuery, *SubjectGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (sgb *SubjectGroupBy) sqlScan(ctx context.Context, root *SubjectQuery, v any) error { +func (_g *SubjectGroupBy) sqlScan(ctx context.Context, root *SubjectQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(sgb.fns)) - for _, fn := range sgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*sgb.flds)+len(sgb.fns)) - for _, f := range *sgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*sgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := sgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -528,27 +528,27 @@ type SubjectSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (ss *SubjectSelect) Aggregate(fns ...AggregateFunc) *SubjectSelect { - ss.fns = append(ss.fns, fns...) - return ss +func (_s *SubjectSelect) Aggregate(fns ...AggregateFunc) *SubjectSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (ss *SubjectSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ss.ctx, ent.OpQuerySelect) - if err := ss.prepareQuery(ctx); err != nil { +func (_s *SubjectSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubjectQuery, *SubjectSelect](ctx, ss.SubjectQuery, ss, ss.inters, v) + return scanWithInterceptors[*SubjectQuery, *SubjectSelect](ctx, _s.SubjectQuery, _s, _s.inters, v) } -func (ss *SubjectSelect) sqlScan(ctx context.Context, root *SubjectQuery, v any) error { +func (_s *SubjectSelect) sqlScan(ctx context.Context, root *SubjectQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(ss.fns)) - for _, fn := range ss.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*ss.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -556,7 +556,7 @@ func (ss *SubjectSelect) sqlScan(ctx context.Context, root *SubjectQuery, v any) } rows := &sql.Rows{} query, args := selector.Query() - if err := ss.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/subscription.go b/openmeter/ent/db/subscription.go index 2cf553d147..df0bc96e93 100644 --- a/openmeter/ent/db/subscription.go +++ b/openmeter/ent/db/subscription.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -141,12 +140,12 @@ func (*Subscription) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case subscription.FieldMetadata: - values[i] = new([]byte) case subscription.FieldID, subscription.FieldNamespace, subscription.FieldName, subscription.FieldDescription, subscription.FieldPlanID, subscription.FieldCustomerID, subscription.FieldCurrency, subscription.FieldBillingCadence: values[i] = new(sql.NullString) case subscription.FieldCreatedAt, subscription.FieldUpdatedAt, subscription.FieldDeletedAt, subscription.FieldActiveFrom, subscription.FieldActiveTo, subscription.FieldBillingAnchor: values[i] = new(sql.NullTime) + case subscription.FieldMetadata: + values[i] = subscription.ValueScanner.Metadata.ScanValue() case subscription.FieldProRatingConfig: values[i] = subscription.ValueScanner.ProRatingConfig.ScanValue() default: @@ -196,12 +195,10 @@ func (_m *Subscription) assignValues(columns []string, values []any) error { *_m.DeletedAt = value.Time } case subscription.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := subscription.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case subscription.FieldActiveFrom: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/subscription/subscription.go b/openmeter/ent/db/subscription/subscription.go index ff557e49bd..57b3b52ebb 100644 --- a/openmeter/ent/db/subscription/subscription.go +++ b/openmeter/ent/db/subscription/subscription.go @@ -157,6 +157,7 @@ var ( DefaultID func() string // ValueScanner of all Subscription fields. ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] ProRatingConfig field.TypeValueScanner[productcatalog.ProRatingConfig] } ) @@ -189,6 +190,11 @@ func ByDeletedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldDeletedAt, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByActiveFrom orders the results by the active_from field. func ByActiveFrom(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldActiveFrom, opts...).ToFunc() diff --git a/openmeter/ent/db/subscription_create.go b/openmeter/ent/db/subscription_create.go index 506973b0cd..3b3dc4923c 100644 --- a/openmeter/ent/db/subscription_create.go +++ b/openmeter/ent/db/subscription_create.go @@ -443,7 +443,11 @@ func (_c *SubscriptionCreate) createSpec() (*Subscription, *sqlgraph.CreateSpec, _node.DeletedAt = &value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(subscription.FieldMetadata, field.TypeJSON, value) + vv, err := subscription.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(subscription.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.ActiveFrom(); ok { diff --git a/openmeter/ent/db/subscription_query.go b/openmeter/ent/db/subscription_query.go index 92d6b6bdfb..5f70afffed 100644 --- a/openmeter/ent/db/subscription_query.go +++ b/openmeter/ent/db/subscription_query.go @@ -943,41 +943,41 @@ type SubscriptionGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (sgb *SubscriptionGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionGroupBy { - sgb.fns = append(sgb.fns, fns...) - return sgb +func (_g *SubscriptionGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (sgb *SubscriptionGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, sgb.build.ctx, ent.OpQueryGroupBy) - if err := sgb.build.prepareQuery(ctx); err != nil { +func (_g *SubscriptionGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionQuery, *SubscriptionGroupBy](ctx, sgb.build, sgb, sgb.build.inters, v) + return scanWithInterceptors[*SubscriptionQuery, *SubscriptionGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (sgb *SubscriptionGroupBy) sqlScan(ctx context.Context, root *SubscriptionQuery, v any) error { +func (_g *SubscriptionGroupBy) sqlScan(ctx context.Context, root *SubscriptionQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(sgb.fns)) - for _, fn := range sgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*sgb.flds)+len(sgb.fns)) - for _, f := range *sgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*sgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := sgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -991,27 +991,27 @@ type SubscriptionSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (ss *SubscriptionSelect) Aggregate(fns ...AggregateFunc) *SubscriptionSelect { - ss.fns = append(ss.fns, fns...) - return ss +func (_s *SubscriptionSelect) Aggregate(fns ...AggregateFunc) *SubscriptionSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (ss *SubscriptionSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, ss.ctx, ent.OpQuerySelect) - if err := ss.prepareQuery(ctx); err != nil { +func (_s *SubscriptionSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionQuery, *SubscriptionSelect](ctx, ss.SubscriptionQuery, ss, ss.inters, v) + return scanWithInterceptors[*SubscriptionQuery, *SubscriptionSelect](ctx, _s.SubscriptionQuery, _s, _s.inters, v) } -func (ss *SubscriptionSelect) sqlScan(ctx context.Context, root *SubscriptionQuery, v any) error { +func (_s *SubscriptionSelect) sqlScan(ctx context.Context, root *SubscriptionQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(ss.fns)) - for _, fn := range ss.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*ss.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -1019,7 +1019,7 @@ func (ss *SubscriptionSelect) sqlScan(ctx context.Context, root *SubscriptionQue } rows := &sql.Rows{} query, args := selector.Query() - if err := ss.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/subscription_update.go b/openmeter/ent/db/subscription_update.go index 8133fd7e71..61b5b40f33 100644 --- a/openmeter/ent/db/subscription_update.go +++ b/openmeter/ent/db/subscription_update.go @@ -425,10 +425,14 @@ func (_u *SubscriptionUpdate) sqlSave(ctx context.Context) (_node int, err error _spec.ClearField(subscription.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(subscription.FieldMetadata, field.TypeJSON, value) + vv, err := subscription.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(subscription.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(subscription.FieldMetadata, field.TypeJSON) + _spec.ClearField(subscription.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.ActiveTo(); ok { _spec.SetField(subscription.FieldActiveTo, field.TypeTime, value) @@ -1107,10 +1111,14 @@ func (_u *SubscriptionUpdateOne) sqlSave(ctx context.Context) (_node *Subscripti _spec.ClearField(subscription.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(subscription.FieldMetadata, field.TypeJSON, value) + vv, err := subscription.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(subscription.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(subscription.FieldMetadata, field.TypeJSON) + _spec.ClearField(subscription.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.ActiveTo(); ok { _spec.SetField(subscription.FieldActiveTo, field.TypeTime, value) diff --git a/openmeter/ent/db/subscriptionaddon.go b/openmeter/ent/db/subscriptionaddon.go index b9b27e834c..94036b6772 100644 --- a/openmeter/ent/db/subscriptionaddon.go +++ b/openmeter/ent/db/subscriptionaddon.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -89,12 +88,12 @@ func (*SubscriptionAddon) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case subscriptionaddon.FieldMetadata: - values[i] = new([]byte) case subscriptionaddon.FieldID, subscriptionaddon.FieldNamespace, subscriptionaddon.FieldAddonID, subscriptionaddon.FieldSubscriptionID: values[i] = new(sql.NullString) case subscriptionaddon.FieldCreatedAt, subscriptionaddon.FieldUpdatedAt, subscriptionaddon.FieldDeletedAt: values[i] = new(sql.NullTime) + case subscriptionaddon.FieldMetadata: + values[i] = subscriptionaddon.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -123,12 +122,10 @@ func (_m *SubscriptionAddon) assignValues(columns []string, values []any) error _m.Namespace = value.String } case subscriptionaddon.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := subscriptionaddon.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case subscriptionaddon.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { diff --git a/openmeter/ent/db/subscriptionaddon/subscriptionaddon.go b/openmeter/ent/db/subscriptionaddon/subscriptionaddon.go index f5175ab1d8..0e04626241 100644 --- a/openmeter/ent/db/subscriptionaddon/subscriptionaddon.go +++ b/openmeter/ent/db/subscriptionaddon/subscriptionaddon.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -96,6 +97,10 @@ var ( SubscriptionIDValidator func(string) error // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all SubscriptionAddon fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // OrderOption defines the ordering options for the SubscriptionAddon queries. @@ -111,6 +116,11 @@ func ByNamespace(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldNamespace, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/openmeter/ent/db/subscriptionaddon_create.go b/openmeter/ent/db/subscriptionaddon_create.go index 0faa5f6f9f..ee4d2ccf01 100644 --- a/openmeter/ent/db/subscriptionaddon_create.go +++ b/openmeter/ent/db/subscriptionaddon_create.go @@ -225,7 +225,10 @@ func (_c *SubscriptionAddonCreate) sqlSave(ctx context.Context) (*SubscriptionAd if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -244,7 +247,7 @@ func (_c *SubscriptionAddonCreate) sqlSave(ctx context.Context) (*SubscriptionAd return _node, nil } -func (_c *SubscriptionAddonCreate) createSpec() (*SubscriptionAddon, *sqlgraph.CreateSpec) { +func (_c *SubscriptionAddonCreate) createSpec() (*SubscriptionAddon, *sqlgraph.CreateSpec, error) { var ( _node = &SubscriptionAddon{config: _c.config} _spec = sqlgraph.NewCreateSpec(subscriptionaddon.Table, sqlgraph.NewFieldSpec(subscriptionaddon.FieldID, field.TypeString)) @@ -259,7 +262,11 @@ func (_c *SubscriptionAddonCreate) createSpec() (*SubscriptionAddon, *sqlgraph.C _node.Namespace = value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(subscriptionaddon.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionaddon.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(subscriptionaddon.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.CreatedAt(); ok { @@ -324,7 +331,7 @@ func (_c *SubscriptionAddonCreate) createSpec() (*SubscriptionAddon, *sqlgraph.C _node.AddonID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -608,7 +615,10 @@ func (_c *SubscriptionAddonCreateBulk) Save(ctx context.Context) ([]*Subscriptio } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/subscriptionaddon_query.go b/openmeter/ent/db/subscriptionaddon_query.go index 1f12038897..c0653fa26f 100644 --- a/openmeter/ent/db/subscriptionaddon_query.go +++ b/openmeter/ent/db/subscriptionaddon_query.go @@ -711,41 +711,41 @@ type SubscriptionAddonGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (sagb *SubscriptionAddonGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionAddonGroupBy { - sagb.fns = append(sagb.fns, fns...) - return sagb +func (_g *SubscriptionAddonGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionAddonGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (sagb *SubscriptionAddonGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, sagb.build.ctx, ent.OpQueryGroupBy) - if err := sagb.build.prepareQuery(ctx); err != nil { +func (_g *SubscriptionAddonGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionAddonQuery, *SubscriptionAddonGroupBy](ctx, sagb.build, sagb, sagb.build.inters, v) + return scanWithInterceptors[*SubscriptionAddonQuery, *SubscriptionAddonGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (sagb *SubscriptionAddonGroupBy) sqlScan(ctx context.Context, root *SubscriptionAddonQuery, v any) error { +func (_g *SubscriptionAddonGroupBy) sqlScan(ctx context.Context, root *SubscriptionAddonQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(sagb.fns)) - for _, fn := range sagb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*sagb.flds)+len(sagb.fns)) - for _, f := range *sagb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*sagb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := sagb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -759,27 +759,27 @@ type SubscriptionAddonSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (sas *SubscriptionAddonSelect) Aggregate(fns ...AggregateFunc) *SubscriptionAddonSelect { - sas.fns = append(sas.fns, fns...) - return sas +func (_s *SubscriptionAddonSelect) Aggregate(fns ...AggregateFunc) *SubscriptionAddonSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (sas *SubscriptionAddonSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, sas.ctx, ent.OpQuerySelect) - if err := sas.prepareQuery(ctx); err != nil { +func (_s *SubscriptionAddonSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionAddonQuery, *SubscriptionAddonSelect](ctx, sas.SubscriptionAddonQuery, sas, sas.inters, v) + return scanWithInterceptors[*SubscriptionAddonQuery, *SubscriptionAddonSelect](ctx, _s.SubscriptionAddonQuery, _s, _s.inters, v) } -func (sas *SubscriptionAddonSelect) sqlScan(ctx context.Context, root *SubscriptionAddonQuery, v any) error { +func (_s *SubscriptionAddonSelect) sqlScan(ctx context.Context, root *SubscriptionAddonQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(sas.fns)) - for _, fn := range sas.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*sas.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -787,7 +787,7 @@ func (sas *SubscriptionAddonSelect) sqlScan(ctx context.Context, root *Subscript } rows := &sql.Rows{} query, args := selector.Query() - if err := sas.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/subscriptionaddon_update.go b/openmeter/ent/db/subscriptionaddon_update.go index f0d8082fd9..8084b47f3a 100644 --- a/openmeter/ent/db/subscriptionaddon_update.go +++ b/openmeter/ent/db/subscriptionaddon_update.go @@ -168,10 +168,14 @@ func (_u *SubscriptionAddonUpdate) sqlSave(ctx context.Context) (_node int, err } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(subscriptionaddon.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionaddon.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(subscriptionaddon.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(subscriptionaddon.FieldMetadata, field.TypeJSON) + _spec.ClearField(subscriptionaddon.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(subscriptionaddon.FieldUpdatedAt, field.TypeTime, value) @@ -416,10 +420,14 @@ func (_u *SubscriptionAddonUpdateOne) sqlSave(ctx context.Context) (_node *Subsc } } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(subscriptionaddon.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionaddon.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(subscriptionaddon.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(subscriptionaddon.FieldMetadata, field.TypeJSON) + _spec.ClearField(subscriptionaddon.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.UpdatedAt(); ok { _spec.SetField(subscriptionaddon.FieldUpdatedAt, field.TypeTime, value) diff --git a/openmeter/ent/db/subscriptionaddonquantity_query.go b/openmeter/ent/db/subscriptionaddonquantity_query.go index 713023a2cd..6f1446868a 100644 --- a/openmeter/ent/db/subscriptionaddonquantity_query.go +++ b/openmeter/ent/db/subscriptionaddonquantity_query.go @@ -559,41 +559,41 @@ type SubscriptionAddonQuantityGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (saqgb *SubscriptionAddonQuantityGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionAddonQuantityGroupBy { - saqgb.fns = append(saqgb.fns, fns...) - return saqgb +func (_g *SubscriptionAddonQuantityGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionAddonQuantityGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (saqgb *SubscriptionAddonQuantityGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, saqgb.build.ctx, ent.OpQueryGroupBy) - if err := saqgb.build.prepareQuery(ctx); err != nil { +func (_g *SubscriptionAddonQuantityGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionAddonQuantityQuery, *SubscriptionAddonQuantityGroupBy](ctx, saqgb.build, saqgb, saqgb.build.inters, v) + return scanWithInterceptors[*SubscriptionAddonQuantityQuery, *SubscriptionAddonQuantityGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (saqgb *SubscriptionAddonQuantityGroupBy) sqlScan(ctx context.Context, root *SubscriptionAddonQuantityQuery, v any) error { +func (_g *SubscriptionAddonQuantityGroupBy) sqlScan(ctx context.Context, root *SubscriptionAddonQuantityQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(saqgb.fns)) - for _, fn := range saqgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*saqgb.flds)+len(saqgb.fns)) - for _, f := range *saqgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*saqgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := saqgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -607,27 +607,27 @@ type SubscriptionAddonQuantitySelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (saqs *SubscriptionAddonQuantitySelect) Aggregate(fns ...AggregateFunc) *SubscriptionAddonQuantitySelect { - saqs.fns = append(saqs.fns, fns...) - return saqs +func (_s *SubscriptionAddonQuantitySelect) Aggregate(fns ...AggregateFunc) *SubscriptionAddonQuantitySelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (saqs *SubscriptionAddonQuantitySelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, saqs.ctx, ent.OpQuerySelect) - if err := saqs.prepareQuery(ctx); err != nil { +func (_s *SubscriptionAddonQuantitySelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionAddonQuantityQuery, *SubscriptionAddonQuantitySelect](ctx, saqs.SubscriptionAddonQuantityQuery, saqs, saqs.inters, v) + return scanWithInterceptors[*SubscriptionAddonQuantityQuery, *SubscriptionAddonQuantitySelect](ctx, _s.SubscriptionAddonQuantityQuery, _s, _s.inters, v) } -func (saqs *SubscriptionAddonQuantitySelect) sqlScan(ctx context.Context, root *SubscriptionAddonQuantityQuery, v any) error { +func (_s *SubscriptionAddonQuantitySelect) sqlScan(ctx context.Context, root *SubscriptionAddonQuantityQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(saqs.fns)) - for _, fn := range saqs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*saqs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -635,7 +635,7 @@ func (saqs *SubscriptionAddonQuantitySelect) sqlScan(ctx context.Context, root * } rows := &sql.Rows{} query, args := selector.Query() - if err := saqs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/subscriptionitem.go b/openmeter/ent/db/subscriptionitem.go index 8e470b4b83..bd2f09851c 100644 --- a/openmeter/ent/db/subscriptionitem.go +++ b/openmeter/ent/db/subscriptionitem.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -132,14 +131,14 @@ func (*SubscriptionItem) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case subscriptionitem.FieldMetadata: - values[i] = new([]byte) case subscriptionitem.FieldRestartsBillingPeriod: values[i] = new(sql.NullBool) case subscriptionitem.FieldID, subscriptionitem.FieldNamespace, subscriptionitem.FieldPhaseID, subscriptionitem.FieldKey, subscriptionitem.FieldEntitlementID, subscriptionitem.FieldActiveFromOverrideRelativeToPhaseStart, subscriptionitem.FieldActiveToOverrideRelativeToPhaseStart, subscriptionitem.FieldName, subscriptionitem.FieldDescription, subscriptionitem.FieldFeatureKey, subscriptionitem.FieldBillingCadence: values[i] = new(sql.NullString) case subscriptionitem.FieldCreatedAt, subscriptionitem.FieldUpdatedAt, subscriptionitem.FieldDeletedAt, subscriptionitem.FieldActiveFrom, subscriptionitem.FieldActiveTo: values[i] = new(sql.NullTime) + case subscriptionitem.FieldMetadata: + values[i] = subscriptionitem.ValueScanner.Metadata.ScanValue() case subscriptionitem.FieldAnnotations: values[i] = subscriptionitem.ValueScanner.Annotations.ScanValue() case subscriptionitem.FieldEntitlementTemplate: @@ -197,12 +196,10 @@ func (_m *SubscriptionItem) assignValues(columns []string, values []any) error { *_m.DeletedAt = value.Time } case subscriptionitem.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := subscriptionitem.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case subscriptionitem.FieldAnnotations: if value, err := subscriptionitem.ValueScanner.Annotations.FromValue(values[i]); err != nil { diff --git a/openmeter/ent/db/subscriptionitem/subscriptionitem.go b/openmeter/ent/db/subscriptionitem/subscriptionitem.go index bcd6bb7387..05e51a8752 100644 --- a/openmeter/ent/db/subscriptionitem/subscriptionitem.go +++ b/openmeter/ent/db/subscriptionitem/subscriptionitem.go @@ -156,6 +156,7 @@ var ( DefaultID func() string // ValueScanner of all SubscriptionItem fields. ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] Annotations field.TypeValueScanner[map[string]interface{}] EntitlementTemplate field.TypeValueScanner[*productcatalog.EntitlementTemplate] TaxConfig field.TypeValueScanner[*productcatalog.TaxConfig] @@ -192,6 +193,11 @@ func ByDeletedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldDeletedAt, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // ByAnnotations orders the results by the annotations field. func ByAnnotations(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldAnnotations, opts...).ToFunc() diff --git a/openmeter/ent/db/subscriptionitem_create.go b/openmeter/ent/db/subscriptionitem_create.go index e4794a06f1..d11e6779a4 100644 --- a/openmeter/ent/db/subscriptionitem_create.go +++ b/openmeter/ent/db/subscriptionitem_create.go @@ -474,7 +474,11 @@ func (_c *SubscriptionItemCreate) createSpec() (*SubscriptionItem, *sqlgraph.Cre _node.DeletedAt = &value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(subscriptionitem.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionitem.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(subscriptionitem.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.Annotations(); ok { diff --git a/openmeter/ent/db/subscriptionitem_query.go b/openmeter/ent/db/subscriptionitem_query.go index 7bec19e10f..356e480c79 100644 --- a/openmeter/ent/db/subscriptionitem_query.go +++ b/openmeter/ent/db/subscriptionitem_query.go @@ -797,41 +797,41 @@ type SubscriptionItemGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (sigb *SubscriptionItemGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionItemGroupBy { - sigb.fns = append(sigb.fns, fns...) - return sigb +func (_g *SubscriptionItemGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionItemGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (sigb *SubscriptionItemGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, sigb.build.ctx, ent.OpQueryGroupBy) - if err := sigb.build.prepareQuery(ctx); err != nil { +func (_g *SubscriptionItemGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionItemQuery, *SubscriptionItemGroupBy](ctx, sigb.build, sigb, sigb.build.inters, v) + return scanWithInterceptors[*SubscriptionItemQuery, *SubscriptionItemGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (sigb *SubscriptionItemGroupBy) sqlScan(ctx context.Context, root *SubscriptionItemQuery, v any) error { +func (_g *SubscriptionItemGroupBy) sqlScan(ctx context.Context, root *SubscriptionItemQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(sigb.fns)) - for _, fn := range sigb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*sigb.flds)+len(sigb.fns)) - for _, f := range *sigb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*sigb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := sigb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -845,27 +845,27 @@ type SubscriptionItemSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (sis *SubscriptionItemSelect) Aggregate(fns ...AggregateFunc) *SubscriptionItemSelect { - sis.fns = append(sis.fns, fns...) - return sis +func (_s *SubscriptionItemSelect) Aggregate(fns ...AggregateFunc) *SubscriptionItemSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (sis *SubscriptionItemSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, sis.ctx, ent.OpQuerySelect) - if err := sis.prepareQuery(ctx); err != nil { +func (_s *SubscriptionItemSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionItemQuery, *SubscriptionItemSelect](ctx, sis.SubscriptionItemQuery, sis, sis.inters, v) + return scanWithInterceptors[*SubscriptionItemQuery, *SubscriptionItemSelect](ctx, _s.SubscriptionItemQuery, _s, _s.inters, v) } -func (sis *SubscriptionItemSelect) sqlScan(ctx context.Context, root *SubscriptionItemQuery, v any) error { +func (_s *SubscriptionItemSelect) sqlScan(ctx context.Context, root *SubscriptionItemQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(sis.fns)) - for _, fn := range sis.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*sis.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -873,7 +873,7 @@ func (sis *SubscriptionItemSelect) sqlScan(ctx context.Context, root *Subscripti } rows := &sql.Rows{} query, args := selector.Query() - if err := sis.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/subscriptionitem_update.go b/openmeter/ent/db/subscriptionitem_update.go index a5a1e81d4d..e7f93ef4b0 100644 --- a/openmeter/ent/db/subscriptionitem_update.go +++ b/openmeter/ent/db/subscriptionitem_update.go @@ -498,10 +498,14 @@ func (_u *SubscriptionItemUpdate) sqlSave(ctx context.Context) (_node int, err e _spec.ClearField(subscriptionitem.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(subscriptionitem.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionitem.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(subscriptionitem.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(subscriptionitem.FieldMetadata, field.TypeJSON) + _spec.ClearField(subscriptionitem.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.Annotations(); ok { vv, err := subscriptionitem.ValueScanner.Annotations.Value(value) @@ -1235,10 +1239,14 @@ func (_u *SubscriptionItemUpdateOne) sqlSave(ctx context.Context) (_node *Subscr _spec.ClearField(subscriptionitem.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(subscriptionitem.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionitem.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(subscriptionitem.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(subscriptionitem.FieldMetadata, field.TypeJSON) + _spec.ClearField(subscriptionitem.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.Annotations(); ok { vv, err := subscriptionitem.ValueScanner.Annotations.Value(value) diff --git a/openmeter/ent/db/subscriptionphase.go b/openmeter/ent/db/subscriptionphase.go index 5f03cdb765..0765247149 100644 --- a/openmeter/ent/db/subscriptionphase.go +++ b/openmeter/ent/db/subscriptionphase.go @@ -3,7 +3,6 @@ package db import ( - "encoding/json" "fmt" "strings" "time" @@ -105,14 +104,14 @@ func (*SubscriptionPhase) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case subscriptionphase.FieldMetadata: - values[i] = new([]byte) case subscriptionphase.FieldSortHint: values[i] = new(sql.NullInt64) case subscriptionphase.FieldID, subscriptionphase.FieldNamespace, subscriptionphase.FieldSubscriptionID, subscriptionphase.FieldKey, subscriptionphase.FieldName, subscriptionphase.FieldDescription: values[i] = new(sql.NullString) case subscriptionphase.FieldCreatedAt, subscriptionphase.FieldUpdatedAt, subscriptionphase.FieldDeletedAt, subscriptionphase.FieldActiveFrom: values[i] = new(sql.NullTime) + case subscriptionphase.FieldMetadata: + values[i] = subscriptionphase.ValueScanner.Metadata.ScanValue() default: values[i] = new(sql.UnknownType) } @@ -160,12 +159,10 @@ func (_m *SubscriptionPhase) assignValues(columns []string, values []any) error *_m.DeletedAt = value.Time } case subscriptionphase.FieldMetadata: - if value, ok := values[i].(*[]byte); !ok { - return fmt.Errorf("unexpected type %T for field metadata", values[i]) - } else if value != nil && len(*value) > 0 { - if err := json.Unmarshal(*value, &_m.Metadata); err != nil { - return fmt.Errorf("unmarshal field metadata: %w", err) - } + if value, err := subscriptionphase.ValueScanner.Metadata.FromValue(values[i]); err != nil { + return err + } else { + _m.Metadata = value } case subscriptionphase.FieldSubscriptionID: if value, ok := values[i].(*sql.NullString); !ok { diff --git a/openmeter/ent/db/subscriptionphase/subscriptionphase.go b/openmeter/ent/db/subscriptionphase/subscriptionphase.go index 8b6329b169..06d17ba280 100644 --- a/openmeter/ent/db/subscriptionphase/subscriptionphase.go +++ b/openmeter/ent/db/subscriptionphase/subscriptionphase.go @@ -7,6 +7,7 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" ) const ( @@ -119,6 +120,10 @@ var ( NameValidator func(string) error // DefaultID holds the default value on creation for the "id" field. DefaultID func() string + // ValueScanner of all SubscriptionPhase fields. + ValueScanner struct { + Metadata field.TypeValueScanner[map[string]string] + } ) // OrderOption defines the ordering options for the SubscriptionPhase queries. @@ -149,6 +154,11 @@ func ByDeletedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldDeletedAt, opts...).ToFunc() } +// ByMetadata orders the results by the metadata field. +func ByMetadata(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldMetadata, opts...).ToFunc() +} + // BySubscriptionID orders the results by the subscription_id field. func BySubscriptionID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldSubscriptionID, opts...).ToFunc() diff --git a/openmeter/ent/db/subscriptionphase_create.go b/openmeter/ent/db/subscriptionphase_create.go index 0003ff62ba..963c890b57 100644 --- a/openmeter/ent/db/subscriptionphase_create.go +++ b/openmeter/ent/db/subscriptionphase_create.go @@ -299,7 +299,10 @@ func (_c *SubscriptionPhaseCreate) sqlSave(ctx context.Context) (*SubscriptionPh if err := _c.check(); err != nil { return nil, err } - _node, _spec := _c.createSpec() + _node, _spec, err := _c.createSpec() + if err != nil { + return nil, err + } if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} @@ -318,7 +321,7 @@ func (_c *SubscriptionPhaseCreate) sqlSave(ctx context.Context) (*SubscriptionPh return _node, nil } -func (_c *SubscriptionPhaseCreate) createSpec() (*SubscriptionPhase, *sqlgraph.CreateSpec) { +func (_c *SubscriptionPhaseCreate) createSpec() (*SubscriptionPhase, *sqlgraph.CreateSpec, error) { var ( _node = &SubscriptionPhase{config: _c.config} _spec = sqlgraph.NewCreateSpec(subscriptionphase.Table, sqlgraph.NewFieldSpec(subscriptionphase.FieldID, field.TypeString)) @@ -345,7 +348,11 @@ func (_c *SubscriptionPhaseCreate) createSpec() (*SubscriptionPhase, *sqlgraph.C _node.DeletedAt = &value } if value, ok := _c.mutation.Metadata(); ok { - _spec.SetField(subscriptionphase.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionphase.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, nil, err + } + _spec.SetField(subscriptionphase.FieldMetadata, field.TypeString, vv) _node.Metadata = value } if value, ok := _c.mutation.Key(); ok { @@ -433,7 +440,7 @@ func (_c *SubscriptionPhaseCreate) createSpec() (*SubscriptionPhase, *sqlgraph.C } _spec.Edges = append(_spec.Edges, edge) } - return _node, _spec + return _node, _spec, nil } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause @@ -837,7 +844,10 @@ func (_c *SubscriptionPhaseCreateBulk) Save(ctx context.Context) ([]*Subscriptio } builder.mutation = mutation var err error - nodes[i], specs[i] = builder.createSpec() + nodes[i], specs[i], err = builder.createSpec() + if err != nil { + return nil, err + } if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { diff --git a/openmeter/ent/db/subscriptionphase_query.go b/openmeter/ent/db/subscriptionphase_query.go index c467b9994d..dfb3c38918 100644 --- a/openmeter/ent/db/subscriptionphase_query.go +++ b/openmeter/ent/db/subscriptionphase_query.go @@ -793,41 +793,41 @@ type SubscriptionPhaseGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (spgb *SubscriptionPhaseGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionPhaseGroupBy { - spgb.fns = append(spgb.fns, fns...) - return spgb +func (_g *SubscriptionPhaseGroupBy) Aggregate(fns ...AggregateFunc) *SubscriptionPhaseGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (spgb *SubscriptionPhaseGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, spgb.build.ctx, ent.OpQueryGroupBy) - if err := spgb.build.prepareQuery(ctx); err != nil { +func (_g *SubscriptionPhaseGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionPhaseQuery, *SubscriptionPhaseGroupBy](ctx, spgb.build, spgb, spgb.build.inters, v) + return scanWithInterceptors[*SubscriptionPhaseQuery, *SubscriptionPhaseGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (spgb *SubscriptionPhaseGroupBy) sqlScan(ctx context.Context, root *SubscriptionPhaseQuery, v any) error { +func (_g *SubscriptionPhaseGroupBy) sqlScan(ctx context.Context, root *SubscriptionPhaseQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(spgb.fns)) - for _, fn := range spgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*spgb.flds)+len(spgb.fns)) - for _, f := range *spgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*spgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := spgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -841,27 +841,27 @@ type SubscriptionPhaseSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (sps *SubscriptionPhaseSelect) Aggregate(fns ...AggregateFunc) *SubscriptionPhaseSelect { - sps.fns = append(sps.fns, fns...) - return sps +func (_s *SubscriptionPhaseSelect) Aggregate(fns ...AggregateFunc) *SubscriptionPhaseSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (sps *SubscriptionPhaseSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, sps.ctx, ent.OpQuerySelect) - if err := sps.prepareQuery(ctx); err != nil { +func (_s *SubscriptionPhaseSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*SubscriptionPhaseQuery, *SubscriptionPhaseSelect](ctx, sps.SubscriptionPhaseQuery, sps, sps.inters, v) + return scanWithInterceptors[*SubscriptionPhaseQuery, *SubscriptionPhaseSelect](ctx, _s.SubscriptionPhaseQuery, _s, _s.inters, v) } -func (sps *SubscriptionPhaseSelect) sqlScan(ctx context.Context, root *SubscriptionPhaseQuery, v any) error { +func (_s *SubscriptionPhaseSelect) sqlScan(ctx context.Context, root *SubscriptionPhaseQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(sps.fns)) - for _, fn := range sps.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*sps.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -869,7 +869,7 @@ func (sps *SubscriptionPhaseSelect) sqlScan(ctx context.Context, root *Subscript } rows := &sql.Rows{} query, args := selector.Query() - if err := sps.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/openmeter/ent/db/subscriptionphase_update.go b/openmeter/ent/db/subscriptionphase_update.go index 05cb01ba08..edd9e5760a 100644 --- a/openmeter/ent/db/subscriptionphase_update.go +++ b/openmeter/ent/db/subscriptionphase_update.go @@ -314,10 +314,14 @@ func (_u *SubscriptionPhaseUpdate) sqlSave(ctx context.Context) (_node int, err _spec.ClearField(subscriptionphase.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(subscriptionphase.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionphase.ValueScanner.Metadata.Value(value) + if err != nil { + return 0, err + } + _spec.SetField(subscriptionphase.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(subscriptionphase.FieldMetadata, field.TypeJSON) + _spec.ClearField(subscriptionphase.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.Name(); ok { _spec.SetField(subscriptionphase.FieldName, field.TypeString, value) @@ -805,10 +809,14 @@ func (_u *SubscriptionPhaseUpdateOne) sqlSave(ctx context.Context) (_node *Subsc _spec.ClearField(subscriptionphase.FieldDeletedAt, field.TypeTime) } if value, ok := _u.mutation.Metadata(); ok { - _spec.SetField(subscriptionphase.FieldMetadata, field.TypeJSON, value) + vv, err := subscriptionphase.ValueScanner.Metadata.Value(value) + if err != nil { + return nil, err + } + _spec.SetField(subscriptionphase.FieldMetadata, field.TypeString, vv) } if _u.mutation.MetadataCleared() { - _spec.ClearField(subscriptionphase.FieldMetadata, field.TypeJSON) + _spec.ClearField(subscriptionphase.FieldMetadata, field.TypeString) } if value, ok := _u.mutation.Name(); ok { _spec.SetField(subscriptionphase.FieldName, field.TypeString, value) diff --git a/openmeter/ent/db/usagereset_query.go b/openmeter/ent/db/usagereset_query.go index 61ed744513..7ff52e1844 100644 --- a/openmeter/ent/db/usagereset_query.go +++ b/openmeter/ent/db/usagereset_query.go @@ -559,41 +559,41 @@ type UsageResetGroupBy struct { } // Aggregate adds the given aggregation functions to the group-by query. -func (urgb *UsageResetGroupBy) Aggregate(fns ...AggregateFunc) *UsageResetGroupBy { - urgb.fns = append(urgb.fns, fns...) - return urgb +func (_g *UsageResetGroupBy) Aggregate(fns ...AggregateFunc) *UsageResetGroupBy { + _g.fns = append(_g.fns, fns...) + return _g } // Scan applies the selector query and scans the result into the given value. -func (urgb *UsageResetGroupBy) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, urgb.build.ctx, ent.OpQueryGroupBy) - if err := urgb.build.prepareQuery(ctx); err != nil { +func (_g *UsageResetGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy) + if err := _g.build.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*UsageResetQuery, *UsageResetGroupBy](ctx, urgb.build, urgb, urgb.build.inters, v) + return scanWithInterceptors[*UsageResetQuery, *UsageResetGroupBy](ctx, _g.build, _g, _g.build.inters, v) } -func (urgb *UsageResetGroupBy) sqlScan(ctx context.Context, root *UsageResetQuery, v any) error { +func (_g *UsageResetGroupBy) sqlScan(ctx context.Context, root *UsageResetQuery, v any) error { selector := root.sqlQuery(ctx).Select() - aggregation := make([]string, 0, len(urgb.fns)) - for _, fn := range urgb.fns { + aggregation := make([]string, 0, len(_g.fns)) + for _, fn := range _g.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { - columns := make([]string, 0, len(*urgb.flds)+len(urgb.fns)) - for _, f := range *urgb.flds { + columns := make([]string, 0, len(*_g.flds)+len(_g.fns)) + for _, f := range *_g.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } - selector.GroupBy(selector.Columns(*urgb.flds...)...) + selector.GroupBy(selector.Columns(*_g.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() - if err := urgb.build.driver.Query(ctx, query, args, rows); err != nil { + if err := _g.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() @@ -607,27 +607,27 @@ type UsageResetSelect struct { } // Aggregate adds the given aggregation functions to the selector query. -func (urs *UsageResetSelect) Aggregate(fns ...AggregateFunc) *UsageResetSelect { - urs.fns = append(urs.fns, fns...) - return urs +func (_s *UsageResetSelect) Aggregate(fns ...AggregateFunc) *UsageResetSelect { + _s.fns = append(_s.fns, fns...) + return _s } // Scan applies the selector query and scans the result into the given value. -func (urs *UsageResetSelect) Scan(ctx context.Context, v any) error { - ctx = setContextOp(ctx, urs.ctx, ent.OpQuerySelect) - if err := urs.prepareQuery(ctx); err != nil { +func (_s *UsageResetSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect) + if err := _s.prepareQuery(ctx); err != nil { return err } - return scanWithInterceptors[*UsageResetQuery, *UsageResetSelect](ctx, urs.UsageResetQuery, urs, urs.inters, v) + return scanWithInterceptors[*UsageResetQuery, *UsageResetSelect](ctx, _s.UsageResetQuery, _s, _s.inters, v) } -func (urs *UsageResetSelect) sqlScan(ctx context.Context, root *UsageResetQuery, v any) error { +func (_s *UsageResetSelect) sqlScan(ctx context.Context, root *UsageResetQuery, v any) error { selector := root.sqlQuery(ctx) - aggregation := make([]string, 0, len(urs.fns)) - for _, fn := range urs.fns { + aggregation := make([]string, 0, len(_s.fns)) + for _, fn := range _s.fns { aggregation = append(aggregation, fn(selector)) } - switch n := len(*urs.selector.flds); { + switch n := len(*_s.selector.flds); { case n == 0 && len(aggregation) > 0: selector.Select(aggregation...) case n != 0 && len(aggregation) > 0: @@ -635,7 +635,7 @@ func (urs *UsageResetSelect) sqlScan(ctx context.Context, root *UsageResetQuery, } rows := &sql.Rows{} query, args := selector.Query() - if err := urs.driver.Query(ctx, query, args, rows); err != nil { + if err := _s.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() diff --git a/pkg/framework/entutils/mixins.go b/pkg/framework/entutils/mixins.go index 894925cbc0..4142a411a7 100644 --- a/pkg/framework/entutils/mixins.go +++ b/pkg/framework/entutils/mixins.go @@ -145,7 +145,9 @@ type MetadataMixin struct { // Fields of the IDMixin. func (MetadataMixin) Fields() []ent.Field { return []ent.Field{ - field.JSON("metadata", map[string]string{}). + field.String("metadata"). + GoType(map[string]string{}). + ValueScanner(JSONStringToMapValueScanner[string, string]()). Optional(). SchemaType(map[string]string{ dialect.Postgres: "jsonb", @@ -161,7 +163,9 @@ type AnnotationsMixin struct { // Fields of the IDMixin. func (AnnotationsMixin) Fields() []ent.Field { return []ent.Field{ - field.JSON("annotations", models.Annotations{}). + field.String("annotations"). + GoType(models.Annotations{}). + ValueScanner(JSONStringToMapValueScanner[string, interface{}]()). Optional(). SchemaType(map[string]string{ dialect.Postgres: "jsonb", diff --git a/pkg/framework/entutils/validators.go b/pkg/framework/entutils/validators.go new file mode 100644 index 0000000000..5aa72dc336 --- /dev/null +++ b/pkg/framework/entutils/validators.go @@ -0,0 +1,5 @@ +package entutils + +func NoopValidator[T any](_ T) error { + return nil +} diff --git a/pkg/framework/entutils/valuescanner.go b/pkg/framework/entutils/valuescanner.go index 1dc658164a..9d32849d2f 100644 --- a/pkg/framework/entutils/valuescanner.go +++ b/pkg/framework/entutils/valuescanner.go @@ -28,3 +28,28 @@ func JSONStringValueScanner[T any]() field.ValueScannerFunc[T, *sql.NullString] }, } } + +func JSONStringToMapValueScanner[K comparable, V any]() field.ValueScannerFunc[map[K]V, *sql.NullString] { + return field.ValueScannerFunc[map[K]V, *sql.NullString]{ + V: func(t map[K]V) (driver.Value, error) { + if len(t) == 0 { + return nil, nil + } + + return json.Marshal(t) + }, + S: func(ns *sql.NullString) (map[K]V, error) { + v := new(map[K]V) + if ns == nil || !ns.Valid { + return *v, nil + } + + b := []byte(ns.String) + if err := json.Unmarshal(b, v); err != nil { + return *v, err + } + + return *v, nil + }, + } +}