entc/gen: temporary workaround for runtime.tmpl (#387)

* entc/gen: temporary workaround for runtime.tmpl

* ci: golangci version
This commit is contained in:
Ariel Mashraki
2020-03-16 10:04:31 +02:00
committed by GitHub
parent 7988d3084d
commit 9cff230d34
17 changed files with 188 additions and 162 deletions

View File

@@ -24,7 +24,7 @@ commands:
jobs:
lint:
docker:
- image: golangci/golangci-lint
- image: golangci/golangci-lint:v1.23-alpine
steps:
- checkout
- *mktestdir

File diff suppressed because one or more lines are too long

View File

@@ -12,16 +12,18 @@ in the LICENSE file in the root directory of this source tree.
import "{{ $.Config.Package }}"
{{ $pkg := base $.Config.Package }}
{{ range $n := $.Nodes }}
{{ $name := print $n.Name "Func" }}
{{ $type := print "*ent." $n.MutationName }}
{{ $type := printf "*%s.%s" $pkg $n.MutationName }}
// The {{ $name }} type is an adapter to allow the use of ordinary
// function as {{ $n.Name }} mutator.
type {{ $name }} func(context.Context, {{ $type }}) (ent.Value, error)
type {{ $name }} func(context.Context, {{ $type }}) ({{ $pkg }}.Value, error)
// Mutate calls f(ctx, m).
func (f {{ $name }}) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
func (f {{ $name }}) Mutate(ctx context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) {
mv, ok := m.({{ $type }})
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T. expect {{ $type }}", m)
@@ -32,11 +34,11 @@ import "{{ $.Config.Package }}"
// On executes the given hook only of the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
// hook.On(Log, {{ $pkg }}.Delete|{{ $pkg }}.Create)
//
func On(hk ent.Hook, op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
func On(hk {{ $pkg }}.Hook, op {{ $pkg }}.Op) {{ $pkg }}.Hook {
return func(next {{ $pkg }}.Mutator) {{ $pkg }}.Mutator {
return {{ $pkg }}.MutateFunc(func(ctx context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) {
if m.Op().Is(op) {
return hk(next).Mutate(ctx, m)
}
@@ -47,15 +49,15 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
// return []ent.Hook{
// Reject(ent.Delete|ent.Update),
// func (T) Hooks() []{{ $pkg }}.Hook {
// return []{{ $pkg }}.Hook{
// Reject({{ $pkg }}.Delete|{{ $pkg }}.Update),
// }
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
func Reject(op {{ $pkg }}.Op) ent.Hook {
return func(next {{ $pkg }}.Mutator) {{ $pkg }}.Mutator {
return {{ $pkg }}.MutateFunc(func(ctx context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}

View File

@@ -12,6 +12,8 @@ in the LICENSE file in the root directory of this source tree.
import "{{ $.Config.Package }}"
{{ $pkg := base $.Config.Package }}
var (
// Allow may be returned by read/write rules to indicate that the policy
// evaluation should terminate with an allow decision.
@@ -39,12 +41,12 @@ type (
// ReadRule defines the interface deciding whether a read is allowed.
ReadRule interface {
EvalRead(context.Context, ent.Value) error
EvalRead(context.Context, {{ $pkg }}.Value) error
}
)
// EvalRead evaluates a load against a read policy.
func (policy ReadPolicy) EvalRead(ctx context.Context, v ent.Value) error {
func (policy ReadPolicy) EvalRead(ctx context.Context, v {{ $pkg }}.Value) error {
for _, rule := range policy {
switch err := rule.EvalRead(ctx, v); {
case err == nil || errors.Is(err, Skip):
@@ -59,10 +61,10 @@ func (policy ReadPolicy) EvalRead(ctx context.Context, v ent.Value) error {
// ReadRuleFunc type is an adapter to allow the use of
// ordinary functions as read rules.
type ReadRuleFunc func(context.Context, ent.Value) error
type ReadRuleFunc func(context.Context, {{ $pkg }}.Value) error
// Eval calls f(ctx, v).
func (f ReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
func (f ReadRuleFunc) EvalRead(ctx context.Context, v {{ $pkg }}.Value) error {
return f(ctx, v)
}
@@ -72,12 +74,12 @@ type (
// WriteRule defines the interface deciding whether a write is allowed.
WriteRule interface {
EvalWrite(context.Context, ent.Mutation) error
EvalWrite(context.Context, {{ $pkg }}.Mutation) error
}
)
// EvalWrite evaluates a mutation against a write policy.
func (policy WritePolicy) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (policy WritePolicy) EvalWrite(ctx context.Context, m {{ $pkg }}.Mutation) error {
for _, rule := range policy {
switch err := rule.EvalWrite(ctx, m); {
case err == nil || errors.Is(err, Skip):
@@ -92,10 +94,10 @@ func (policy WritePolicy) EvalWrite(ctx context.Context, m ent.Mutation) error {
// WriteRuleFunc type is an adapter to allow the use of
// ordinary functions as write rules.
type WriteRuleFunc func(context.Context, ent.Mutation) error
type WriteRuleFunc func(context.Context, {{ $pkg }}.Mutation) error
// Eval calls f(ctx, m).
func (f WriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (f WriteRuleFunc) EvalWrite(ctx context.Context, m {{ $pkg }}.Mutation) error {
return f(ctx, m)
}
@@ -106,12 +108,12 @@ type Policy struct {
}
// EvalRead forwards evaluation to read policy.
func (policy Policy) EvalRead(ctx context.Context, v ent.Value) error {
func (policy Policy) EvalRead(ctx context.Context, v {{ $pkg }}.Value) error {
return policy.Read.EvalRead(ctx, v)
}
// EvalWrite forwards evaluation to write policy.
func (policy Policy) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (policy Policy) EvalWrite(ctx context.Context, m {{ $pkg }}.Mutation) error {
return policy.Write.EvalWrite(ctx, m)
}
@@ -133,18 +135,18 @@ func AlwaysDenyRule() ReadWriteRule {
type fixedDecisionRule struct { err error }
func (f fixedDecisionRule) EvalRead(context.Context, ent.Value) error { return f.err }
func (f fixedDecisionRule) EvalWrite(context.Context, ent.Mutation) error { return f.err }
func (f fixedDecisionRule) EvalRead(context.Context, {{ $pkg }}.Value) error { return f.err }
func (f fixedDecisionRule) EvalWrite(context.Context, {{ $pkg }}.Mutation) error { return f.err }
{{- range $n := $.Nodes }}
{{ $name := print $n.Name "ReadRuleFunc" }}
{{ $type := print "*ent." $n.Name }}
{{ $type := printf "*%s.%s" $pkg $n.Name }}
// The {{ $name }} type is an adapter to allow the use of ordinary
// functions as a read rule.
type {{ $name }} func(context.Context, {{ $type }}) error
// EvalRead calls f(ctx, v).
func (f {{ $name }}) EvalRead(ctx context.Context, v ent.Value) error {
func (f {{ $name }}) EvalRead(ctx context.Context, v {{ $pkg }}.Value) error {
if v, ok := v.({{ $type }}); ok {
return f(ctx, v)
}
@@ -152,13 +154,13 @@ func (f fixedDecisionRule) EvalWrite(context.Context, ent.Mutation) error { retu
}
{{ $name = print $n.Name "WriteRuleFunc" }}
{{ $type = print "*ent." $n.MutationName }}
{{ $type = printf "*%s.%s" $pkg $n.MutationName }}
// The {{ $name }} type is an adapter to allow the use of ordinary
// functions as a write rule.
type {{ $name }} func(context.Context, {{ $type }}) error
// EvalWrite calls f(ctx, m).
func (f {{ $name }}) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (f {{ $name }}) EvalWrite(ctx context.Context, m {{ $pkg }}.Mutation) error {
if m, ok := m.({{ $type }}); ok {
return f(ctx, m)
}

View File

@@ -113,19 +113,20 @@ func init() {
{{- end }}
{{- with $n.Fields }}
{{ $pkg }}Fields := {{ $schema }}.{{ $n.Name }}{}.Fields()
_ = {{ $pkg }}Fields
{{- end }}
{{- range $i, $f := $n.Fields }}
{{- $desc := print $pkg "Desc" $f.StructField }}
{{- /* enum default values handled near their declarations (in type package). */}}
{{- if or (and $f.Default (not $f.IsEnum)) $f.UpdateDefault $f.Validators }}
{{- if $f.Position.MixedIn }}
// {{ $desc }} is the schema descriptor for {{ $f.Name }} field.
{{ $desc }} := {{ $pkg }}MixinFields[{{ $f.Position.MixinIndex }}][{{ $f.Position.Index }}].Descriptor()
{{- else }}
// {{ $desc }} is the schema descriptor for {{ $f.Name }} field.
{{ $desc }} := {{ $pkg }}Fields[{{ $f.Position.Index }}].Descriptor()
// {{ $desc }} is the schema descriptor for {{ $f.Name }} field.
{{ $desc }} := {{ $pkg }}MixinFields[{{ $f.Position.MixinIndex }}][{{ $f.Position.Index }}].Descriptor()
{{- else }}
// {{ $desc }} is the schema descriptor for {{ $f.Name }} field.
{{ $desc }} := {{ $pkg }}Fields[{{ $f.Position.Index }}].Descriptor()
{{- end }}
{{- end }}
{{- end }}
{{- if and $f.Default (not $f.IsEnum) }}
{{- $default := print $pkg "." $f.DefaultName }}
// {{ $default }} holds the default value on creation for the {{ $f.Name }} field.

View File

@@ -17,6 +17,7 @@ import (
// to their package variables.
func init() {
blobFields := schema.Blob{}.Fields()
_ = blobFields
// blobDescUUID is the schema descriptor for uuid field.
blobDescUUID := blobFields[1].Descriptor()
// blob.DefaultUUID holds the default value on creation for the uuid field.

View File

@@ -33,6 +33,7 @@ func init() {
cardMixin[0].Fields(),
}
cardFields := schema.Card{}.Fields()
_ = cardFields
// cardDescCreateTime is the schema descriptor for create_time field.
cardDescCreateTime := cardMixinFields[0][0].Descriptor()
// card.DefaultCreateTime holds the default value on creation for the create_time field.
@@ -52,11 +53,13 @@ func init() {
// card.NameValidator is a validator for the "name" field. It is called by the builders before save.
card.NameValidator = cardDescName.Validators[0].(func(string) error)
fieldtypeFields := schema.FieldType{}.Fields()
_ = fieldtypeFields
// fieldtypeDescValidateOptionalInt32 is the schema descriptor for validate_optional_int32 field.
fieldtypeDescValidateOptionalInt32 := fieldtypeFields[15].Descriptor()
// fieldtype.ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save.
fieldtype.ValidateOptionalInt32Validator = fieldtypeDescValidateOptionalInt32.Validators[0].(func(int32) error)
fileFields := schema.File{}.Fields()
_ = fileFields
// fileDescSize is the schema descriptor for size field.
fileDescSize := fileFields[0].Descriptor()
// file.DefaultSize holds the default value on creation for the size field.
@@ -64,6 +67,7 @@ func init() {
// file.SizeValidator is a validator for the "size" field. It is called by the builders before save.
file.SizeValidator = fileDescSize.Validators[0].(func(int) error)
groupFields := schema.Group{}.Fields()
_ = groupFields
// groupDescActive is the schema descriptor for active field.
groupDescActive := groupFields[0].Descriptor()
// group.DefaultActive holds the default value on creation for the active field.
@@ -111,6 +115,7 @@ func init() {
}
}()
groupinfoFields := schema.GroupInfo{}.Fields()
_ = groupinfoFields
// groupinfoDescMaxUsers is the schema descriptor for max_users field.
groupinfoDescMaxUsers := groupinfoFields[1].Descriptor()
// groupinfo.DefaultMaxUsers holds the default value on creation for the max_users field.
@@ -120,6 +125,7 @@ func init() {
userMixin[0].Fields(),
}
userFields := schema.User{}.Fields()
_ = userFields
// userDescOptionalInt is the schema descriptor for optional_int field.
userDescOptionalInt := userMixinFields[0][0].Descriptor()
// user.OptionalIntValidator is a validator for the "optional_int" field. It is called by the builders before save.

View File

@@ -33,6 +33,7 @@ func init() {
cardMixin[0].Fields(),
}
cardFields := schema.Card{}.Fields()
_ = cardFields
// cardDescCreateTime is the schema descriptor for create_time field.
cardDescCreateTime := cardMixinFields[0][0].Descriptor()
// card.DefaultCreateTime holds the default value on creation for the create_time field.
@@ -52,11 +53,13 @@ func init() {
// card.NameValidator is a validator for the "name" field. It is called by the builders before save.
card.NameValidator = cardDescName.Validators[0].(func(string) error)
fieldtypeFields := schema.FieldType{}.Fields()
_ = fieldtypeFields
// fieldtypeDescValidateOptionalInt32 is the schema descriptor for validate_optional_int32 field.
fieldtypeDescValidateOptionalInt32 := fieldtypeFields[15].Descriptor()
// fieldtype.ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save.
fieldtype.ValidateOptionalInt32Validator = fieldtypeDescValidateOptionalInt32.Validators[0].(func(int32) error)
fileFields := schema.File{}.Fields()
_ = fileFields
// fileDescSize is the schema descriptor for size field.
fileDescSize := fileFields[0].Descriptor()
// file.DefaultSize holds the default value on creation for the size field.
@@ -64,6 +67,7 @@ func init() {
// file.SizeValidator is a validator for the "size" field. It is called by the builders before save.
file.SizeValidator = fileDescSize.Validators[0].(func(int) error)
groupFields := schema.Group{}.Fields()
_ = groupFields
// groupDescActive is the schema descriptor for active field.
groupDescActive := groupFields[0].Descriptor()
// group.DefaultActive holds the default value on creation for the active field.
@@ -111,6 +115,7 @@ func init() {
}
}()
groupinfoFields := schema.GroupInfo{}.Fields()
_ = groupinfoFields
// groupinfoDescMaxUsers is the schema descriptor for max_users field.
groupinfoDescMaxUsers := groupinfoFields[1].Descriptor()
// groupinfo.DefaultMaxUsers holds the default value on creation for the max_users field.
@@ -120,6 +125,7 @@ func init() {
userMixin[0].Fields(),
}
userFields := schema.User{}.Fields()
_ = userFields
// userDescOptionalInt is the schema descriptor for optional_int field.
userDescOptionalInt := userMixinFields[0][0].Descriptor()
// user.OptionalIntValidator is a validator for the "optional_int" field. It is called by the builders before save.

View File

@@ -22,6 +22,7 @@ func init() {
card.Hooks[i] = h
}
cardFields := schema.Card{}.Fields()
_ = cardFields
// cardDescNumber is the schema descriptor for number field.
cardDescNumber := cardFields[0].Descriptor()
// card.DefaultNumber holds the default value on creation for the number field.

View File

@@ -10,42 +10,43 @@ import (
"context"
"fmt"
"github.com/facebookincubator/ent/entc/integration/customid/ent"
"github.com/facebookincubator/ent/entc/integration/ent"
"github.com/facebookincubator/ent/entc/integration/migrate/entv1"
)
// The CarFunc type is an adapter to allow the use of ordinary
// function as Car mutator.
type CarFunc func(context.Context, *ent.CarMutation) (ent.Value, error)
type CarFunc func(context.Context, *entv1.CarMutation) (entv1.Value, error)
// Mutate calls f(ctx, m).
func (f CarFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
mv, ok := m.(*ent.CarMutation)
func (f CarFunc) Mutate(ctx context.Context, m entv1.Mutation) (entv1.Value, error) {
mv, ok := m.(*entv1.CarMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CarMutation", m)
return nil, fmt.Errorf("unexpected mutation type %T. expect *entv1.CarMutation", m)
}
return f(ctx, mv)
}
// The UserFunc type is an adapter to allow the use of ordinary
// function as User mutator.
type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error)
type UserFunc func(context.Context, *entv1.UserMutation) (entv1.Value, error)
// Mutate calls f(ctx, m).
func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
mv, ok := m.(*ent.UserMutation)
func (f UserFunc) Mutate(ctx context.Context, m entv1.Mutation) (entv1.Value, error) {
mv, ok := m.(*entv1.UserMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserMutation", m)
return nil, fmt.Errorf("unexpected mutation type %T. expect *entv1.UserMutation", m)
}
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
// hook.On(Log, entv1.Delete|entv1.Create)
//
func On(hk ent.Hook, op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
func On(hk entv1.Hook, op entv1.Op) entv1.Hook {
return func(next entv1.Mutator) entv1.Mutator {
return entv1.MutateFunc(func(ctx context.Context, m entv1.Mutation) (entv1.Value, error) {
if m.Op().Is(op) {
return hk(next).Mutate(ctx, m)
}
@@ -56,15 +57,15 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
// return []ent.Hook{
// Reject(ent.Delete|ent.Update),
// func (T) Hooks() []entv1.Hook {
// return []entv1.Hook{
// Reject(entv1.Delete|entv1.Update),
// }
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
func Reject(op entv1.Op) ent.Hook {
return func(next entv1.Mutator) entv1.Mutator {
return entv1.MutateFunc(func(ctx context.Context, m entv1.Mutation) (entv1.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}

View File

@@ -11,7 +11,7 @@ import (
"errors"
"fmt"
"github.com/facebookincubator/ent/entc/integration/customid/ent"
"github.com/facebookincubator/ent/entc/integration/migrate/entv1"
)
var (
@@ -49,12 +49,12 @@ type (
// ReadRule defines the interface deciding whether a read is allowed.
ReadRule interface {
EvalRead(context.Context, ent.Value) error
EvalRead(context.Context, entv1.Value) error
}
)
// EvalRead evaluates a load against a read policy.
func (policy ReadPolicy) EvalRead(ctx context.Context, v ent.Value) error {
func (policy ReadPolicy) EvalRead(ctx context.Context, v entv1.Value) error {
for _, rule := range policy {
switch err := rule.EvalRead(ctx, v); {
case err == nil || errors.Is(err, Skip):
@@ -69,10 +69,10 @@ func (policy ReadPolicy) EvalRead(ctx context.Context, v ent.Value) error {
// ReadRuleFunc type is an adapter to allow the use of
// ordinary functions as read rules.
type ReadRuleFunc func(context.Context, ent.Value) error
type ReadRuleFunc func(context.Context, entv1.Value) error
// Eval calls f(ctx, v).
func (f ReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
func (f ReadRuleFunc) EvalRead(ctx context.Context, v entv1.Value) error {
return f(ctx, v)
}
@@ -82,12 +82,12 @@ type (
// WriteRule defines the interface deciding whether a write is allowed.
WriteRule interface {
EvalWrite(context.Context, ent.Mutation) error
EvalWrite(context.Context, entv1.Mutation) error
}
)
// EvalWrite evaluates a mutation against a write policy.
func (policy WritePolicy) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (policy WritePolicy) EvalWrite(ctx context.Context, m entv1.Mutation) error {
for _, rule := range policy {
switch err := rule.EvalWrite(ctx, m); {
case err == nil || errors.Is(err, Skip):
@@ -102,10 +102,10 @@ func (policy WritePolicy) EvalWrite(ctx context.Context, m ent.Mutation) error {
// WriteRuleFunc type is an adapter to allow the use of
// ordinary functions as write rules.
type WriteRuleFunc func(context.Context, ent.Mutation) error
type WriteRuleFunc func(context.Context, entv1.Mutation) error
// Eval calls f(ctx, m).
func (f WriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (f WriteRuleFunc) EvalWrite(ctx context.Context, m entv1.Mutation) error {
return f(ctx, m)
}
@@ -116,12 +116,12 @@ type Policy struct {
}
// EvalRead forwards evaluation to read policy.
func (policy Policy) EvalRead(ctx context.Context, v ent.Value) error {
func (policy Policy) EvalRead(ctx context.Context, v entv1.Value) error {
return policy.Read.EvalRead(ctx, v)
}
// EvalWrite forwards evaluation to write policy.
func (policy Policy) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (policy Policy) EvalWrite(ctx context.Context, m entv1.Mutation) error {
return policy.Write.EvalWrite(ctx, m)
}
@@ -143,53 +143,53 @@ func AlwaysDenyRule() ReadWriteRule {
type fixedDecisionRule struct{ err error }
func (f fixedDecisionRule) EvalRead(context.Context, ent.Value) error { return f.err }
func (f fixedDecisionRule) EvalWrite(context.Context, ent.Mutation) error { return f.err }
func (f fixedDecisionRule) EvalRead(context.Context, entv1.Value) error { return f.err }
func (f fixedDecisionRule) EvalWrite(context.Context, entv1.Mutation) error { return f.err }
// The CarReadRuleFunc type is an adapter to allow the use of ordinary
// functions as a read rule.
type CarReadRuleFunc func(context.Context, *ent.Car) error
type CarReadRuleFunc func(context.Context, *entv1.Car) error
// EvalRead calls f(ctx, v).
func (f CarReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
if v, ok := v.(*ent.Car); ok {
func (f CarReadRuleFunc) EvalRead(ctx context.Context, v entv1.Value) error {
if v, ok := v.(*entv1.Car); ok {
return f(ctx, v)
}
return Denyf("ent/privacy: unexpected value type %T, expect *ent.Car", v)
return Denyf("ent/privacy: unexpected value type %T, expect *entv1.Car", v)
}
// The CarWriteRuleFunc type is an adapter to allow the use of ordinary
// functions as a write rule.
type CarWriteRuleFunc func(context.Context, *ent.CarMutation) error
type CarWriteRuleFunc func(context.Context, *entv1.CarMutation) error
// EvalWrite calls f(ctx, m).
func (f CarWriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.CarMutation); ok {
func (f CarWriteRuleFunc) EvalWrite(ctx context.Context, m entv1.Mutation) error {
if m, ok := m.(*entv1.CarMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CarMutation", m)
return Denyf("ent/privacy: unexpected mutation type %T, expect *entv1.CarMutation", m)
}
// The UserReadRuleFunc type is an adapter to allow the use of ordinary
// functions as a read rule.
type UserReadRuleFunc func(context.Context, *ent.User) error
type UserReadRuleFunc func(context.Context, *entv1.User) error
// EvalRead calls f(ctx, v).
func (f UserReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
if v, ok := v.(*ent.User); ok {
func (f UserReadRuleFunc) EvalRead(ctx context.Context, v entv1.Value) error {
if v, ok := v.(*entv1.User); ok {
return f(ctx, v)
}
return Denyf("ent/privacy: unexpected value type %T, expect *ent.User", v)
return Denyf("ent/privacy: unexpected value type %T, expect *entv1.User", v)
}
// The UserWriteRuleFunc type is an adapter to allow the use of ordinary
// functions as a write rule.
type UserWriteRuleFunc func(context.Context, *ent.UserMutation) error
type UserWriteRuleFunc func(context.Context, *entv1.UserMutation) error
// EvalWrite calls f(ctx, m).
func (f UserWriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.UserMutation); ok {
func (f UserWriteRuleFunc) EvalWrite(ctx context.Context, m entv1.Mutation) error {
if m, ok := m.(*entv1.UserMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m)
return Denyf("ent/privacy: unexpected mutation type %T, expect *entv1.UserMutation", m)
}

View File

@@ -17,6 +17,7 @@ import (
// to their package variables.
func init() {
userFields := schema.User{}.Fields()
_ = userFields
// userDescName is the schema descriptor for name field.
userDescName := userFields[2].Descriptor()
// user.NameValidator is a validator for the "name" field. It is called by the builders before save.

View File

@@ -10,68 +10,69 @@ import (
"context"
"fmt"
"github.com/facebookincubator/ent/entc/integration/customid/ent"
"github.com/facebookincubator/ent/entc/integration/ent"
"github.com/facebookincubator/ent/entc/integration/migrate/entv2"
)
// The CarFunc type is an adapter to allow the use of ordinary
// function as Car mutator.
type CarFunc func(context.Context, *ent.CarMutation) (ent.Value, error)
type CarFunc func(context.Context, *entv2.CarMutation) (entv2.Value, error)
// Mutate calls f(ctx, m).
func (f CarFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
mv, ok := m.(*ent.CarMutation)
func (f CarFunc) Mutate(ctx context.Context, m entv2.Mutation) (entv2.Value, error) {
mv, ok := m.(*entv2.CarMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CarMutation", m)
return nil, fmt.Errorf("unexpected mutation type %T. expect *entv2.CarMutation", m)
}
return f(ctx, mv)
}
// The GroupFunc type is an adapter to allow the use of ordinary
// function as Group mutator.
type GroupFunc func(context.Context, *ent.GroupMutation) (ent.Value, error)
type GroupFunc func(context.Context, *entv2.GroupMutation) (entv2.Value, error)
// Mutate calls f(ctx, m).
func (f GroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
mv, ok := m.(*ent.GroupMutation)
func (f GroupFunc) Mutate(ctx context.Context, m entv2.Mutation) (entv2.Value, error) {
mv, ok := m.(*entv2.GroupMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.GroupMutation", m)
return nil, fmt.Errorf("unexpected mutation type %T. expect *entv2.GroupMutation", m)
}
return f(ctx, mv)
}
// The PetFunc type is an adapter to allow the use of ordinary
// function as Pet mutator.
type PetFunc func(context.Context, *ent.PetMutation) (ent.Value, error)
type PetFunc func(context.Context, *entv2.PetMutation) (entv2.Value, error)
// Mutate calls f(ctx, m).
func (f PetFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
mv, ok := m.(*ent.PetMutation)
func (f PetFunc) Mutate(ctx context.Context, m entv2.Mutation) (entv2.Value, error) {
mv, ok := m.(*entv2.PetMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PetMutation", m)
return nil, fmt.Errorf("unexpected mutation type %T. expect *entv2.PetMutation", m)
}
return f(ctx, mv)
}
// The UserFunc type is an adapter to allow the use of ordinary
// function as User mutator.
type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error)
type UserFunc func(context.Context, *entv2.UserMutation) (entv2.Value, error)
// Mutate calls f(ctx, m).
func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
mv, ok := m.(*ent.UserMutation)
func (f UserFunc) Mutate(ctx context.Context, m entv2.Mutation) (entv2.Value, error) {
mv, ok := m.(*entv2.UserMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserMutation", m)
return nil, fmt.Errorf("unexpected mutation type %T. expect *entv2.UserMutation", m)
}
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
// hook.On(Log, entv2.Delete|entv2.Create)
//
func On(hk ent.Hook, op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
func On(hk entv2.Hook, op entv2.Op) entv2.Hook {
return func(next entv2.Mutator) entv2.Mutator {
return entv2.MutateFunc(func(ctx context.Context, m entv2.Mutation) (entv2.Value, error) {
if m.Op().Is(op) {
return hk(next).Mutate(ctx, m)
}
@@ -82,15 +83,15 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
// return []ent.Hook{
// Reject(ent.Delete|ent.Update),
// func (T) Hooks() []entv2.Hook {
// return []entv2.Hook{
// Reject(entv2.Delete|entv2.Update),
// }
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
func Reject(op entv2.Op) ent.Hook {
return func(next entv2.Mutator) entv2.Mutator {
return entv2.MutateFunc(func(ctx context.Context, m entv2.Mutation) (entv2.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}

View File

@@ -11,7 +11,7 @@ import (
"errors"
"fmt"
"github.com/facebookincubator/ent/entc/integration/customid/ent"
"github.com/facebookincubator/ent/entc/integration/migrate/entv2"
)
var (
@@ -49,12 +49,12 @@ type (
// ReadRule defines the interface deciding whether a read is allowed.
ReadRule interface {
EvalRead(context.Context, ent.Value) error
EvalRead(context.Context, entv2.Value) error
}
)
// EvalRead evaluates a load against a read policy.
func (policy ReadPolicy) EvalRead(ctx context.Context, v ent.Value) error {
func (policy ReadPolicy) EvalRead(ctx context.Context, v entv2.Value) error {
for _, rule := range policy {
switch err := rule.EvalRead(ctx, v); {
case err == nil || errors.Is(err, Skip):
@@ -69,10 +69,10 @@ func (policy ReadPolicy) EvalRead(ctx context.Context, v ent.Value) error {
// ReadRuleFunc type is an adapter to allow the use of
// ordinary functions as read rules.
type ReadRuleFunc func(context.Context, ent.Value) error
type ReadRuleFunc func(context.Context, entv2.Value) error
// Eval calls f(ctx, v).
func (f ReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
func (f ReadRuleFunc) EvalRead(ctx context.Context, v entv2.Value) error {
return f(ctx, v)
}
@@ -82,12 +82,12 @@ type (
// WriteRule defines the interface deciding whether a write is allowed.
WriteRule interface {
EvalWrite(context.Context, ent.Mutation) error
EvalWrite(context.Context, entv2.Mutation) error
}
)
// EvalWrite evaluates a mutation against a write policy.
func (policy WritePolicy) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (policy WritePolicy) EvalWrite(ctx context.Context, m entv2.Mutation) error {
for _, rule := range policy {
switch err := rule.EvalWrite(ctx, m); {
case err == nil || errors.Is(err, Skip):
@@ -102,10 +102,10 @@ func (policy WritePolicy) EvalWrite(ctx context.Context, m ent.Mutation) error {
// WriteRuleFunc type is an adapter to allow the use of
// ordinary functions as write rules.
type WriteRuleFunc func(context.Context, ent.Mutation) error
type WriteRuleFunc func(context.Context, entv2.Mutation) error
// Eval calls f(ctx, m).
func (f WriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (f WriteRuleFunc) EvalWrite(ctx context.Context, m entv2.Mutation) error {
return f(ctx, m)
}
@@ -116,12 +116,12 @@ type Policy struct {
}
// EvalRead forwards evaluation to read policy.
func (policy Policy) EvalRead(ctx context.Context, v ent.Value) error {
func (policy Policy) EvalRead(ctx context.Context, v entv2.Value) error {
return policy.Read.EvalRead(ctx, v)
}
// EvalWrite forwards evaluation to write policy.
func (policy Policy) EvalWrite(ctx context.Context, m ent.Mutation) error {
func (policy Policy) EvalWrite(ctx context.Context, m entv2.Mutation) error {
return policy.Write.EvalWrite(ctx, m)
}
@@ -143,101 +143,101 @@ func AlwaysDenyRule() ReadWriteRule {
type fixedDecisionRule struct{ err error }
func (f fixedDecisionRule) EvalRead(context.Context, ent.Value) error { return f.err }
func (f fixedDecisionRule) EvalWrite(context.Context, ent.Mutation) error { return f.err }
func (f fixedDecisionRule) EvalRead(context.Context, entv2.Value) error { return f.err }
func (f fixedDecisionRule) EvalWrite(context.Context, entv2.Mutation) error { return f.err }
// The CarReadRuleFunc type is an adapter to allow the use of ordinary
// functions as a read rule.
type CarReadRuleFunc func(context.Context, *ent.Car) error
type CarReadRuleFunc func(context.Context, *entv2.Car) error
// EvalRead calls f(ctx, v).
func (f CarReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
if v, ok := v.(*ent.Car); ok {
func (f CarReadRuleFunc) EvalRead(ctx context.Context, v entv2.Value) error {
if v, ok := v.(*entv2.Car); ok {
return f(ctx, v)
}
return Denyf("ent/privacy: unexpected value type %T, expect *ent.Car", v)
return Denyf("ent/privacy: unexpected value type %T, expect *entv2.Car", v)
}
// The CarWriteRuleFunc type is an adapter to allow the use of ordinary
// functions as a write rule.
type CarWriteRuleFunc func(context.Context, *ent.CarMutation) error
type CarWriteRuleFunc func(context.Context, *entv2.CarMutation) error
// EvalWrite calls f(ctx, m).
func (f CarWriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.CarMutation); ok {
func (f CarWriteRuleFunc) EvalWrite(ctx context.Context, m entv2.Mutation) error {
if m, ok := m.(*entv2.CarMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CarMutation", m)
return Denyf("ent/privacy: unexpected mutation type %T, expect *entv2.CarMutation", m)
}
// The GroupReadRuleFunc type is an adapter to allow the use of ordinary
// functions as a read rule.
type GroupReadRuleFunc func(context.Context, *ent.Group) error
type GroupReadRuleFunc func(context.Context, *entv2.Group) error
// EvalRead calls f(ctx, v).
func (f GroupReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
if v, ok := v.(*ent.Group); ok {
func (f GroupReadRuleFunc) EvalRead(ctx context.Context, v entv2.Value) error {
if v, ok := v.(*entv2.Group); ok {
return f(ctx, v)
}
return Denyf("ent/privacy: unexpected value type %T, expect *ent.Group", v)
return Denyf("ent/privacy: unexpected value type %T, expect *entv2.Group", v)
}
// The GroupWriteRuleFunc type is an adapter to allow the use of ordinary
// functions as a write rule.
type GroupWriteRuleFunc func(context.Context, *ent.GroupMutation) error
type GroupWriteRuleFunc func(context.Context, *entv2.GroupMutation) error
// EvalWrite calls f(ctx, m).
func (f GroupWriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.GroupMutation); ok {
func (f GroupWriteRuleFunc) EvalWrite(ctx context.Context, m entv2.Mutation) error {
if m, ok := m.(*entv2.GroupMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m)
return Denyf("ent/privacy: unexpected mutation type %T, expect *entv2.GroupMutation", m)
}
// The PetReadRuleFunc type is an adapter to allow the use of ordinary
// functions as a read rule.
type PetReadRuleFunc func(context.Context, *ent.Pet) error
type PetReadRuleFunc func(context.Context, *entv2.Pet) error
// EvalRead calls f(ctx, v).
func (f PetReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
if v, ok := v.(*ent.Pet); ok {
func (f PetReadRuleFunc) EvalRead(ctx context.Context, v entv2.Value) error {
if v, ok := v.(*entv2.Pet); ok {
return f(ctx, v)
}
return Denyf("ent/privacy: unexpected value type %T, expect *ent.Pet", v)
return Denyf("ent/privacy: unexpected value type %T, expect *entv2.Pet", v)
}
// The PetWriteRuleFunc type is an adapter to allow the use of ordinary
// functions as a write rule.
type PetWriteRuleFunc func(context.Context, *ent.PetMutation) error
type PetWriteRuleFunc func(context.Context, *entv2.PetMutation) error
// EvalWrite calls f(ctx, m).
func (f PetWriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.PetMutation); ok {
func (f PetWriteRuleFunc) EvalWrite(ctx context.Context, m entv2.Mutation) error {
if m, ok := m.(*entv2.PetMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.PetMutation", m)
return Denyf("ent/privacy: unexpected mutation type %T, expect *entv2.PetMutation", m)
}
// The UserReadRuleFunc type is an adapter to allow the use of ordinary
// functions as a read rule.
type UserReadRuleFunc func(context.Context, *ent.User) error
type UserReadRuleFunc func(context.Context, *entv2.User) error
// EvalRead calls f(ctx, v).
func (f UserReadRuleFunc) EvalRead(ctx context.Context, v ent.Value) error {
if v, ok := v.(*ent.User); ok {
func (f UserReadRuleFunc) EvalRead(ctx context.Context, v entv2.Value) error {
if v, ok := v.(*entv2.User); ok {
return f(ctx, v)
}
return Denyf("ent/privacy: unexpected value type %T, expect *ent.User", v)
return Denyf("ent/privacy: unexpected value type %T, expect *entv2.User", v)
}
// The UserWriteRuleFunc type is an adapter to allow the use of ordinary
// functions as a write rule.
type UserWriteRuleFunc func(context.Context, *ent.UserMutation) error
type UserWriteRuleFunc func(context.Context, *entv2.UserMutation) error
// EvalWrite calls f(ctx, m).
func (f UserWriteRuleFunc) EvalWrite(ctx context.Context, m ent.Mutation) error {
if m, ok := m.(*ent.UserMutation); ok {
func (f UserWriteRuleFunc) EvalWrite(ctx context.Context, m entv2.Mutation) error {
if m, ok := m.(*entv2.UserMutation); ok {
return f(ctx, m)
}
return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m)
return Denyf("ent/privacy: unexpected mutation type %T, expect *entv2.UserMutation", m)
}

View File

@@ -17,6 +17,7 @@ import (
// to their package variables.
func init() {
userFields := schema.User{}.Fields()
_ = userFields
// userDescPhone is the schema descriptor for phone field.
userDescPhone := userFields[4].Descriptor()
// user.DefaultPhone holds the default value on creation for the phone field.

View File

@@ -32,6 +32,7 @@ func init() {
planet.Hooks[i+1] = h
}
planetFields := schema.Planet{}.Fields()
_ = planetFields
// planetDescName is the schema descriptor for name field.
planetDescName := planetFields[0].Descriptor()
// planet.NameValidator is a validator for the "name" field. It is called by the builders before save.

View File

@@ -19,11 +19,13 @@ import (
// to their package variables.
func init() {
groupFields := schema.Group{}.Fields()
_ = groupFields
// groupDescName is the schema descriptor for name field.
groupDescName := groupFields[0].Descriptor()
// group.NameValidator is a validator for the "name" field. It is called by the builders before save.
group.NameValidator = groupDescName.Validators[0].(func(string) error)
userFields := schema.User{}.Fields()
_ = userFields
// userDescAge is the schema descriptor for age field.
userDescAge := userFields[0].Descriptor()
// user.AgeValidator is a validator for the "age" field. It is called by the builders before save.