From f309ff9922153576dfe18e90ca4e2b0bc6b97f21 Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Thu, 16 Oct 2025 18:32:42 +0200 Subject: [PATCH] entproto: Added PhpNamespace() message option Signed-off-by: Vincent Composieux --- entproto/README.md | 13 +- entproto/adapter.go | 22 +- .../internal/entprototest/adapter_test.go | 6 + entproto/internal/entprototest/ent/client.go | 163 +++++- entproto/internal/entprototest/ent/ent.go | 2 + .../internal/entprototest/ent/hook/hook.go | 12 + .../ent/messagewithphpnamespace.go | 103 ++++ .../messagewithphpnamespace.go | 47 ++ .../ent/messagewithphpnamespace/where.go | 138 +++++ .../ent/messagewithphpnamespace_create.go | 183 ++++++ .../ent/messagewithphpnamespace_delete.go | 88 +++ .../ent/messagewithphpnamespace_query.go | 527 ++++++++++++++++++ .../ent/messagewithphpnamespace_update.go | 209 +++++++ .../entprototest/ent/migrate/schema.go | 12 + .../internal/entprototest/ent/mutation.go | 328 +++++++++++ .../entprototest/ent/predicate/predicate.go | 3 + .../ent/schema/message_with_php_namespace.go | 44 ++ entproto/internal/entprototest/ent/tx.go | 3 + entproto/message.go | 12 +- 19 files changed, 1900 insertions(+), 15 deletions(-) create mode 100644 entproto/internal/entprototest/ent/messagewithphpnamespace.go create mode 100644 entproto/internal/entprototest/ent/messagewithphpnamespace/messagewithphpnamespace.go create mode 100644 entproto/internal/entprototest/ent/messagewithphpnamespace/where.go create mode 100644 entproto/internal/entprototest/ent/messagewithphpnamespace_create.go create mode 100644 entproto/internal/entprototest/ent/messagewithphpnamespace_delete.go create mode 100644 entproto/internal/entprototest/ent/messagewithphpnamespace_query.go create mode 100644 entproto/internal/entprototest/ent/messagewithphpnamespace_update.go create mode 100644 entproto/internal/entprototest/ent/schema/message_with_php_namespace.go diff --git a/entproto/README.md b/entproto/README.md index 1a901ebf1..4e4176c47 100644 --- a/entproto/README.md +++ b/entproto/README.md @@ -174,7 +174,6 @@ func (User) Annotations() []schema.Annotation { By default the proto package name for the generated files will be `entpb` but it can be specified using a functional option: ```go - func (MessageWithPackageName) Annotations() []schema.Annotation { return []schema.Annotation{entproto.Message( entproto.PackageName("io.entgo.apps.todo"), @@ -182,6 +181,16 @@ func (MessageWithPackageName) Annotations() []schema.Annotation { } ``` +In case you want to add a custom PHP namespace, you can also add the following option: + +```go +func (MessageWithPackageName) Annotations() []schema.Annotation { + return []schema.Annotation{entproto.Message( + entproto.PhpNamespace("My\\Company\\Todo"), + )} +} +``` + Per the protobuf style guide: > Package name should be in lowercase, and should correspond to the directory hierarchy. e.g., if a file is in my/package/, then the package name should be my.package. @@ -331,7 +340,7 @@ message User { Field type mappings: | Ent Type | Proto Type | More considerations | -|----------------|---------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| -------------- | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | TypeBool | bool | | | TypeTime | google.protobuf.Timestamp | | | TypeJSON\[[]T] | repeated T | T must be one of: `string`, `int32`, `int64`, `uint32`, `uint64` | diff --git a/entproto/adapter.go b/entproto/adapter.go index a34ac2766..a958ab3aa 100644 --- a/entproto/adapter.go +++ b/entproto/adapter.go @@ -121,15 +121,35 @@ func (a *Adapter) parse() error { if _, ok := protoPackages[protoPkg]; !ok { goPkg := a.goPackageName(protoPkg) + + var phpNamespace *string + + msgAnnot, err := extractMessageAnnotation(genType) + if err == nil && msgAnnot.PhpNamespace != "" { + phpNamespace = &msgAnnot.PhpNamespace + } + protoPackages[protoPkg] = &descriptorpb.FileDescriptorProto{ Name: relFileName(protoPkg), Package: &protoPkg, Syntax: strptr("proto3"), Options: &descriptorpb.FileOptions{ - GoPackage: &goPkg, + GoPackage: &goPkg, + PhpNamespace: phpNamespace, }, } } + + // Extract and set PhpNamespace if provided (even for existing packages) + msgAnnot, err := extractMessageAnnotation(genType) + if err == nil && msgAnnot.PhpNamespace != "" { + fd := protoPackages[protoPkg] + if fd.Options == nil { + fd.Options = &descriptorpb.FileOptions{} + } + fd.Options.PhpNamespace = &msgAnnot.PhpNamespace + } + fd := protoPackages[protoPkg] fd.MessageType = append(fd.MessageType, messageDescriptor) a.schemaProtoFiles[genType.Name] = *fd.Name diff --git a/entproto/internal/entprototest/adapter_test.go b/entproto/internal/entprototest/adapter_test.go index 6e91d2286..0c6e76743 100644 --- a/entproto/internal/entprototest/adapter_test.go +++ b/entproto/internal/entprototest/adapter_test.go @@ -290,3 +290,9 @@ func (suite *AdapterTestSuite) TestOptionals() { suite.Require().EqualValues(descriptorpb.FieldDescriptorProto_TYPE_MESSAGE, bytesField.GetType()) suite.Require().EqualValues("BytesValue", uuidField.GetMessageType().GetName()) } + +func (suite *AdapterTestSuite) TestMessageWithPhpNamespace() { + fd, err := suite.adapter.GetFileDescriptor("MessageWithPhpNamespace") + suite.Require().NoError(err) + suite.Equal("My\\Company\\Todo", fd.GetFileOptions().GetPhpNamespace()) +} diff --git a/entproto/internal/entprototest/ent/client.go b/entproto/internal/entprototest/ent/client.go index 61b24d983..cc1b9421c 100644 --- a/entproto/internal/entprototest/ent/client.go +++ b/entproto/internal/entprototest/ent/client.go @@ -29,6 +29,7 @@ import ( "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithints" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithoptionals" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithpackagename" + "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithphpnamespace" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithstrings" "entgo.io/contrib/entproto/internal/entprototest/ent/nobackref" "entgo.io/contrib/entproto/internal/entprototest/ent/onemethodservice" @@ -79,6 +80,8 @@ type Client struct { MessageWithOptionals *MessageWithOptionalsClient // MessageWithPackageName is the client for interacting with the MessageWithPackageName builders. MessageWithPackageName *MessageWithPackageNameClient + // MessageWithPhpNamespace is the client for interacting with the MessageWithPhpNamespace builders. + MessageWithPhpNamespace *MessageWithPhpNamespaceClient // MessageWithStrings is the client for interacting with the MessageWithStrings builders. MessageWithStrings *MessageWithStringsClient // NoBackref is the client for interacting with the NoBackref builders. @@ -122,6 +125,7 @@ func (c *Client) init() { c.MessageWithInts = NewMessageWithIntsClient(c.config) c.MessageWithOptionals = NewMessageWithOptionalsClient(c.config) c.MessageWithPackageName = NewMessageWithPackageNameClient(c.config) + c.MessageWithPhpNamespace = NewMessageWithPhpNamespaceClient(c.config) c.MessageWithStrings = NewMessageWithStringsClient(c.config) c.NoBackref = NewNoBackrefClient(c.config) c.OneMethodService = NewOneMethodServiceClient(c.config) @@ -238,6 +242,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { MessageWithInts: NewMessageWithIntsClient(cfg), MessageWithOptionals: NewMessageWithOptionalsClient(cfg), MessageWithPackageName: NewMessageWithPackageNameClient(cfg), + MessageWithPhpNamespace: NewMessageWithPhpNamespaceClient(cfg), MessageWithStrings: NewMessageWithStringsClient(cfg), NoBackref: NewNoBackrefClient(cfg), OneMethodService: NewOneMethodServiceClient(cfg), @@ -281,6 +286,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) MessageWithInts: NewMessageWithIntsClient(cfg), MessageWithOptionals: NewMessageWithOptionalsClient(cfg), MessageWithPackageName: NewMessageWithPackageNameClient(cfg), + MessageWithPhpNamespace: NewMessageWithPhpNamespaceClient(cfg), MessageWithStrings: NewMessageWithStringsClient(cfg), NoBackref: NewNoBackrefClient(cfg), OneMethodService: NewOneMethodServiceClient(cfg), @@ -322,9 +328,9 @@ func (c *Client) Use(hooks ...Hook) { c.DuplicateNumberMessage, c.EnumWithConflictingValue, c.ExplicitSkippedMessage, c.Image, c.ImplicitSkippedMessage, c.InvalidFieldMessage, c.MessageWithEnum, c.MessageWithFieldOne, c.MessageWithID, c.MessageWithInts, - c.MessageWithOptionals, c.MessageWithPackageName, c.MessageWithStrings, - c.NoBackref, c.OneMethodService, c.Portal, c.SkipEdgeExample, - c.TwoMethodService, c.User, c.ValidMessage, + c.MessageWithOptionals, c.MessageWithPackageName, c.MessageWithPhpNamespace, + c.MessageWithStrings, c.NoBackref, c.OneMethodService, c.Portal, + c.SkipEdgeExample, c.TwoMethodService, c.User, c.ValidMessage, } { n.Use(hooks...) } @@ -338,9 +344,9 @@ func (c *Client) Intercept(interceptors ...Interceptor) { c.DuplicateNumberMessage, c.EnumWithConflictingValue, c.ExplicitSkippedMessage, c.Image, c.ImplicitSkippedMessage, c.InvalidFieldMessage, c.MessageWithEnum, c.MessageWithFieldOne, c.MessageWithID, c.MessageWithInts, - c.MessageWithOptionals, c.MessageWithPackageName, c.MessageWithStrings, - c.NoBackref, c.OneMethodService, c.Portal, c.SkipEdgeExample, - c.TwoMethodService, c.User, c.ValidMessage, + c.MessageWithOptionals, c.MessageWithPackageName, c.MessageWithPhpNamespace, + c.MessageWithStrings, c.NoBackref, c.OneMethodService, c.Portal, + c.SkipEdgeExample, c.TwoMethodService, c.User, c.ValidMessage, } { n.Intercept(interceptors...) } @@ -381,6 +387,8 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) { return c.MessageWithOptionals.mutate(ctx, m) case *MessageWithPackageNameMutation: return c.MessageWithPackageName.mutate(ctx, m) + case *MessageWithPhpNamespaceMutation: + return c.MessageWithPhpNamespace.mutate(ctx, m) case *MessageWithStringsMutation: return c.MessageWithStrings.mutate(ctx, m) case *NoBackrefMutation: @@ -2610,6 +2618,139 @@ func (c *MessageWithPackageNameClient) mutate(ctx context.Context, m *MessageWit } } +// MessageWithPhpNamespaceClient is a client for the MessageWithPhpNamespace schema. +type MessageWithPhpNamespaceClient struct { + config +} + +// NewMessageWithPhpNamespaceClient returns a client for the MessageWithPhpNamespace from the given config. +func NewMessageWithPhpNamespaceClient(c config) *MessageWithPhpNamespaceClient { + return &MessageWithPhpNamespaceClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `messagewithphpnamespace.Hooks(f(g(h())))`. +func (c *MessageWithPhpNamespaceClient) Use(hooks ...Hook) { + c.hooks.MessageWithPhpNamespace = append(c.hooks.MessageWithPhpNamespace, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `messagewithphpnamespace.Intercept(f(g(h())))`. +func (c *MessageWithPhpNamespaceClient) Intercept(interceptors ...Interceptor) { + c.inters.MessageWithPhpNamespace = append(c.inters.MessageWithPhpNamespace, interceptors...) +} + +// Create returns a builder for creating a MessageWithPhpNamespace entity. +func (c *MessageWithPhpNamespaceClient) Create() *MessageWithPhpNamespaceCreate { + mutation := newMessageWithPhpNamespaceMutation(c.config, OpCreate) + return &MessageWithPhpNamespaceCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of MessageWithPhpNamespace entities. +func (c *MessageWithPhpNamespaceClient) CreateBulk(builders ...*MessageWithPhpNamespaceCreate) *MessageWithPhpNamespaceCreateBulk { + return &MessageWithPhpNamespaceCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *MessageWithPhpNamespaceClient) MapCreateBulk(slice any, setFunc func(*MessageWithPhpNamespaceCreate, int)) *MessageWithPhpNamespaceCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &MessageWithPhpNamespaceCreateBulk{err: fmt.Errorf("calling to MessageWithPhpNamespaceClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*MessageWithPhpNamespaceCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &MessageWithPhpNamespaceCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for MessageWithPhpNamespace. +func (c *MessageWithPhpNamespaceClient) Update() *MessageWithPhpNamespaceUpdate { + mutation := newMessageWithPhpNamespaceMutation(c.config, OpUpdate) + return &MessageWithPhpNamespaceUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *MessageWithPhpNamespaceClient) UpdateOne(mwpn *MessageWithPhpNamespace) *MessageWithPhpNamespaceUpdateOne { + mutation := newMessageWithPhpNamespaceMutation(c.config, OpUpdateOne, withMessageWithPhpNamespace(mwpn)) + return &MessageWithPhpNamespaceUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *MessageWithPhpNamespaceClient) UpdateOneID(id int) *MessageWithPhpNamespaceUpdateOne { + mutation := newMessageWithPhpNamespaceMutation(c.config, OpUpdateOne, withMessageWithPhpNamespaceID(id)) + return &MessageWithPhpNamespaceUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for MessageWithPhpNamespace. +func (c *MessageWithPhpNamespaceClient) Delete() *MessageWithPhpNamespaceDelete { + mutation := newMessageWithPhpNamespaceMutation(c.config, OpDelete) + return &MessageWithPhpNamespaceDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *MessageWithPhpNamespaceClient) DeleteOne(mwpn *MessageWithPhpNamespace) *MessageWithPhpNamespaceDeleteOne { + return c.DeleteOneID(mwpn.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *MessageWithPhpNamespaceClient) DeleteOneID(id int) *MessageWithPhpNamespaceDeleteOne { + builder := c.Delete().Where(messagewithphpnamespace.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &MessageWithPhpNamespaceDeleteOne{builder} +} + +// Query returns a query builder for MessageWithPhpNamespace. +func (c *MessageWithPhpNamespaceClient) Query() *MessageWithPhpNamespaceQuery { + return &MessageWithPhpNamespaceQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeMessageWithPhpNamespace}, + inters: c.Interceptors(), + } +} + +// Get returns a MessageWithPhpNamespace entity by its id. +func (c *MessageWithPhpNamespaceClient) Get(ctx context.Context, id int) (*MessageWithPhpNamespace, error) { + return c.Query().Where(messagewithphpnamespace.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *MessageWithPhpNamespaceClient) GetX(ctx context.Context, id int) *MessageWithPhpNamespace { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// Hooks returns the client hooks. +func (c *MessageWithPhpNamespaceClient) Hooks() []Hook { + return c.hooks.MessageWithPhpNamespace +} + +// Interceptors returns the client interceptors. +func (c *MessageWithPhpNamespaceClient) Interceptors() []Interceptor { + return c.inters.MessageWithPhpNamespace +} + +func (c *MessageWithPhpNamespaceClient) mutate(ctx context.Context, m *MessageWithPhpNamespaceMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&MessageWithPhpNamespaceCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&MessageWithPhpNamespaceUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&MessageWithPhpNamespaceUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&MessageWithPhpNamespaceDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown MessageWithPhpNamespace mutation op: %q", m.Op()) + } +} + // MessageWithStringsClient is a client for the MessageWithStrings schema. type MessageWithStringsClient struct { config @@ -3777,15 +3918,17 @@ type ( EnumWithConflictingValue, ExplicitSkippedMessage, Image, ImplicitSkippedMessage, InvalidFieldMessage, MessageWithEnum, MessageWithFieldOne, MessageWithID, MessageWithInts, MessageWithOptionals, - MessageWithPackageName, MessageWithStrings, NoBackref, OneMethodService, - Portal, SkipEdgeExample, TwoMethodService, User, ValidMessage []ent.Hook + MessageWithPackageName, MessageWithPhpNamespace, MessageWithStrings, NoBackref, + OneMethodService, Portal, SkipEdgeExample, TwoMethodService, User, + ValidMessage []ent.Hook } inters struct { AllMethodsService, BlogPost, Category, DependsOnSkipped, DuplicateNumberMessage, EnumWithConflictingValue, ExplicitSkippedMessage, Image, ImplicitSkippedMessage, InvalidFieldMessage, MessageWithEnum, MessageWithFieldOne, MessageWithID, MessageWithInts, MessageWithOptionals, - MessageWithPackageName, MessageWithStrings, NoBackref, OneMethodService, - Portal, SkipEdgeExample, TwoMethodService, User, ValidMessage []ent.Interceptor + MessageWithPackageName, MessageWithPhpNamespace, MessageWithStrings, NoBackref, + OneMethodService, Portal, SkipEdgeExample, TwoMethodService, User, + ValidMessage []ent.Interceptor } ) diff --git a/entproto/internal/entprototest/ent/ent.go b/entproto/internal/entprototest/ent/ent.go index f6118d604..0c47d20c5 100644 --- a/entproto/internal/entprototest/ent/ent.go +++ b/entproto/internal/entprototest/ent/ent.go @@ -25,6 +25,7 @@ import ( "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithints" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithoptionals" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithpackagename" + "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithphpnamespace" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithstrings" "entgo.io/contrib/entproto/internal/entprototest/ent/nobackref" "entgo.io/contrib/entproto/internal/entprototest/ent/onemethodservice" @@ -112,6 +113,7 @@ func checkColumn(table, column string) error { messagewithints.Table: messagewithints.ValidColumn, messagewithoptionals.Table: messagewithoptionals.ValidColumn, messagewithpackagename.Table: messagewithpackagename.ValidColumn, + messagewithphpnamespace.Table: messagewithphpnamespace.ValidColumn, messagewithstrings.Table: messagewithstrings.ValidColumn, nobackref.Table: nobackref.ValidColumn, onemethodservice.Table: onemethodservice.ValidColumn, diff --git a/entproto/internal/entprototest/ent/hook/hook.go b/entproto/internal/entprototest/ent/hook/hook.go index 4493bcf35..f73750710 100644 --- a/entproto/internal/entprototest/ent/hook/hook.go +++ b/entproto/internal/entprototest/ent/hook/hook.go @@ -201,6 +201,18 @@ func (f MessageWithPackageNameFunc) Mutate(ctx context.Context, m ent.Mutation) return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MessageWithPackageNameMutation", m) } +// The MessageWithPhpNamespaceFunc type is an adapter to allow the use of ordinary +// function as MessageWithPhpNamespace mutator. +type MessageWithPhpNamespaceFunc func(context.Context, *ent.MessageWithPhpNamespaceMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f MessageWithPhpNamespaceFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.MessageWithPhpNamespaceMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MessageWithPhpNamespaceMutation", m) +} + // The MessageWithStringsFunc type is an adapter to allow the use of ordinary // function as MessageWithStrings mutator. type MessageWithStringsFunc func(context.Context, *ent.MessageWithStringsMutation) (ent.Value, error) diff --git a/entproto/internal/entprototest/ent/messagewithphpnamespace.go b/entproto/internal/entprototest/ent/messagewithphpnamespace.go new file mode 100644 index 000000000..20196d0e5 --- /dev/null +++ b/entproto/internal/entprototest/ent/messagewithphpnamespace.go @@ -0,0 +1,103 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "fmt" + "strings" + + "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithphpnamespace" + "entgo.io/ent" + "entgo.io/ent/dialect/sql" +) + +// MessageWithPhpNamespace is the model entity for the MessageWithPhpNamespace schema. +type MessageWithPhpNamespace struct { + config `json:"-"` + // ID of the ent. + ID int `json:"id,omitempty"` + // Name holds the value of the "name" field. + Name string `json:"name,omitempty"` + selectValues sql.SelectValues +} + +// scanValues returns the types for scanning values from sql.Rows. +func (*MessageWithPhpNamespace) scanValues(columns []string) ([]any, error) { + values := make([]any, len(columns)) + for i := range columns { + switch columns[i] { + case messagewithphpnamespace.FieldID: + values[i] = new(sql.NullInt64) + case messagewithphpnamespace.FieldName: + values[i] = new(sql.NullString) + default: + values[i] = new(sql.UnknownType) + } + } + return values, nil +} + +// assignValues assigns the values that were returned from sql.Rows (after scanning) +// to the MessageWithPhpNamespace fields. +func (mwpn *MessageWithPhpNamespace) assignValues(columns []string, values []any) error { + if m, n := len(values), len(columns); m < n { + return fmt.Errorf("mismatch number of scan values: %d != %d", m, n) + } + for i := range columns { + switch columns[i] { + case messagewithphpnamespace.FieldID: + value, ok := values[i].(*sql.NullInt64) + if !ok { + return fmt.Errorf("unexpected type %T for field id", value) + } + mwpn.ID = int(value.Int64) + case messagewithphpnamespace.FieldName: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field name", values[i]) + } else if value.Valid { + mwpn.Name = value.String + } + default: + mwpn.selectValues.Set(columns[i], values[i]) + } + } + return nil +} + +// Value returns the ent.Value that was dynamically selected and assigned to the MessageWithPhpNamespace. +// This includes values selected through modifiers, order, etc. +func (mwpn *MessageWithPhpNamespace) Value(name string) (ent.Value, error) { + return mwpn.selectValues.Get(name) +} + +// Update returns a builder for updating this MessageWithPhpNamespace. +// Note that you need to call MessageWithPhpNamespace.Unwrap() before calling this method if this MessageWithPhpNamespace +// was returned from a transaction, and the transaction was committed or rolled back. +func (mwpn *MessageWithPhpNamespace) Update() *MessageWithPhpNamespaceUpdateOne { + return NewMessageWithPhpNamespaceClient(mwpn.config).UpdateOne(mwpn) +} + +// Unwrap unwraps the MessageWithPhpNamespace entity that was returned from a transaction after it was closed, +// so that all future queries will be executed through the driver which created the transaction. +func (mwpn *MessageWithPhpNamespace) Unwrap() *MessageWithPhpNamespace { + _tx, ok := mwpn.config.driver.(*txDriver) + if !ok { + panic("ent: MessageWithPhpNamespace is not a transactional entity") + } + mwpn.config.driver = _tx.drv + return mwpn +} + +// String implements the fmt.Stringer. +func (mwpn *MessageWithPhpNamespace) String() string { + var builder strings.Builder + builder.WriteString("MessageWithPhpNamespace(") + builder.WriteString(fmt.Sprintf("id=%v, ", mwpn.ID)) + builder.WriteString("name=") + builder.WriteString(mwpn.Name) + builder.WriteByte(')') + return builder.String() +} + +// MessageWithPhpNamespaces is a parsable slice of MessageWithPhpNamespace. +type MessageWithPhpNamespaces []*MessageWithPhpNamespace diff --git a/entproto/internal/entprototest/ent/messagewithphpnamespace/messagewithphpnamespace.go b/entproto/internal/entprototest/ent/messagewithphpnamespace/messagewithphpnamespace.go new file mode 100644 index 000000000..12a017163 --- /dev/null +++ b/entproto/internal/entprototest/ent/messagewithphpnamespace/messagewithphpnamespace.go @@ -0,0 +1,47 @@ +// Code generated by ent, DO NOT EDIT. + +package messagewithphpnamespace + +import ( + "entgo.io/ent/dialect/sql" +) + +const ( + // Label holds the string label denoting the messagewithphpnamespace type in the database. + Label = "message_with_php_namespace" + // FieldID holds the string denoting the id field in the database. + FieldID = "id" + // FieldName holds the string denoting the name field in the database. + FieldName = "name" + // Table holds the table name of the messagewithphpnamespace in the database. + Table = "message_with_php_namespaces" +) + +// Columns holds all SQL columns for messagewithphpnamespace fields. +var Columns = []string{ + FieldID, + FieldName, +} + +// ValidColumn reports if the column name is valid (part of the table columns). +func ValidColumn(column string) bool { + for i := range Columns { + if column == Columns[i] { + return true + } + } + return false +} + +// OrderOption defines the ordering options for the MessageWithPhpNamespace queries. +type OrderOption func(*sql.Selector) + +// ByID orders the results by the id field. +func ByID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldID, opts...).ToFunc() +} + +// ByName orders the results by the name field. +func ByName(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldName, opts...).ToFunc() +} diff --git a/entproto/internal/entprototest/ent/messagewithphpnamespace/where.go b/entproto/internal/entprototest/ent/messagewithphpnamespace/where.go new file mode 100644 index 000000000..1d6d37ec0 --- /dev/null +++ b/entproto/internal/entprototest/ent/messagewithphpnamespace/where.go @@ -0,0 +1,138 @@ +// Code generated by ent, DO NOT EDIT. + +package messagewithphpnamespace + +import ( + "entgo.io/contrib/entproto/internal/entprototest/ent/predicate" + "entgo.io/ent/dialect/sql" +) + +// ID filters vertices based on their ID field. +func ID(id int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldEQ(FieldID, id)) +} + +// IDEQ applies the EQ predicate on the ID field. +func IDEQ(id int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldEQ(FieldID, id)) +} + +// IDNEQ applies the NEQ predicate on the ID field. +func IDNEQ(id int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldNEQ(FieldID, id)) +} + +// IDIn applies the In predicate on the ID field. +func IDIn(ids ...int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldIn(FieldID, ids...)) +} + +// IDNotIn applies the NotIn predicate on the ID field. +func IDNotIn(ids ...int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldNotIn(FieldID, ids...)) +} + +// IDGT applies the GT predicate on the ID field. +func IDGT(id int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldGT(FieldID, id)) +} + +// IDGTE applies the GTE predicate on the ID field. +func IDGTE(id int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldGTE(FieldID, id)) +} + +// IDLT applies the LT predicate on the ID field. +func IDLT(id int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldLT(FieldID, id)) +} + +// IDLTE applies the LTE predicate on the ID field. +func IDLTE(id int) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldLTE(FieldID, id)) +} + +// Name applies equality check predicate on the "name" field. It's identical to NameEQ. +func Name(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldEQ(FieldName, v)) +} + +// NameEQ applies the EQ predicate on the "name" field. +func NameEQ(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldEQ(FieldName, v)) +} + +// NameNEQ applies the NEQ predicate on the "name" field. +func NameNEQ(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldNEQ(FieldName, v)) +} + +// NameIn applies the In predicate on the "name" field. +func NameIn(vs ...string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldIn(FieldName, vs...)) +} + +// NameNotIn applies the NotIn predicate on the "name" field. +func NameNotIn(vs ...string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldNotIn(FieldName, vs...)) +} + +// NameGT applies the GT predicate on the "name" field. +func NameGT(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldGT(FieldName, v)) +} + +// NameGTE applies the GTE predicate on the "name" field. +func NameGTE(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldGTE(FieldName, v)) +} + +// NameLT applies the LT predicate on the "name" field. +func NameLT(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldLT(FieldName, v)) +} + +// NameLTE applies the LTE predicate on the "name" field. +func NameLTE(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldLTE(FieldName, v)) +} + +// NameContains applies the Contains predicate on the "name" field. +func NameContains(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldContains(FieldName, v)) +} + +// NameHasPrefix applies the HasPrefix predicate on the "name" field. +func NameHasPrefix(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldHasPrefix(FieldName, v)) +} + +// NameHasSuffix applies the HasSuffix predicate on the "name" field. +func NameHasSuffix(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldHasSuffix(FieldName, v)) +} + +// NameEqualFold applies the EqualFold predicate on the "name" field. +func NameEqualFold(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldEqualFold(FieldName, v)) +} + +// NameContainsFold applies the ContainsFold predicate on the "name" field. +func NameContainsFold(v string) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.FieldContainsFold(FieldName, v)) +} + +// And groups predicates with the AND operator between them. +func And(predicates ...predicate.MessageWithPhpNamespace) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.AndPredicates(predicates...)) +} + +// Or groups predicates with the OR operator between them. +func Or(predicates ...predicate.MessageWithPhpNamespace) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.OrPredicates(predicates...)) +} + +// Not applies the not operator on the given predicate. +func Not(p predicate.MessageWithPhpNamespace) predicate.MessageWithPhpNamespace { + return predicate.MessageWithPhpNamespace(sql.NotPredicates(p)) +} diff --git a/entproto/internal/entprototest/ent/messagewithphpnamespace_create.go b/entproto/internal/entprototest/ent/messagewithphpnamespace_create.go new file mode 100644 index 000000000..fdd62c3a9 --- /dev/null +++ b/entproto/internal/entprototest/ent/messagewithphpnamespace_create.go @@ -0,0 +1,183 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithphpnamespace" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" +) + +// MessageWithPhpNamespaceCreate is the builder for creating a MessageWithPhpNamespace entity. +type MessageWithPhpNamespaceCreate struct { + config + mutation *MessageWithPhpNamespaceMutation + hooks []Hook +} + +// SetName sets the "name" field. +func (mwpnc *MessageWithPhpNamespaceCreate) SetName(s string) *MessageWithPhpNamespaceCreate { + mwpnc.mutation.SetName(s) + return mwpnc +} + +// Mutation returns the MessageWithPhpNamespaceMutation object of the builder. +func (mwpnc *MessageWithPhpNamespaceCreate) Mutation() *MessageWithPhpNamespaceMutation { + return mwpnc.mutation +} + +// Save creates the MessageWithPhpNamespace in the database. +func (mwpnc *MessageWithPhpNamespaceCreate) Save(ctx context.Context) (*MessageWithPhpNamespace, error) { + return withHooks(ctx, mwpnc.sqlSave, mwpnc.mutation, mwpnc.hooks) +} + +// SaveX calls Save and panics if Save returns an error. +func (mwpnc *MessageWithPhpNamespaceCreate) SaveX(ctx context.Context) *MessageWithPhpNamespace { + v, err := mwpnc.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (mwpnc *MessageWithPhpNamespaceCreate) Exec(ctx context.Context) error { + _, err := mwpnc.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (mwpnc *MessageWithPhpNamespaceCreate) ExecX(ctx context.Context) { + if err := mwpnc.Exec(ctx); err != nil { + panic(err) + } +} + +// check runs all checks and user-defined validators on the builder. +func (mwpnc *MessageWithPhpNamespaceCreate) check() error { + if _, ok := mwpnc.mutation.Name(); !ok { + return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "MessageWithPhpNamespace.name"`)} + } + return nil +} + +func (mwpnc *MessageWithPhpNamespaceCreate) sqlSave(ctx context.Context) (*MessageWithPhpNamespace, error) { + if err := mwpnc.check(); err != nil { + return nil, err + } + _node, _spec := mwpnc.createSpec() + if err := sqlgraph.CreateNode(ctx, mwpnc.driver, _spec); err != nil { + if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + id := _spec.ID.Value.(int64) + _node.ID = int(id) + mwpnc.mutation.id = &_node.ID + mwpnc.mutation.done = true + return _node, nil +} + +func (mwpnc *MessageWithPhpNamespaceCreate) createSpec() (*MessageWithPhpNamespace, *sqlgraph.CreateSpec) { + var ( + _node = &MessageWithPhpNamespace{config: mwpnc.config} + _spec = sqlgraph.NewCreateSpec(messagewithphpnamespace.Table, sqlgraph.NewFieldSpec(messagewithphpnamespace.FieldID, field.TypeInt)) + ) + if value, ok := mwpnc.mutation.Name(); ok { + _spec.SetField(messagewithphpnamespace.FieldName, field.TypeString, value) + _node.Name = value + } + return _node, _spec +} + +// MessageWithPhpNamespaceCreateBulk is the builder for creating many MessageWithPhpNamespace entities in bulk. +type MessageWithPhpNamespaceCreateBulk struct { + config + err error + builders []*MessageWithPhpNamespaceCreate +} + +// Save creates the MessageWithPhpNamespace entities in the database. +func (mwpncb *MessageWithPhpNamespaceCreateBulk) Save(ctx context.Context) ([]*MessageWithPhpNamespace, error) { + if mwpncb.err != nil { + return nil, mwpncb.err + } + specs := make([]*sqlgraph.CreateSpec, len(mwpncb.builders)) + nodes := make([]*MessageWithPhpNamespace, len(mwpncb.builders)) + mutators := make([]Mutator, len(mwpncb.builders)) + for i := range mwpncb.builders { + func(i int, root context.Context) { + builder := mwpncb.builders[i] + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutation, ok := m.(*MessageWithPhpNamespaceMutation) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + if err := builder.check(); err != nil { + return nil, err + } + builder.mutation = mutation + var err error + nodes[i], specs[i] = builder.createSpec() + if i < len(mutators)-1 { + _, err = mutators[i+1].Mutate(root, mwpncb.builders[i+1].mutation) + } else { + spec := &sqlgraph.BatchCreateSpec{Nodes: specs} + // Invoke the actual operation on the latest mutation in the chain. + if err = sqlgraph.BatchCreate(ctx, mwpncb.driver, spec); err != nil { + if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + } + } + if err != nil { + return nil, err + } + mutation.id = &nodes[i].ID + if specs[i].ID.Value != nil { + id := specs[i].ID.Value.(int64) + nodes[i].ID = int(id) + } + mutation.done = true + return nodes[i], nil + }) + for i := len(builder.hooks) - 1; i >= 0; i-- { + mut = builder.hooks[i](mut) + } + mutators[i] = mut + }(i, ctx) + } + if len(mutators) > 0 { + if _, err := mutators[0].Mutate(ctx, mwpncb.builders[0].mutation); err != nil { + return nil, err + } + } + return nodes, nil +} + +// SaveX is like Save, but panics if an error occurs. +func (mwpncb *MessageWithPhpNamespaceCreateBulk) SaveX(ctx context.Context) []*MessageWithPhpNamespace { + v, err := mwpncb.Save(ctx) + if err != nil { + panic(err) + } + return v +} + +// Exec executes the query. +func (mwpncb *MessageWithPhpNamespaceCreateBulk) Exec(ctx context.Context) error { + _, err := mwpncb.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (mwpncb *MessageWithPhpNamespaceCreateBulk) ExecX(ctx context.Context) { + if err := mwpncb.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/entproto/internal/entprototest/ent/messagewithphpnamespace_delete.go b/entproto/internal/entprototest/ent/messagewithphpnamespace_delete.go new file mode 100644 index 000000000..94c4d5036 --- /dev/null +++ b/entproto/internal/entprototest/ent/messagewithphpnamespace_delete.go @@ -0,0 +1,88 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + + "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithphpnamespace" + "entgo.io/contrib/entproto/internal/entprototest/ent/predicate" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" +) + +// MessageWithPhpNamespaceDelete is the builder for deleting a MessageWithPhpNamespace entity. +type MessageWithPhpNamespaceDelete struct { + config + hooks []Hook + mutation *MessageWithPhpNamespaceMutation +} + +// Where appends a list predicates to the MessageWithPhpNamespaceDelete builder. +func (mwpnd *MessageWithPhpNamespaceDelete) Where(ps ...predicate.MessageWithPhpNamespace) *MessageWithPhpNamespaceDelete { + mwpnd.mutation.Where(ps...) + return mwpnd +} + +// Exec executes the deletion query and returns how many vertices were deleted. +func (mwpnd *MessageWithPhpNamespaceDelete) Exec(ctx context.Context) (int, error) { + return withHooks(ctx, mwpnd.sqlExec, mwpnd.mutation, mwpnd.hooks) +} + +// ExecX is like Exec, but panics if an error occurs. +func (mwpnd *MessageWithPhpNamespaceDelete) ExecX(ctx context.Context) int { + n, err := mwpnd.Exec(ctx) + if err != nil { + panic(err) + } + return n +} + +func (mwpnd *MessageWithPhpNamespaceDelete) sqlExec(ctx context.Context) (int, error) { + _spec := sqlgraph.NewDeleteSpec(messagewithphpnamespace.Table, sqlgraph.NewFieldSpec(messagewithphpnamespace.FieldID, field.TypeInt)) + if ps := mwpnd.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + affected, err := sqlgraph.DeleteNodes(ctx, mwpnd.driver, _spec) + if err != nil && sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + mwpnd.mutation.done = true + return affected, err +} + +// MessageWithPhpNamespaceDeleteOne is the builder for deleting a single MessageWithPhpNamespace entity. +type MessageWithPhpNamespaceDeleteOne struct { + mwpnd *MessageWithPhpNamespaceDelete +} + +// Where appends a list predicates to the MessageWithPhpNamespaceDelete builder. +func (mwpndo *MessageWithPhpNamespaceDeleteOne) Where(ps ...predicate.MessageWithPhpNamespace) *MessageWithPhpNamespaceDeleteOne { + mwpndo.mwpnd.mutation.Where(ps...) + return mwpndo +} + +// Exec executes the deletion query. +func (mwpndo *MessageWithPhpNamespaceDeleteOne) Exec(ctx context.Context) error { + n, err := mwpndo.mwpnd.Exec(ctx) + switch { + case err != nil: + return err + case n == 0: + return &NotFoundError{messagewithphpnamespace.Label} + default: + return nil + } +} + +// ExecX is like Exec, but panics if an error occurs. +func (mwpndo *MessageWithPhpNamespaceDeleteOne) ExecX(ctx context.Context) { + if err := mwpndo.Exec(ctx); err != nil { + panic(err) + } +} diff --git a/entproto/internal/entprototest/ent/messagewithphpnamespace_query.go b/entproto/internal/entprototest/ent/messagewithphpnamespace_query.go new file mode 100644 index 000000000..227da348f --- /dev/null +++ b/entproto/internal/entprototest/ent/messagewithphpnamespace_query.go @@ -0,0 +1,527 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "fmt" + "math" + + "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithphpnamespace" + "entgo.io/contrib/entproto/internal/entprototest/ent/predicate" + "entgo.io/ent" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" +) + +// MessageWithPhpNamespaceQuery is the builder for querying MessageWithPhpNamespace entities. +type MessageWithPhpNamespaceQuery struct { + config + ctx *QueryContext + order []messagewithphpnamespace.OrderOption + inters []Interceptor + predicates []predicate.MessageWithPhpNamespace + // intermediate query (i.e. traversal path). + sql *sql.Selector + path func(context.Context) (*sql.Selector, error) +} + +// Where adds a new predicate for the MessageWithPhpNamespaceQuery builder. +func (mwpnq *MessageWithPhpNamespaceQuery) Where(ps ...predicate.MessageWithPhpNamespace) *MessageWithPhpNamespaceQuery { + mwpnq.predicates = append(mwpnq.predicates, ps...) + return mwpnq +} + +// Limit the number of records to be returned by this query. +func (mwpnq *MessageWithPhpNamespaceQuery) Limit(limit int) *MessageWithPhpNamespaceQuery { + mwpnq.ctx.Limit = &limit + return mwpnq +} + +// Offset to start from. +func (mwpnq *MessageWithPhpNamespaceQuery) Offset(offset int) *MessageWithPhpNamespaceQuery { + mwpnq.ctx.Offset = &offset + return mwpnq +} + +// Unique configures the query builder to filter duplicate records on query. +// By default, unique is set to true, and can be disabled using this method. +func (mwpnq *MessageWithPhpNamespaceQuery) Unique(unique bool) *MessageWithPhpNamespaceQuery { + mwpnq.ctx.Unique = &unique + return mwpnq +} + +// Order specifies how the records should be ordered. +func (mwpnq *MessageWithPhpNamespaceQuery) Order(o ...messagewithphpnamespace.OrderOption) *MessageWithPhpNamespaceQuery { + mwpnq.order = append(mwpnq.order, o...) + return mwpnq +} + +// First returns the first MessageWithPhpNamespace entity from the query. +// Returns a *NotFoundError when no MessageWithPhpNamespace was found. +func (mwpnq *MessageWithPhpNamespaceQuery) First(ctx context.Context) (*MessageWithPhpNamespace, error) { + nodes, err := mwpnq.Limit(1).All(setContextOp(ctx, mwpnq.ctx, ent.OpQueryFirst)) + if err != nil { + return nil, err + } + if len(nodes) == 0 { + return nil, &NotFoundError{messagewithphpnamespace.Label} + } + return nodes[0], nil +} + +// FirstX is like First, but panics if an error occurs. +func (mwpnq *MessageWithPhpNamespaceQuery) FirstX(ctx context.Context) *MessageWithPhpNamespace { + node, err := mwpnq.First(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return node +} + +// FirstID returns the first MessageWithPhpNamespace ID from the query. +// Returns a *NotFoundError when no MessageWithPhpNamespace ID was found. +func (mwpnq *MessageWithPhpNamespaceQuery) FirstID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = mwpnq.Limit(1).IDs(setContextOp(ctx, mwpnq.ctx, ent.OpQueryFirstID)); err != nil { + return + } + if len(ids) == 0 { + err = &NotFoundError{messagewithphpnamespace.Label} + return + } + return ids[0], nil +} + +// FirstIDX is like FirstID, but panics if an error occurs. +func (mwpnq *MessageWithPhpNamespaceQuery) FirstIDX(ctx context.Context) int { + id, err := mwpnq.FirstID(ctx) + if err != nil && !IsNotFound(err) { + panic(err) + } + return id +} + +// Only returns a single MessageWithPhpNamespace entity found by the query, ensuring it only returns one. +// Returns a *NotSingularError when more than one MessageWithPhpNamespace entity is found. +// Returns a *NotFoundError when no MessageWithPhpNamespace entities are found. +func (mwpnq *MessageWithPhpNamespaceQuery) Only(ctx context.Context) (*MessageWithPhpNamespace, error) { + nodes, err := mwpnq.Limit(2).All(setContextOp(ctx, mwpnq.ctx, ent.OpQueryOnly)) + if err != nil { + return nil, err + } + switch len(nodes) { + case 1: + return nodes[0], nil + case 0: + return nil, &NotFoundError{messagewithphpnamespace.Label} + default: + return nil, &NotSingularError{messagewithphpnamespace.Label} + } +} + +// OnlyX is like Only, but panics if an error occurs. +func (mwpnq *MessageWithPhpNamespaceQuery) OnlyX(ctx context.Context) *MessageWithPhpNamespace { + node, err := mwpnq.Only(ctx) + if err != nil { + panic(err) + } + return node +} + +// OnlyID is like Only, but returns the only MessageWithPhpNamespace ID in the query. +// Returns a *NotSingularError when more than one MessageWithPhpNamespace ID is found. +// Returns a *NotFoundError when no entities are found. +func (mwpnq *MessageWithPhpNamespaceQuery) OnlyID(ctx context.Context) (id int, err error) { + var ids []int + if ids, err = mwpnq.Limit(2).IDs(setContextOp(ctx, mwpnq.ctx, ent.OpQueryOnlyID)); err != nil { + return + } + switch len(ids) { + case 1: + id = ids[0] + case 0: + err = &NotFoundError{messagewithphpnamespace.Label} + default: + err = &NotSingularError{messagewithphpnamespace.Label} + } + return +} + +// OnlyIDX is like OnlyID, but panics if an error occurs. +func (mwpnq *MessageWithPhpNamespaceQuery) OnlyIDX(ctx context.Context) int { + id, err := mwpnq.OnlyID(ctx) + if err != nil { + panic(err) + } + return id +} + +// All executes the query and returns a list of MessageWithPhpNamespaces. +func (mwpnq *MessageWithPhpNamespaceQuery) All(ctx context.Context) ([]*MessageWithPhpNamespace, error) { + ctx = setContextOp(ctx, mwpnq.ctx, ent.OpQueryAll) + if err := mwpnq.prepareQuery(ctx); err != nil { + return nil, err + } + qr := querierAll[[]*MessageWithPhpNamespace, *MessageWithPhpNamespaceQuery]() + return withInterceptors[[]*MessageWithPhpNamespace](ctx, mwpnq, qr, mwpnq.inters) +} + +// AllX is like All, but panics if an error occurs. +func (mwpnq *MessageWithPhpNamespaceQuery) AllX(ctx context.Context) []*MessageWithPhpNamespace { + nodes, err := mwpnq.All(ctx) + if err != nil { + panic(err) + } + return nodes +} + +// IDs executes the query and returns a list of MessageWithPhpNamespace IDs. +func (mwpnq *MessageWithPhpNamespaceQuery) IDs(ctx context.Context) (ids []int, err error) { + if mwpnq.ctx.Unique == nil && mwpnq.path != nil { + mwpnq.Unique(true) + } + ctx = setContextOp(ctx, mwpnq.ctx, ent.OpQueryIDs) + if err = mwpnq.Select(messagewithphpnamespace.FieldID).Scan(ctx, &ids); err != nil { + return nil, err + } + return ids, nil +} + +// IDsX is like IDs, but panics if an error occurs. +func (mwpnq *MessageWithPhpNamespaceQuery) IDsX(ctx context.Context) []int { + ids, err := mwpnq.IDs(ctx) + if err != nil { + panic(err) + } + return ids +} + +// Count returns the count of the given query. +func (mwpnq *MessageWithPhpNamespaceQuery) Count(ctx context.Context) (int, error) { + ctx = setContextOp(ctx, mwpnq.ctx, ent.OpQueryCount) + if err := mwpnq.prepareQuery(ctx); err != nil { + return 0, err + } + return withInterceptors[int](ctx, mwpnq, querierCount[*MessageWithPhpNamespaceQuery](), mwpnq.inters) +} + +// CountX is like Count, but panics if an error occurs. +func (mwpnq *MessageWithPhpNamespaceQuery) CountX(ctx context.Context) int { + count, err := mwpnq.Count(ctx) + if err != nil { + panic(err) + } + return count +} + +// Exist returns true if the query has elements in the graph. +func (mwpnq *MessageWithPhpNamespaceQuery) Exist(ctx context.Context) (bool, error) { + ctx = setContextOp(ctx, mwpnq.ctx, ent.OpQueryExist) + switch _, err := mwpnq.FirstID(ctx); { + case IsNotFound(err): + return false, nil + case err != nil: + return false, fmt.Errorf("ent: check existence: %w", err) + default: + return true, nil + } +} + +// ExistX is like Exist, but panics if an error occurs. +func (mwpnq *MessageWithPhpNamespaceQuery) ExistX(ctx context.Context) bool { + exist, err := mwpnq.Exist(ctx) + if err != nil { + panic(err) + } + return exist +} + +// Clone returns a duplicate of the MessageWithPhpNamespaceQuery builder, including all associated steps. It can be +// used to prepare common query builders and use them differently after the clone is made. +func (mwpnq *MessageWithPhpNamespaceQuery) Clone() *MessageWithPhpNamespaceQuery { + if mwpnq == nil { + return nil + } + return &MessageWithPhpNamespaceQuery{ + config: mwpnq.config, + ctx: mwpnq.ctx.Clone(), + order: append([]messagewithphpnamespace.OrderOption{}, mwpnq.order...), + inters: append([]Interceptor{}, mwpnq.inters...), + predicates: append([]predicate.MessageWithPhpNamespace{}, mwpnq.predicates...), + // clone intermediate query. + sql: mwpnq.sql.Clone(), + path: mwpnq.path, + } +} + +// GroupBy is used to group vertices by one or more fields/columns. +// It is often used with aggregate functions, like: count, max, mean, min, sum. +// +// Example: +// +// var v []struct { +// Name string `json:"name,omitempty"` +// Count int `json:"count,omitempty"` +// } +// +// client.MessageWithPhpNamespace.Query(). +// GroupBy(messagewithphpnamespace.FieldName). +// Aggregate(ent.Count()). +// Scan(ctx, &v) +func (mwpnq *MessageWithPhpNamespaceQuery) GroupBy(field string, fields ...string) *MessageWithPhpNamespaceGroupBy { + mwpnq.ctx.Fields = append([]string{field}, fields...) + grbuild := &MessageWithPhpNamespaceGroupBy{build: mwpnq} + grbuild.flds = &mwpnq.ctx.Fields + grbuild.label = messagewithphpnamespace.Label + grbuild.scan = grbuild.Scan + return grbuild +} + +// Select allows the selection one or more fields/columns for the given query, +// instead of selecting all fields in the entity. +// +// Example: +// +// var v []struct { +// Name string `json:"name,omitempty"` +// } +// +// client.MessageWithPhpNamespace.Query(). +// Select(messagewithphpnamespace.FieldName). +// Scan(ctx, &v) +func (mwpnq *MessageWithPhpNamespaceQuery) Select(fields ...string) *MessageWithPhpNamespaceSelect { + mwpnq.ctx.Fields = append(mwpnq.ctx.Fields, fields...) + sbuild := &MessageWithPhpNamespaceSelect{MessageWithPhpNamespaceQuery: mwpnq} + sbuild.label = messagewithphpnamespace.Label + sbuild.flds, sbuild.scan = &mwpnq.ctx.Fields, sbuild.Scan + return sbuild +} + +// Aggregate returns a MessageWithPhpNamespaceSelect configured with the given aggregations. +func (mwpnq *MessageWithPhpNamespaceQuery) Aggregate(fns ...AggregateFunc) *MessageWithPhpNamespaceSelect { + return mwpnq.Select().Aggregate(fns...) +} + +func (mwpnq *MessageWithPhpNamespaceQuery) prepareQuery(ctx context.Context) error { + for _, inter := range mwpnq.inters { + if inter == nil { + return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)") + } + if trv, ok := inter.(Traverser); ok { + if err := trv.Traverse(ctx, mwpnq); err != nil { + return err + } + } + } + for _, f := range mwpnq.ctx.Fields { + if !messagewithphpnamespace.ValidColumn(f) { + return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + } + if mwpnq.path != nil { + prev, err := mwpnq.path(ctx) + if err != nil { + return err + } + mwpnq.sql = prev + } + return nil +} + +func (mwpnq *MessageWithPhpNamespaceQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*MessageWithPhpNamespace, error) { + var ( + nodes = []*MessageWithPhpNamespace{} + _spec = mwpnq.querySpec() + ) + _spec.ScanValues = func(columns []string) ([]any, error) { + return (*MessageWithPhpNamespace).scanValues(nil, columns) + } + _spec.Assign = func(columns []string, values []any) error { + node := &MessageWithPhpNamespace{config: mwpnq.config} + nodes = append(nodes, node) + return node.assignValues(columns, values) + } + for i := range hooks { + hooks[i](ctx, _spec) + } + if err := sqlgraph.QueryNodes(ctx, mwpnq.driver, _spec); err != nil { + return nil, err + } + if len(nodes) == 0 { + return nodes, nil + } + return nodes, nil +} + +func (mwpnq *MessageWithPhpNamespaceQuery) sqlCount(ctx context.Context) (int, error) { + _spec := mwpnq.querySpec() + _spec.Node.Columns = mwpnq.ctx.Fields + if len(mwpnq.ctx.Fields) > 0 { + _spec.Unique = mwpnq.ctx.Unique != nil && *mwpnq.ctx.Unique + } + return sqlgraph.CountNodes(ctx, mwpnq.driver, _spec) +} + +func (mwpnq *MessageWithPhpNamespaceQuery) querySpec() *sqlgraph.QuerySpec { + _spec := sqlgraph.NewQuerySpec(messagewithphpnamespace.Table, messagewithphpnamespace.Columns, sqlgraph.NewFieldSpec(messagewithphpnamespace.FieldID, field.TypeInt)) + _spec.From = mwpnq.sql + if unique := mwpnq.ctx.Unique; unique != nil { + _spec.Unique = *unique + } else if mwpnq.path != nil { + _spec.Unique = true + } + if fields := mwpnq.ctx.Fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, messagewithphpnamespace.FieldID) + for i := range fields { + if fields[i] != messagewithphpnamespace.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) + } + } + } + if ps := mwpnq.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if limit := mwpnq.ctx.Limit; limit != nil { + _spec.Limit = *limit + } + if offset := mwpnq.ctx.Offset; offset != nil { + _spec.Offset = *offset + } + if ps := mwpnq.order; len(ps) > 0 { + _spec.Order = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + return _spec +} + +func (mwpnq *MessageWithPhpNamespaceQuery) sqlQuery(ctx context.Context) *sql.Selector { + builder := sql.Dialect(mwpnq.driver.Dialect()) + t1 := builder.Table(messagewithphpnamespace.Table) + columns := mwpnq.ctx.Fields + if len(columns) == 0 { + columns = messagewithphpnamespace.Columns + } + selector := builder.Select(t1.Columns(columns...)...).From(t1) + if mwpnq.sql != nil { + selector = mwpnq.sql + selector.Select(selector.Columns(columns...)...) + } + if mwpnq.ctx.Unique != nil && *mwpnq.ctx.Unique { + selector.Distinct() + } + for _, p := range mwpnq.predicates { + p(selector) + } + for _, p := range mwpnq.order { + p(selector) + } + if offset := mwpnq.ctx.Offset; offset != nil { + // limit is mandatory for offset clause. We start + // with default value, and override it below if needed. + selector.Offset(*offset).Limit(math.MaxInt32) + } + if limit := mwpnq.ctx.Limit; limit != nil { + selector.Limit(*limit) + } + return selector +} + +// MessageWithPhpNamespaceGroupBy is the group-by builder for MessageWithPhpNamespace entities. +type MessageWithPhpNamespaceGroupBy struct { + selector + build *MessageWithPhpNamespaceQuery +} + +// Aggregate adds the given aggregation functions to the group-by query. +func (mwpngb *MessageWithPhpNamespaceGroupBy) Aggregate(fns ...AggregateFunc) *MessageWithPhpNamespaceGroupBy { + mwpngb.fns = append(mwpngb.fns, fns...) + return mwpngb +} + +// Scan applies the selector query and scans the result into the given value. +func (mwpngb *MessageWithPhpNamespaceGroupBy) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, mwpngb.build.ctx, ent.OpQueryGroupBy) + if err := mwpngb.build.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*MessageWithPhpNamespaceQuery, *MessageWithPhpNamespaceGroupBy](ctx, mwpngb.build, mwpngb, mwpngb.build.inters, v) +} + +func (mwpngb *MessageWithPhpNamespaceGroupBy) sqlScan(ctx context.Context, root *MessageWithPhpNamespaceQuery, v any) error { + selector := root.sqlQuery(ctx).Select() + aggregation := make([]string, 0, len(mwpngb.fns)) + for _, fn := range mwpngb.fns { + aggregation = append(aggregation, fn(selector)) + } + if len(selector.SelectedColumns()) == 0 { + columns := make([]string, 0, len(*mwpngb.flds)+len(mwpngb.fns)) + for _, f := range *mwpngb.flds { + columns = append(columns, selector.C(f)) + } + columns = append(columns, aggregation...) + selector.Select(columns...) + } + selector.GroupBy(selector.Columns(*mwpngb.flds...)...) + if err := selector.Err(); err != nil { + return err + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := mwpngb.build.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} + +// MessageWithPhpNamespaceSelect is the builder for selecting fields of MessageWithPhpNamespace entities. +type MessageWithPhpNamespaceSelect struct { + *MessageWithPhpNamespaceQuery + selector +} + +// Aggregate adds the given aggregation functions to the selector query. +func (mwpns *MessageWithPhpNamespaceSelect) Aggregate(fns ...AggregateFunc) *MessageWithPhpNamespaceSelect { + mwpns.fns = append(mwpns.fns, fns...) + return mwpns +} + +// Scan applies the selector query and scans the result into the given value. +func (mwpns *MessageWithPhpNamespaceSelect) Scan(ctx context.Context, v any) error { + ctx = setContextOp(ctx, mwpns.ctx, ent.OpQuerySelect) + if err := mwpns.prepareQuery(ctx); err != nil { + return err + } + return scanWithInterceptors[*MessageWithPhpNamespaceQuery, *MessageWithPhpNamespaceSelect](ctx, mwpns.MessageWithPhpNamespaceQuery, mwpns, mwpns.inters, v) +} + +func (mwpns *MessageWithPhpNamespaceSelect) sqlScan(ctx context.Context, root *MessageWithPhpNamespaceQuery, v any) error { + selector := root.sqlQuery(ctx) + aggregation := make([]string, 0, len(mwpns.fns)) + for _, fn := range mwpns.fns { + aggregation = append(aggregation, fn(selector)) + } + switch n := len(*mwpns.selector.flds); { + case n == 0 && len(aggregation) > 0: + selector.Select(aggregation...) + case n != 0 && len(aggregation) > 0: + selector.AppendSelect(aggregation...) + } + rows := &sql.Rows{} + query, args := selector.Query() + if err := mwpns.driver.Query(ctx, query, args, rows); err != nil { + return err + } + defer rows.Close() + return sql.ScanSlice(rows, v) +} diff --git a/entproto/internal/entprototest/ent/messagewithphpnamespace_update.go b/entproto/internal/entprototest/ent/messagewithphpnamespace_update.go new file mode 100644 index 000000000..290814d0b --- /dev/null +++ b/entproto/internal/entprototest/ent/messagewithphpnamespace_update.go @@ -0,0 +1,209 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +import ( + "context" + "errors" + "fmt" + + "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithphpnamespace" + "entgo.io/contrib/entproto/internal/entprototest/ent/predicate" + "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "entgo.io/ent/schema/field" +) + +// MessageWithPhpNamespaceUpdate is the builder for updating MessageWithPhpNamespace entities. +type MessageWithPhpNamespaceUpdate struct { + config + hooks []Hook + mutation *MessageWithPhpNamespaceMutation +} + +// Where appends a list predicates to the MessageWithPhpNamespaceUpdate builder. +func (mwpnu *MessageWithPhpNamespaceUpdate) Where(ps ...predicate.MessageWithPhpNamespace) *MessageWithPhpNamespaceUpdate { + mwpnu.mutation.Where(ps...) + return mwpnu +} + +// SetName sets the "name" field. +func (mwpnu *MessageWithPhpNamespaceUpdate) SetName(s string) *MessageWithPhpNamespaceUpdate { + mwpnu.mutation.SetName(s) + return mwpnu +} + +// SetNillableName sets the "name" field if the given value is not nil. +func (mwpnu *MessageWithPhpNamespaceUpdate) SetNillableName(s *string) *MessageWithPhpNamespaceUpdate { + if s != nil { + mwpnu.SetName(*s) + } + return mwpnu +} + +// Mutation returns the MessageWithPhpNamespaceMutation object of the builder. +func (mwpnu *MessageWithPhpNamespaceUpdate) Mutation() *MessageWithPhpNamespaceMutation { + return mwpnu.mutation +} + +// Save executes the query and returns the number of nodes affected by the update operation. +func (mwpnu *MessageWithPhpNamespaceUpdate) Save(ctx context.Context) (int, error) { + return withHooks(ctx, mwpnu.sqlSave, mwpnu.mutation, mwpnu.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (mwpnu *MessageWithPhpNamespaceUpdate) SaveX(ctx context.Context) int { + affected, err := mwpnu.Save(ctx) + if err != nil { + panic(err) + } + return affected +} + +// Exec executes the query. +func (mwpnu *MessageWithPhpNamespaceUpdate) Exec(ctx context.Context) error { + _, err := mwpnu.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (mwpnu *MessageWithPhpNamespaceUpdate) ExecX(ctx context.Context) { + if err := mwpnu.Exec(ctx); err != nil { + panic(err) + } +} + +func (mwpnu *MessageWithPhpNamespaceUpdate) sqlSave(ctx context.Context) (n int, err error) { + _spec := sqlgraph.NewUpdateSpec(messagewithphpnamespace.Table, messagewithphpnamespace.Columns, sqlgraph.NewFieldSpec(messagewithphpnamespace.FieldID, field.TypeInt)) + if ps := mwpnu.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := mwpnu.mutation.Name(); ok { + _spec.SetField(messagewithphpnamespace.FieldName, field.TypeString, value) + } + if n, err = sqlgraph.UpdateNodes(ctx, mwpnu.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{messagewithphpnamespace.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return 0, err + } + mwpnu.mutation.done = true + return n, nil +} + +// MessageWithPhpNamespaceUpdateOne is the builder for updating a single MessageWithPhpNamespace entity. +type MessageWithPhpNamespaceUpdateOne struct { + config + fields []string + hooks []Hook + mutation *MessageWithPhpNamespaceMutation +} + +// SetName sets the "name" field. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) SetName(s string) *MessageWithPhpNamespaceUpdateOne { + mwpnuo.mutation.SetName(s) + return mwpnuo +} + +// SetNillableName sets the "name" field if the given value is not nil. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) SetNillableName(s *string) *MessageWithPhpNamespaceUpdateOne { + if s != nil { + mwpnuo.SetName(*s) + } + return mwpnuo +} + +// Mutation returns the MessageWithPhpNamespaceMutation object of the builder. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) Mutation() *MessageWithPhpNamespaceMutation { + return mwpnuo.mutation +} + +// Where appends a list predicates to the MessageWithPhpNamespaceUpdate builder. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) Where(ps ...predicate.MessageWithPhpNamespace) *MessageWithPhpNamespaceUpdateOne { + mwpnuo.mutation.Where(ps...) + return mwpnuo +} + +// Select allows selecting one or more fields (columns) of the returned entity. +// The default is selecting all fields defined in the entity schema. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) Select(field string, fields ...string) *MessageWithPhpNamespaceUpdateOne { + mwpnuo.fields = append([]string{field}, fields...) + return mwpnuo +} + +// Save executes the query and returns the updated MessageWithPhpNamespace entity. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) Save(ctx context.Context) (*MessageWithPhpNamespace, error) { + return withHooks(ctx, mwpnuo.sqlSave, mwpnuo.mutation, mwpnuo.hooks) +} + +// SaveX is like Save, but panics if an error occurs. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) SaveX(ctx context.Context) *MessageWithPhpNamespace { + node, err := mwpnuo.Save(ctx) + if err != nil { + panic(err) + } + return node +} + +// Exec executes the query on the entity. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) Exec(ctx context.Context) error { + _, err := mwpnuo.Save(ctx) + return err +} + +// ExecX is like Exec, but panics if an error occurs. +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) ExecX(ctx context.Context) { + if err := mwpnuo.Exec(ctx); err != nil { + panic(err) + } +} + +func (mwpnuo *MessageWithPhpNamespaceUpdateOne) sqlSave(ctx context.Context) (_node *MessageWithPhpNamespace, err error) { + _spec := sqlgraph.NewUpdateSpec(messagewithphpnamespace.Table, messagewithphpnamespace.Columns, sqlgraph.NewFieldSpec(messagewithphpnamespace.FieldID, field.TypeInt)) + id, ok := mwpnuo.mutation.ID() + if !ok { + return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "MessageWithPhpNamespace.id" for update`)} + } + _spec.Node.ID.Value = id + if fields := mwpnuo.fields; len(fields) > 0 { + _spec.Node.Columns = make([]string, 0, len(fields)) + _spec.Node.Columns = append(_spec.Node.Columns, messagewithphpnamespace.FieldID) + for _, f := range fields { + if !messagewithphpnamespace.ValidColumn(f) { + return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} + } + if f != messagewithphpnamespace.FieldID { + _spec.Node.Columns = append(_spec.Node.Columns, f) + } + } + } + if ps := mwpnuo.mutation.predicates; len(ps) > 0 { + _spec.Predicate = func(selector *sql.Selector) { + for i := range ps { + ps[i](selector) + } + } + } + if value, ok := mwpnuo.mutation.Name(); ok { + _spec.SetField(messagewithphpnamespace.FieldName, field.TypeString, value) + } + _node = &MessageWithPhpNamespace{config: mwpnuo.config} + _spec.Assign = _node.assignValues + _spec.ScanValues = _node.scanValues + if err = sqlgraph.UpdateNode(ctx, mwpnuo.driver, _spec); err != nil { + if _, ok := err.(*sqlgraph.NotFoundError); ok { + err = &NotFoundError{messagewithphpnamespace.Label} + } else if sqlgraph.IsConstraintError(err) { + err = &ConstraintError{msg: err.Error(), wrap: err} + } + return nil, err + } + mwpnuo.mutation.done = true + return _node, nil +} diff --git a/entproto/internal/entprototest/ent/migrate/schema.go b/entproto/internal/entprototest/ent/migrate/schema.go index a63ede3c4..22f28e623 100644 --- a/entproto/internal/entprototest/ent/migrate/schema.go +++ b/entproto/internal/entprototest/ent/migrate/schema.go @@ -223,6 +223,17 @@ var ( Columns: MessageWithPackageNamesColumns, PrimaryKey: []*schema.Column{MessageWithPackageNamesColumns[0]}, } + // MessageWithPhpNamespacesColumns holds the columns for the "message_with_php_namespaces" table. + MessageWithPhpNamespacesColumns = []*schema.Column{ + {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "name", Type: field.TypeString}, + } + // MessageWithPhpNamespacesTable holds the schema information for the "message_with_php_namespaces" table. + MessageWithPhpNamespacesTable = &schema.Table{ + Name: "message_with_php_namespaces", + Columns: MessageWithPhpNamespacesColumns, + PrimaryKey: []*schema.Column{MessageWithPhpNamespacesColumns[0]}, + } // MessageWithStringsColumns holds the columns for the "message_with_strings" table. MessageWithStringsColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, @@ -384,6 +395,7 @@ var ( MessageWithIntsTable, MessageWithOptionalsTable, MessageWithPackageNamesTable, + MessageWithPhpNamespacesTable, MessageWithStringsTable, NoBackrefsTable, OneMethodServicesTable, diff --git a/entproto/internal/entprototest/ent/mutation.go b/entproto/internal/entprototest/ent/mutation.go index 1d5a89ff0..606db6246 100644 --- a/entproto/internal/entprototest/ent/mutation.go +++ b/entproto/internal/entprototest/ent/mutation.go @@ -21,6 +21,7 @@ import ( "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithints" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithoptionals" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithpackagename" + "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithphpnamespace" "entgo.io/contrib/entproto/internal/entprototest/ent/messagewithstrings" "entgo.io/contrib/entproto/internal/entprototest/ent/nobackref" "entgo.io/contrib/entproto/internal/entprototest/ent/portal" @@ -59,6 +60,7 @@ const ( TypeMessageWithInts = "MessageWithInts" TypeMessageWithOptionals = "MessageWithOptionals" TypeMessageWithPackageName = "MessageWithPackageName" + TypeMessageWithPhpNamespace = "MessageWithPhpNamespace" TypeMessageWithStrings = "MessageWithStrings" TypeNoBackref = "NoBackref" TypeOneMethodService = "OneMethodService" @@ -6740,6 +6742,332 @@ func (m *MessageWithPackageNameMutation) ResetEdge(name string) error { return fmt.Errorf("unknown MessageWithPackageName edge %s", name) } +// MessageWithPhpNamespaceMutation represents an operation that mutates the MessageWithPhpNamespace nodes in the graph. +type MessageWithPhpNamespaceMutation struct { + config + op Op + typ string + id *int + name *string + clearedFields map[string]struct{} + done bool + oldValue func(context.Context) (*MessageWithPhpNamespace, error) + predicates []predicate.MessageWithPhpNamespace +} + +var _ ent.Mutation = (*MessageWithPhpNamespaceMutation)(nil) + +// messagewithphpnamespaceOption allows management of the mutation configuration using functional options. +type messagewithphpnamespaceOption func(*MessageWithPhpNamespaceMutation) + +// newMessageWithPhpNamespaceMutation creates new mutation for the MessageWithPhpNamespace entity. +func newMessageWithPhpNamespaceMutation(c config, op Op, opts ...messagewithphpnamespaceOption) *MessageWithPhpNamespaceMutation { + m := &MessageWithPhpNamespaceMutation{ + config: c, + op: op, + typ: TypeMessageWithPhpNamespace, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withMessageWithPhpNamespaceID sets the ID field of the mutation. +func withMessageWithPhpNamespaceID(id int) messagewithphpnamespaceOption { + return func(m *MessageWithPhpNamespaceMutation) { + var ( + err error + once sync.Once + value *MessageWithPhpNamespace + ) + m.oldValue = func(ctx context.Context) (*MessageWithPhpNamespace, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().MessageWithPhpNamespace.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withMessageWithPhpNamespace sets the old MessageWithPhpNamespace of the mutation. +func withMessageWithPhpNamespace(node *MessageWithPhpNamespace) messagewithphpnamespaceOption { + return func(m *MessageWithPhpNamespaceMutation) { + m.oldValue = func(context.Context) (*MessageWithPhpNamespace, error) { + return node, nil + } + m.id = &node.ID + } +} + +// Client returns a new `ent.Client` from the mutation. If the mutation was +// executed in a transaction (ent.Tx), a transactional client is returned. +func (m MessageWithPhpNamespaceMutation) Client() *Client { + client := &Client{config: m.config} + client.init() + return client +} + +// Tx returns an `ent.Tx` for mutations that were executed in transactions; +// it returns an error otherwise. +func (m MessageWithPhpNamespaceMutation) Tx() (*Tx, error) { + if _, ok := m.driver.(*txDriver); !ok { + return nil, errors.New("ent: mutation is not running in a transaction") + } + tx := &Tx{config: m.config} + tx.init() + return tx, nil +} + +// ID returns the ID value in the mutation. Note that the ID is only available +// if it was provided to the builder or after it was returned from the database. +func (m *MessageWithPhpNamespaceMutation) ID() (id int, exists bool) { + if m.id == nil { + return + } + return *m.id, true +} + +// IDs queries the database and returns the entity ids that match the mutation's predicate. +// That means, if the mutation is applied within a transaction with an isolation level such +// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated +// or updated by the mutation. +func (m *MessageWithPhpNamespaceMutation) IDs(ctx context.Context) ([]int, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []int{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().MessageWithPhpNamespace.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetName sets the "name" field. +func (m *MessageWithPhpNamespaceMutation) SetName(s string) { + m.name = &s +} + +// Name returns the value of the "name" field in the mutation. +func (m *MessageWithPhpNamespaceMutation) Name() (r string, exists bool) { + v := m.name + if v == nil { + return + } + return *v, true +} + +// OldName returns the old "name" field's value of the MessageWithPhpNamespace entity. +// If the MessageWithPhpNamespace object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *MessageWithPhpNamespaceMutation) OldName(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldName is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldName requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldName: %w", err) + } + return oldValue.Name, nil +} + +// ResetName resets all changes to the "name" field. +func (m *MessageWithPhpNamespaceMutation) ResetName() { + m.name = nil +} + +// Where appends a list predicates to the MessageWithPhpNamespaceMutation builder. +func (m *MessageWithPhpNamespaceMutation) Where(ps ...predicate.MessageWithPhpNamespace) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the MessageWithPhpNamespaceMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *MessageWithPhpNamespaceMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.MessageWithPhpNamespace, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *MessageWithPhpNamespaceMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *MessageWithPhpNamespaceMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (MessageWithPhpNamespace). +func (m *MessageWithPhpNamespaceMutation) Type() string { + return m.typ +} + +// Fields returns all fields that were changed during this mutation. Note that in +// order to get all numeric fields that were incremented/decremented, call +// AddedFields(). +func (m *MessageWithPhpNamespaceMutation) Fields() []string { + fields := make([]string, 0, 1) + if m.name != nil { + fields = append(fields, messagewithphpnamespace.FieldName) + } + return fields +} + +// Field returns the value of a field with the given name. The second boolean +// return value indicates that this field was not set, or was not defined in the +// schema. +func (m *MessageWithPhpNamespaceMutation) Field(name string) (ent.Value, bool) { + switch name { + case messagewithphpnamespace.FieldName: + return m.Name() + } + return nil, false +} + +// OldField returns the old value of the field from the database. An error is +// returned if the mutation operation is not UpdateOne, or the query to the +// database failed. +func (m *MessageWithPhpNamespaceMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case messagewithphpnamespace.FieldName: + return m.OldName(ctx) + } + return nil, fmt.Errorf("unknown MessageWithPhpNamespace field %s", name) +} + +// SetField sets the value of a field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *MessageWithPhpNamespaceMutation) SetField(name string, value ent.Value) error { + switch name { + case messagewithphpnamespace.FieldName: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetName(v) + return nil + } + return fmt.Errorf("unknown MessageWithPhpNamespace field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *MessageWithPhpNamespaceMutation) AddedFields() []string { + return nil +} + +// AddedField returns the numeric value that was incremented/decremented on a field +// with the given name. The second boolean return value indicates that this field +// was not set, or was not defined in the schema. +func (m *MessageWithPhpNamespaceMutation) AddedField(name string) (ent.Value, bool) { + return nil, false +} + +// AddField adds the value to the field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *MessageWithPhpNamespaceMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown MessageWithPhpNamespace numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *MessageWithPhpNamespaceMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *MessageWithPhpNamespaceMutation) FieldCleared(name string) bool { + _, ok := m.clearedFields[name] + return ok +} + +// ClearField clears the value of the field with the given name. It returns an +// error if the field is not defined in the schema. +func (m *MessageWithPhpNamespaceMutation) ClearField(name string) error { + return fmt.Errorf("unknown MessageWithPhpNamespace nullable field %s", name) +} + +// ResetField resets all changes in the mutation for the field with the given name. +// It returns an error if the field is not defined in the schema. +func (m *MessageWithPhpNamespaceMutation) ResetField(name string) error { + switch name { + case messagewithphpnamespace.FieldName: + m.ResetName() + return nil + } + return fmt.Errorf("unknown MessageWithPhpNamespace field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *MessageWithPhpNamespaceMutation) AddedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *MessageWithPhpNamespaceMutation) AddedIDs(name string) []ent.Value { + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *MessageWithPhpNamespaceMutation) RemovedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *MessageWithPhpNamespaceMutation) RemovedIDs(name string) []ent.Value { + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *MessageWithPhpNamespaceMutation) ClearedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *MessageWithPhpNamespaceMutation) EdgeCleared(name string) bool { + return false +} + +// ClearEdge clears the value of the edge with the given name. It returns an error +// if that edge is not defined in the schema. +func (m *MessageWithPhpNamespaceMutation) ClearEdge(name string) error { + return fmt.Errorf("unknown MessageWithPhpNamespace unique edge %s", name) +} + +// ResetEdge resets all changes to the edge with the given name in this mutation. +// It returns an error if the edge is not defined in the schema. +func (m *MessageWithPhpNamespaceMutation) ResetEdge(name string) error { + return fmt.Errorf("unknown MessageWithPhpNamespace edge %s", name) +} + // MessageWithStringsMutation represents an operation that mutates the MessageWithStrings nodes in the graph. type MessageWithStringsMutation struct { config diff --git a/entproto/internal/entprototest/ent/predicate/predicate.go b/entproto/internal/entprototest/ent/predicate/predicate.go index db302b771..11b739938 100644 --- a/entproto/internal/entprototest/ent/predicate/predicate.go +++ b/entproto/internal/entprototest/ent/predicate/predicate.go @@ -54,6 +54,9 @@ type MessageWithOptionals func(*sql.Selector) // MessageWithPackageName is the predicate function for messagewithpackagename builders. type MessageWithPackageName func(*sql.Selector) +// MessageWithPhpNamespace is the predicate function for messagewithphpnamespace builders. +type MessageWithPhpNamespace func(*sql.Selector) + // MessageWithStrings is the predicate function for messagewithstrings builders. type MessageWithStrings func(*sql.Selector) diff --git a/entproto/internal/entprototest/ent/schema/message_with_php_namespace.go b/entproto/internal/entprototest/ent/schema/message_with_php_namespace.go new file mode 100644 index 000000000..3625da61d --- /dev/null +++ b/entproto/internal/entprototest/ent/schema/message_with_php_namespace.go @@ -0,0 +1,44 @@ +// Copyright 2019-present Facebook +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package schema + +import ( + "entgo.io/contrib/entproto" + "entgo.io/ent" + "entgo.io/ent/schema" + "entgo.io/ent/schema/field" +) + +// MessageWithPhpNamespace holds the schema definition for the MessageWithPhpNamespace entity. +type MessageWithPhpNamespace struct { + ent.Schema +} + +// Fields of the MessageWithPhpNamespace. +func (MessageWithPhpNamespace) Fields() []ent.Field { + return []ent.Field{ + field.String("name"). + Annotations(entproto.Field(2)), + } +} + +func (MessageWithPhpNamespace) Annotations() []schema.Annotation { + return []schema.Annotation{ + entproto.Message( + entproto.PackageName("io.entgo.apps.todo"), + entproto.PhpNamespace("My\\Company\\Todo"), + ), + } +} diff --git a/entproto/internal/entprototest/ent/tx.go b/entproto/internal/entprototest/ent/tx.go index 212efa85f..1005a9833 100644 --- a/entproto/internal/entprototest/ent/tx.go +++ b/entproto/internal/entprototest/ent/tx.go @@ -44,6 +44,8 @@ type Tx struct { MessageWithOptionals *MessageWithOptionalsClient // MessageWithPackageName is the client for interacting with the MessageWithPackageName builders. MessageWithPackageName *MessageWithPackageNameClient + // MessageWithPhpNamespace is the client for interacting with the MessageWithPhpNamespace builders. + MessageWithPhpNamespace *MessageWithPhpNamespaceClient // MessageWithStrings is the client for interacting with the MessageWithStrings builders. MessageWithStrings *MessageWithStringsClient // NoBackref is the client for interacting with the NoBackref builders. @@ -207,6 +209,7 @@ func (tx *Tx) init() { tx.MessageWithInts = NewMessageWithIntsClient(tx.config) tx.MessageWithOptionals = NewMessageWithOptionalsClient(tx.config) tx.MessageWithPackageName = NewMessageWithPackageNameClient(tx.config) + tx.MessageWithPhpNamespace = NewMessageWithPhpNamespaceClient(tx.config) tx.MessageWithStrings = NewMessageWithStringsClient(tx.config) tx.NoBackref = NewNoBackrefClient(tx.config) tx.OneMethodService = NewOneMethodServiceClient(tx.config) diff --git a/entproto/message.go b/entproto/message.go index cc50e377b..08fcaadf1 100644 --- a/entproto/message.go +++ b/entproto/message.go @@ -55,9 +55,17 @@ func PackageName(pkg string) MessageOption { } } +// PhpNamespace modifies the generated message's PHP namespace +func PhpNamespace(namespace string) MessageOption { + return func(msg *message) { + msg.PhpNamespace = namespace + } +} + type message struct { - Generate bool - Package string + Generate bool + Package string + PhpNamespace string } func (m message) Name() string {