mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
Summary: wip Reviewed By: alexsn Differential Revision: D17052576 fbshipit-source-id: 165faca2ff02c22bb76f164d81d406ce46ab04c4
235 lines
6.7 KiB
Cheetah
235 lines
6.7 KiB
Cheetah
{{ define "numeric" }}
|
|
package field
|
|
|
|
import "errors"
|
|
|
|
//go:generate go run gen/gen.go
|
|
|
|
{{ range $_, $t := $.Ints }}
|
|
{{ $title := title $t.String }}
|
|
// {{ $title }} returns a new Field with type {{ $t }}.
|
|
func {{ $title }}(name string) *{{ $t }}Builder { return &{{ $t }}Builder{desc: &Descriptor{Type: Type{{ $title }}, Name: name}} }
|
|
{{ end }}
|
|
|
|
// Float returns a new Field with type float64.
|
|
func Float(name string) *float64Builder { return &float64Builder{desc: &Descriptor{Type: TypeFloat64, Name: name}} }
|
|
|
|
// Float32 returns a new Field with type float32.
|
|
func Float32(name string) *float32Builder { return &float32Builder{desc: &Descriptor{Type: TypeFloat32, Name: name}} }
|
|
|
|
{{ range $_, $t := $.Ints }}
|
|
{{ $builder := printf "%sBuilder" $t }}
|
|
|
|
// {{ $builder }} is the builder for {{ $t }} field.
|
|
type {{ $builder }} struct {
|
|
desc *Descriptor
|
|
}
|
|
|
|
// Unique makes the field unique within all vertices of this type.
|
|
func (b *{{ $builder }}) Unique() *{{ $builder }} {
|
|
b.desc.Unique = true
|
|
return b
|
|
}
|
|
|
|
// Range adds a range validator for this field where the given value needs to be in the range of [i, j].
|
|
func (b *{{ $builder }}) Range(i, j {{ $t }}) *{{ $builder }} {
|
|
b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error {
|
|
if v < i || v > j {
|
|
return errors.New("value out of range")
|
|
}
|
|
return nil
|
|
})
|
|
return b
|
|
}
|
|
|
|
// Min adds a minimum value validator for this field. Operation fails if the validator fails.
|
|
func (b *{{ $builder }}) Min(i {{ $t }}) *{{ $builder }} {
|
|
b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error {
|
|
if v < i {
|
|
return errors.New("value out of range")
|
|
}
|
|
return nil
|
|
})
|
|
return b
|
|
}
|
|
|
|
// Max adds a maximum value validator for this field. Operation fails if the validator fails.
|
|
func (b *{{ $builder }}) Max(i {{ $t }}) *{{ $builder }} {
|
|
b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error {
|
|
if v > i {
|
|
return errors.New("value out of range")
|
|
}
|
|
return nil
|
|
})
|
|
return b
|
|
}
|
|
|
|
// Positive adds a minimum value validator with the value of 1. Operation fails if the validator fails.
|
|
func (b *{{ $builder }}) Positive() *{{ $builder }} {
|
|
return b.Min(1)
|
|
}
|
|
|
|
{{ if hasPrefix $t.String "uint" | not }}
|
|
// Negative adds a maximum value validator with the value of -1. Operation fails if the validator fails.
|
|
func (b *{{ $builder }}) Negative() *{{ $builder }} {
|
|
return b.Max(-1)
|
|
}
|
|
{{ end }}
|
|
|
|
// Default sets the default value of the field.
|
|
func (b *{{ $builder }}) Default(i {{ $t }}) *{{ $builder }} {
|
|
b.desc.Default = i
|
|
return b
|
|
}
|
|
|
|
// Nillable indicates that this field is a nillable.
|
|
// Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field.
|
|
func (b *{{ $builder }}) Nillable() *{{ $builder }} {
|
|
b.desc.Nillable = true
|
|
return b
|
|
}
|
|
|
|
// Comment sets the comment of the field.
|
|
func (b *{{ $builder }}) Comment(c string) *{{ $builder }} {
|
|
return b
|
|
}
|
|
|
|
// Optional indicates that this field is optional on create.
|
|
// Unlike edges, fields are required by default.
|
|
func (b *{{ $builder }}) Optional() *{{ $builder }} {
|
|
b.desc.Optional = true
|
|
return b
|
|
}
|
|
|
|
// Immutable indicates that this field cannot be updated.
|
|
func (b *{{ $builder }}) Immutable() *{{ $builder }} {
|
|
b.desc.Immutable = true
|
|
return b
|
|
}
|
|
|
|
// StructTag sets the struct tag of the field.
|
|
func (b *{{ $builder }}) StructTag(s string) *{{ $builder }} {
|
|
b.desc.Tag = s
|
|
return b
|
|
}
|
|
|
|
// Validate adds a validator for this field. Operation fails if the validation fails.
|
|
func (b *{{ $builder }}) Validate(fn func({{ $t }}) error) *{{ $builder }} {
|
|
b.desc.Validators = append(b.desc.Validators, fn)
|
|
return b
|
|
}
|
|
|
|
// Descriptor implements the ent.Field interface by returning its descriptor.
|
|
func (b *{{ $builder }}) Descriptor() *Descriptor {
|
|
return b.desc
|
|
}
|
|
|
|
{{ end }}
|
|
|
|
{{ range $_, $t := $.Floats }}
|
|
{{ $builder := printf "%sBuilder" $t }}
|
|
|
|
// {{ $builder }} is the builder for float fields.
|
|
type {{ $builder }} struct {
|
|
desc *Descriptor
|
|
}
|
|
|
|
// Unique makes the field unique within all vertices of this type.
|
|
func (b *{{ $builder }}) Unique() *{{ $builder }} {
|
|
b.desc.Unique = true
|
|
return b
|
|
}
|
|
|
|
// Range adds a range validator for this field where the given value needs to be in the range of [i, j].
|
|
func (b *{{ $builder }}) Range(i, j {{ $t }}) *{{ $builder }} {
|
|
b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error {
|
|
if v < i || v > j {
|
|
return errors.New("value out of range")
|
|
}
|
|
return nil
|
|
})
|
|
return b
|
|
}
|
|
|
|
// Min adds a minimum value validator for this field. Operation fails if the validator fails.
|
|
func (b *{{ $builder }}) Min(i {{ $t }}) *{{ $builder }} {
|
|
b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error {
|
|
if v < i {
|
|
return errors.New("value out of range")
|
|
}
|
|
return nil
|
|
})
|
|
return b
|
|
}
|
|
|
|
// Max adds a maximum value validator for this field. Operation fails if the validator fails.
|
|
func (b *{{ $builder }}) Max(i {{ $t }}) *{{ $builder }} {
|
|
b.desc.Validators = append(b.desc.Validators, func(v {{ $t }}) error {
|
|
if v > i {
|
|
return errors.New("value out of range")
|
|
}
|
|
return nil
|
|
})
|
|
return b
|
|
}
|
|
|
|
// Positive adds a minimum value validator with the value of 0.000001. Operation fails if the validator fails.
|
|
func (b *{{ $builder }}) Positive() *{{ $builder }} {
|
|
return b.Min(1e-06)
|
|
}
|
|
|
|
// Negative adds a maximum value validator with the value of -0.000001. Operation fails if the validator fails.
|
|
func (b *{{ $builder }}) Negative() *{{ $builder }} {
|
|
return b.Max(-1e-06)
|
|
}
|
|
|
|
// Default sets the default value of the field.
|
|
func (b *{{ $builder }}) Default(i {{ $t }}) *{{ $builder }} {
|
|
b.desc.Default = i
|
|
return b
|
|
}
|
|
|
|
// Nillable indicates that this field is a nillable.
|
|
// Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field.
|
|
func (b *{{ $builder }}) Nillable() *{{ $builder }} {
|
|
b.desc.Nillable = true
|
|
return b
|
|
}
|
|
|
|
// Comment sets the comment of the field.
|
|
func (b *{{ $builder }}) Comment(c string) *{{ $builder }} {
|
|
return b
|
|
}
|
|
|
|
// Optional indicates that this field is optional on create.
|
|
// Unlike edges, fields are required by default.
|
|
func (b *{{ $builder }}) Optional() *{{ $builder }} {
|
|
b.desc.Optional = true
|
|
return b
|
|
}
|
|
|
|
// Immutable indicates that this field cannot be updated.
|
|
func (b *{{ $builder }}) Immutable() *{{ $builder }} {
|
|
b.desc.Immutable = true
|
|
return b
|
|
}
|
|
|
|
// StructTag sets the struct tag of the field.
|
|
func (b *{{ $builder }}) StructTag(s string) *{{ $builder }} {
|
|
b.desc.Tag = s
|
|
return b
|
|
}
|
|
|
|
// Validate adds a validator for this field. Operation fails if the validation fails.
|
|
func (b *{{ $builder }}) Validate(fn func({{ $t }}) error) *{{ $builder }} {
|
|
b.desc.Validators = append(b.desc.Validators, fn)
|
|
return b
|
|
}
|
|
|
|
// Descriptor implements the ent.Field interface by returning its descriptor.
|
|
func (b *{{ $builder }}) Descriptor() *Descriptor {
|
|
return b.desc
|
|
}
|
|
{{ end }}
|
|
|
|
{{ end }} |