schema/field: add annotation option to schema field (#622)

This commit is contained in:
Ariel Mashraki
2020-07-19 18:01:04 +03:00
committed by GitHub
parent bdd80ebb3f
commit 54f0a6769b
16 changed files with 418 additions and 30 deletions

View File

@@ -266,6 +266,19 @@ func (b *stringBuilder) GoType(typ interface{}) *stringBuilder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.String("dir").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *stringBuilder) Annotations(annotations ...Annotation) *stringBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *stringBuilder) Descriptor() *Descriptor {
return b.desc
@@ -347,6 +360,19 @@ func (b *timeBuilder) GoType(typ interface{}) *timeBuilder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Time("deleted_at").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *timeBuilder) Annotations(annotations ...Annotation) *timeBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *timeBuilder) Descriptor() *Descriptor {
return b.desc
@@ -425,6 +451,19 @@ func (b *boolBuilder) GoType(typ interface{}) *boolBuilder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Bool("deleted").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *boolBuilder) Annotations(annotations ...Annotation) *boolBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *boolBuilder) Descriptor() *Descriptor {
return b.desc
@@ -462,7 +501,7 @@ func (b *bytesBuilder) Immutable() *bytesBuilder {
}
// Comment sets the comment of the field.
func (b *bytesBuilder) Comment(c string) *bytesBuilder {
func (b *bytesBuilder) Comment(string) *bytesBuilder {
return b
}
@@ -497,9 +536,17 @@ func (b *bytesBuilder) GoType(typ interface{}) *bytesBuilder {
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *bytesBuilder) Descriptor() *Descriptor {
return b.desc
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Bytes("ip").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *bytesBuilder) Annotations(annotations ...Annotation) *bytesBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// SchemaType overrides the default database type with a custom
@@ -516,6 +563,11 @@ func (b *bytesBuilder) SchemaType(types map[string]string) *bytesBuilder {
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *bytesBuilder) Descriptor() *Descriptor {
return b.desc
}
// jsonBuilder is the builder for json fields.
type jsonBuilder struct {
desc *Descriptor
@@ -566,6 +618,19 @@ func (b *jsonBuilder) SchemaType(types map[string]string) *jsonBuilder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.JSON("json").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *jsonBuilder) Annotations(annotations ...Annotation) *jsonBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *jsonBuilder) Descriptor() *Descriptor {
return b.desc
@@ -639,6 +704,19 @@ func (b *enumBuilder) SchemaType(types map[string]string) *enumBuilder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Enum("enum").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *enumBuilder) Annotations(annotations ...Annotation) *enumBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *enumBuilder) Descriptor() *Descriptor {
return b.desc
@@ -715,11 +793,32 @@ func (b *uuidBuilder) SchemaType(types map[string]string) *uuidBuilder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.UUID("id", uuid.New()).
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *uuidBuilder) Annotations(annotations ...Annotation) *uuidBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uuidBuilder) Descriptor() *Descriptor {
return b.desc
}
// Annotation is used to attach arbitrary metadata to the field object in codegen.
// The object must be serializable to JSON raw value (e.g. struct, map or slice).
// Template extensions can retrieve this metadata and use inside their templates.
type Annotation interface {
// Name defines the name of the annotation.
Name() string
}
// A Descriptor for field configuration.
type Descriptor struct {
Tag string // struct tag.
@@ -737,6 +836,7 @@ type Descriptor struct {
Enums []string // enum values.
Sensitive bool // sensitive info string field.
SchemaType map[string]string // override the schema type.
Annotations []Annotation // field annotations.
err error
}

View File

@@ -174,6 +174,19 @@ func (b *{{ $builder }}) GoType(typ interface{}) *{{ $builder }} {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.{{ $tt }}("{{ $t }}").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *{{ $builder }}) Annotations(annotations ...Annotation) *{{ $builder }} {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *{{ $builder }}) Descriptor() *Descriptor {
return b.desc
@@ -320,6 +333,19 @@ func (b *{{ $builder }}) GoType(typ interface{}) *{{ $builder }} {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.{{ $tt }}("{{ $t }}").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *{{ $builder }}) Annotations(annotations ...Annotation) *{{ $builder }} {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *{{ $builder }}) Descriptor() *Descriptor {
return b.desc

View File

@@ -239,6 +239,19 @@ func (b *intBuilder) GoType(typ interface{}) *intBuilder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Int("int").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *intBuilder) Annotations(annotations ...Annotation) *intBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *intBuilder) Descriptor() *Descriptor {
return b.desc
@@ -366,6 +379,19 @@ func (b *uintBuilder) GoType(typ interface{}) *uintBuilder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Uint("uint").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *uintBuilder) Annotations(annotations ...Annotation) *uintBuilder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uintBuilder) Descriptor() *Descriptor {
return b.desc
@@ -503,6 +529,19 @@ func (b *int8Builder) GoType(typ interface{}) *int8Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Int8("int8").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *int8Builder) Annotations(annotations ...Annotation) *int8Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int8Builder) Descriptor() *Descriptor {
return b.desc
@@ -640,6 +679,19 @@ func (b *int16Builder) GoType(typ interface{}) *int16Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Int16("int16").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *int16Builder) Annotations(annotations ...Annotation) *int16Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int16Builder) Descriptor() *Descriptor {
return b.desc
@@ -777,6 +829,19 @@ func (b *int32Builder) GoType(typ interface{}) *int32Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Int32("int32").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *int32Builder) Annotations(annotations ...Annotation) *int32Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int32Builder) Descriptor() *Descriptor {
return b.desc
@@ -914,6 +979,19 @@ func (b *int64Builder) GoType(typ interface{}) *int64Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Int64("int64").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *int64Builder) Annotations(annotations ...Annotation) *int64Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int64Builder) Descriptor() *Descriptor {
return b.desc
@@ -1041,6 +1119,19 @@ func (b *uint8Builder) GoType(typ interface{}) *uint8Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Uint8("uint8").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *uint8Builder) Annotations(annotations ...Annotation) *uint8Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint8Builder) Descriptor() *Descriptor {
return b.desc
@@ -1168,6 +1259,19 @@ func (b *uint16Builder) GoType(typ interface{}) *uint16Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Uint16("uint16").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *uint16Builder) Annotations(annotations ...Annotation) *uint16Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint16Builder) Descriptor() *Descriptor {
return b.desc
@@ -1295,6 +1399,19 @@ func (b *uint32Builder) GoType(typ interface{}) *uint32Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Uint32("uint32").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *uint32Builder) Annotations(annotations ...Annotation) *uint32Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint32Builder) Descriptor() *Descriptor {
return b.desc
@@ -1422,6 +1539,19 @@ func (b *uint64Builder) GoType(typ interface{}) *uint64Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Uint64("uint64").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *uint64Builder) Annotations(annotations ...Annotation) *uint64Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint64Builder) Descriptor() *Descriptor {
return b.desc
@@ -1568,6 +1698,19 @@ func (b *float64Builder) GoType(typ interface{}) *float64Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Float64("float64").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *float64Builder) Annotations(annotations ...Annotation) *float64Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *float64Builder) Descriptor() *Descriptor {
return b.desc
@@ -1701,6 +1844,19 @@ func (b *float32Builder) GoType(typ interface{}) *float32Builder {
return b
}
// Annotations adds a list of annotations to the field object to be used by
// codegen extensions.
//
// field.Float32("float32").
// Annotations(entgql.Config{
// Ordered: true,
// })
//
func (b *float32Builder) Annotations(annotations ...Annotation) *float32Builder {
b.desc.Annotations = append(b.desc.Annotations, annotations...)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *float32Builder) Descriptor() *Descriptor {
return b.desc