Files
ent/schema/field/gen/numeric.tmpl
Alex Snast 373769dfaf ent/gen: adding EqualFold predicate for string fields
Summary: Similar to `ContainsFold` predicate requires `--storage=sql` argument to be passed to entc.

Reviewed By: a8m

Differential Revision: D17074805

fbshipit-source-id: ced299154417fe2c9007cd6a7a504f53c8b2ef98
2019-08-27 10:48:53 -07:00

239 lines
6.9 KiB
Cheetah

{{ define "numeric" }}
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
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 }}