diff --git a/field.go b/field.go index 229716a..1802c8c 100644 --- a/field.go +++ b/field.go @@ -5,7 +5,6 @@ package ens import ( "fmt" - "github.com/things-go/ens/internal/sqlx" "github.com/things-go/ens/matcher" "gorm.io/plugin/soft_delete" ) @@ -86,83 +85,3 @@ func (field *FieldDescriptor) build(opt *Option) { } } } - -// Field returns a new Field with the type. -func Field(t *GoType, name string) *fieldBuilder { - return &fieldBuilder{ - inner: &FieldDescriptor{ - Name: name, - Type: t, - }, - } -} - -// FieldFromDef returns a new Field with the type and ColumnDef. -// auto set name, comment, nullable, column and optional. -func FieldFromDef(t *GoType, def ColumnDef) *fieldBuilder { - col := def.Column() - return &fieldBuilder{ - inner: &FieldDescriptor{ - Name: col.Name, - Comment: sqlx.MustComment(col.Attrs), - Nullable: col.Type.Null, - Column: def, - Type: t, - Optional: col.Type.Null, - }, - } -} - -var _ Fielder = (*fieldBuilder)(nil) - -// fieldBuilder is the builder for field. -type fieldBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *fieldBuilder) Comment(c string) *fieldBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *fieldBuilder) Nullable() *fieldBuilder { - b.inner.Nullable = true - return b -} - -// Column the column expression of the field. -func (b *fieldBuilder) Column(e ColumnDef) *fieldBuilder { - b.inner.Column = e - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Bool("deleted"). -// GoType(sql.NullBool{}) -func (b *fieldBuilder) GoType(typ any) *fieldBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *fieldBuilder) Optional() *fieldBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -func (b *fieldBuilder) Tags(tags ...string) *fieldBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *fieldBuilder) Build(opt *Option) *FieldDescriptor { - b.inner.build(opt) - return b.inner -} diff --git a/field_bool.go b/field_bool.go deleted file mode 100644 index 04cf370..0000000 --- a/field_bool.go +++ /dev/null @@ -1,68 +0,0 @@ -package ens - -import ( - "reflect" -) - -var _ Fielder = (*boolBuilder)(nil) -var boolType = reflect.TypeOf(false) - -// Bool returns a new Field with type bool. -func Bool(name string) *boolBuilder { - return &boolBuilder{ - &FieldDescriptor{ - Name: name, - Type: BoolType(), - }, - } -} - -// boolBuilder is the builder for boolean fields. -type boolBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *boolBuilder) Comment(c string) *boolBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *boolBuilder) Nullable() *boolBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Bool("deleted"). -// GoType(sql.NullBool{}) -func (b *boolBuilder) GoType(typ any) *boolBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *boolBuilder) Optional() *boolBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of Tags to the field object. -// -// field.String("dir"). -// Tags("yaml:"xxx"") -func (b *boolBuilder) Tags(tags ...string) *boolBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Field interface by returning its descriptor. -func (b *boolBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(boolType) - b.inner.build(opt) - return b.inner -} diff --git a/field_builder.go b/field_builder.go new file mode 100644 index 0000000..fe7f5f0 --- /dev/null +++ b/field_builder.go @@ -0,0 +1,111 @@ +package ens + +import ( + "database/sql/driver" + + "github.com/things-go/ens/internal/sqlx" +) + +func Bool(name string) *FieldBuilder { return Field(BoolType(), name) } +func Int(name string) *FieldBuilder { return Field(IntType(), name) } +func Int8(name string) *FieldBuilder { return Field(Int8Type(), name) } +func Int16(name string) *FieldBuilder { return Field(Int16Type(), name) } +func Int32(name string) *FieldBuilder { return Field(Int32Type(), name) } +func Int64(name string) *FieldBuilder { return Field(Int64Type(), name) } +func Uint(name string) *FieldBuilder { return Field(UintType(), name) } +func Uint8(name string) *FieldBuilder { return Field(Uint8Type(), name) } +func Uint16(name string) *FieldBuilder { return Field(Uint16Type(), name) } +func Uint32(name string) *FieldBuilder { return Field(Uint32Type(), name) } +func Uint64(name string) *FieldBuilder { return Field(Uint64Type(), name) } +func Float32(name string) *FieldBuilder { return Field(Float32Type(), name) } +func Float64(name string) *FieldBuilder { return Field(Float64Type(), name) } +func String(name string) *FieldBuilder { return Field(StringType(), name) } +func Bytes(name string) *FieldBuilder { return Field(BytesType(), name) } +func JSON(name string) *FieldBuilder { return Field(JSONRawMessageType(), name) } +func Enum(name string) *FieldBuilder { return Field(EnumType(), name) } +func Time(name string) *FieldBuilder { return Field(TimeType(), name) } +func UUID(name string, typ driver.Valuer) *FieldBuilder { return Field(NewGoType(TypeUUID, typ), name) } + +var _ Fielder = (*FieldBuilder)(nil) + +// FieldBuilder is the builder for field. +type FieldBuilder struct { + inner *FieldDescriptor +} + +// Field returns a new Field with the type. +func Field(t *GoType, name string) *FieldBuilder { + return &FieldBuilder{ + inner: &FieldDescriptor{ + Name: name, + Type: t, + }, + } +} + +// FieldFromDef returns a new Field with the type and ColumnDef. +// auto set name, comment, nullable, column and optional. +func FieldFromDef(t *GoType, def ColumnDef) *FieldBuilder { + col := def.Column() + return &FieldBuilder{ + inner: &FieldDescriptor{ + Name: col.Name, + Comment: sqlx.MustComment(col.Attrs), + Nullable: col.Type.Null, + Column: def, + Type: t, + Optional: col.Type.Null, + }, + } +} + +// Column the column expression of the field. +func (b *FieldBuilder) Column(e ColumnDef) *FieldBuilder { + b.inner.Column = e + return b +} + +// Comment sets the comment of the field. +func (b *FieldBuilder) Comment(c string) *FieldBuilder { + b.inner.Comment = c + return b +} + +// Nullable indicates that this field is a nullable. +func (b *FieldBuilder) Nullable() *FieldBuilder { + b.inner.Nullable = true + return b +} + +// GoType overrides the default Go type with a custom one. +// +// field.Bool("deleted"). +// GoType(sql.NullBool{}) +// field.Bytes("ip"). +// GoType(net.IP("127.0.0.1")) +// field.String("dir"). +// GoType(http.Dir("dir")) +func (b *FieldBuilder) GoType(typ any) *FieldBuilder { + b.inner.goType(typ) + return b +} + +// Optional indicates that this field is optional. +// Unlike "Nullable" only fields, +// "Optional" fields are pointers in the generated struct. +func (b *FieldBuilder) Optional() *FieldBuilder { + b.inner.Optional = true + return b +} + +// Tags adds a list of tags to the field tag. +func (b *FieldBuilder) Tags(tags ...string) *FieldBuilder { + b.inner.Tags = append(b.inner.Tags, tags...) + return b +} + +// Build implements the Fielder interface by returning its descriptor. +func (b *FieldBuilder) Build(opt *Option) *FieldDescriptor { + b.inner.build(opt) + return b.inner +} diff --git a/field_bytes.go b/field_bytes.go deleted file mode 100644 index 2fb8425..0000000 --- a/field_bytes.go +++ /dev/null @@ -1,68 +0,0 @@ -package ens - -import ( - "reflect" -) - -var _ Fielder = (*bytesBuilder)(nil) -var bytesType = reflect.TypeOf([]byte(nil)) - -// Bytes returns a new Field with type bytes/buffer. -// In MySQL and SQLite, it is the "BLOB" type, and it does not support for Gremlin. -func Bytes(name string) *bytesBuilder { - return &bytesBuilder{ - &FieldDescriptor{ - Name: name, - Type: BytesType(), - }, - } -} - -// bytesBuilder is the builder for bytes fields. -type bytesBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *bytesBuilder) Comment(c string) *bytesBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *bytesBuilder) Nullable() *bytesBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Bytes("ip"). -// GoType(net.IP("127.0.0.1")) -func (b *bytesBuilder) GoType(typ any) *bytesBuilder { - // b.desc.goType(typ) - return b -} - -// Optional indicates that this field is optional on create. -// Unlike edges, fields are required by default. -func (b *bytesBuilder) Optional() *bytesBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of Tags to the field object. -// -// field.String("dir"). -// Tags("yaml:"xxx"") -func (b *bytesBuilder) Tags(tags ...string) *bytesBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Field interface by returning its descriptor. -func (b *bytesBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(bytesType) - b.inner.build(opt) - return b.inner -} diff --git a/field_enum.go b/field_enum.go deleted file mode 100644 index 5ca1517..0000000 --- a/field_enum.go +++ /dev/null @@ -1,61 +0,0 @@ -package ens - -// Enum returns a new Field with type enum. -func Enum(name string) *enumBuilder { - return &enumBuilder{ - &FieldDescriptor{ - Name: name, - Type: EnumType(), - }, - } -} - -// enumBuilder is the builder for enum fields. -type enumBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *enumBuilder) Comment(c string) *enumBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *enumBuilder) Nullable() *enumBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Bool("deleted"). -// GoType(sql.NullBool{}) -func (b *enumBuilder) GoType(typ any) *enumBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *enumBuilder) Optional() *enumBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of Tags to the field object. -// -// field.String("dir"). -// Tags("yaml:"xxx"") -func (b *enumBuilder) Tags(tags ...string) *enumBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Field interface by returning its descriptor. -func (b *enumBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(stringType) - b.inner.build(opt) - return b.inner -} diff --git a/field_float32.go b/field_float32.go deleted file mode 100644 index f638ac8..0000000 --- a/field_float32.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/float.tmpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*float32Builder)(nil) -var float32Type = reflect.TypeOf(float32(0)) - -// Float32 returns a new Field with type float32. -func Float32(name string) *float32Builder { - return &float32Builder{ - &FieldDescriptor{ - Name: name, - Type: Float32Type(), - }, - } -} - -// float32Builder is the builder for float32 fields. -type float32Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *float32Builder) Comment(c string) *float32Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *float32Builder) Nullable() *float32Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Float32("float32"). -// GoType(pkg.Float32(0)) -func (b *float32Builder) GoType(typ any) *float32Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *float32Builder) Optional() *float32Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Float32("float32"). -// Tags("yaml:"xxx"") -func (b *float32Builder) Tags(tags ...string) *float32Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *float32Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(float32Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_float64.go b/field_float64.go deleted file mode 100644 index 969b157..0000000 --- a/field_float64.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/float.tmpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*float64Builder)(nil) -var float64Type = reflect.TypeOf(float64(0)) - -// Float64 returns a new Field with type float64. -func Float64(name string) *float64Builder { - return &float64Builder{ - &FieldDescriptor{ - Name: name, - Type: Float64Type(), - }, - } -} - -// float64Builder is the builder for float64 fields. -type float64Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *float64Builder) Comment(c string) *float64Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *float64Builder) Nullable() *float64Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Float64("float64"). -// GoType(pkg.Float64(0)) -func (b *float64Builder) GoType(typ any) *float64Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *float64Builder) Optional() *float64Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Float64("float64"). -// Tags("yaml:"xxx"") -func (b *float64Builder) Tags(tags ...string) *float64Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *float64Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(float64Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_int.go b/field_int.go deleted file mode 100644 index 588b75e..0000000 --- a/field_int.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*intBuilder)(nil) -var intType = reflect.TypeOf(int(0)) - -// Int returns a new Field with type int. -func Int(name string) *intBuilder { - return &intBuilder{ - &FieldDescriptor{ - Name: name, - Type: IntType(), - }, - } -} - -// intBuilder is the builder for int field. -type intBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *intBuilder) Comment(c string) *intBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *intBuilder) Nullable() *intBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Int("int"). -// GoType(pkg.Int(0)) -func (b *intBuilder) GoType(typ any) *intBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *intBuilder) Optional() *intBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Int("int"). -// Tags("yaml:"xxx"") -func (b *intBuilder) Tags(tags ...string) *intBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *intBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(intType) - b.inner.build(opt) - return b.inner -} diff --git a/field_int16.go b/field_int16.go deleted file mode 100644 index e166ad7..0000000 --- a/field_int16.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*int16Builder)(nil) -var int16Type = reflect.TypeOf(int16(0)) - -// Int16 returns a new Field with type int16. -func Int16(name string) *int16Builder { - return &int16Builder{ - &FieldDescriptor{ - Name: name, - Type: Int16Type(), - }, - } -} - -// int16Builder is the builder for int16 field. -type int16Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *int16Builder) Comment(c string) *int16Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *int16Builder) Nullable() *int16Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Int16("int16"). -// GoType(pkg.Int16(0)) -func (b *int16Builder) GoType(typ any) *int16Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *int16Builder) Optional() *int16Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Int16("int16"). -// Tags("yaml:"xxx"") -func (b *int16Builder) Tags(tags ...string) *int16Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *int16Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(int16Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_int32.go b/field_int32.go deleted file mode 100644 index b29fc46..0000000 --- a/field_int32.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*int32Builder)(nil) -var int32Type = reflect.TypeOf(int32(0)) - -// Int32 returns a new Field with type int32. -func Int32(name string) *int32Builder { - return &int32Builder{ - &FieldDescriptor{ - Name: name, - Type: Int32Type(), - }, - } -} - -// int32Builder is the builder for int32 field. -type int32Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *int32Builder) Comment(c string) *int32Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *int32Builder) Nullable() *int32Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Int32("int32"). -// GoType(pkg.Int32(0)) -func (b *int32Builder) GoType(typ any) *int32Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *int32Builder) Optional() *int32Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Int32("int32"). -// Tags("yaml:"xxx"") -func (b *int32Builder) Tags(tags ...string) *int32Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *int32Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(int32Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_int64.go b/field_int64.go deleted file mode 100644 index 74ba3aa..0000000 --- a/field_int64.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*int64Builder)(nil) -var int64Type = reflect.TypeOf(int64(0)) - -// Int64 returns a new Field with type int64. -func Int64(name string) *int64Builder { - return &int64Builder{ - &FieldDescriptor{ - Name: name, - Type: Int64Type(), - }, - } -} - -// int64Builder is the builder for int64 field. -type int64Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *int64Builder) Comment(c string) *int64Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *int64Builder) Nullable() *int64Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Int64("int64"). -// GoType(pkg.Int64(0)) -func (b *int64Builder) GoType(typ any) *int64Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *int64Builder) Optional() *int64Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Int64("int64"). -// Tags("yaml:"xxx"") -func (b *int64Builder) Tags(tags ...string) *int64Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *int64Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(int64Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_int8.go b/field_int8.go deleted file mode 100644 index 1fcb856..0000000 --- a/field_int8.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*int8Builder)(nil) -var int8Type = reflect.TypeOf(int8(0)) - -// Int8 returns a new Field with type int8. -func Int8(name string) *int8Builder { - return &int8Builder{ - &FieldDescriptor{ - Name: name, - Type: Int8Type(), - }, - } -} - -// int8Builder is the builder for int8 field. -type int8Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *int8Builder) Comment(c string) *int8Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *int8Builder) Nullable() *int8Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Int8("int8"). -// GoType(pkg.Int8(0)) -func (b *int8Builder) GoType(typ any) *int8Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *int8Builder) Optional() *int8Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Int8("int8"). -// Tags("yaml:"xxx"") -func (b *int8Builder) Tags(tags ...string) *int8Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *int8Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(int8Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_json.go b/field_json.go deleted file mode 100644 index bc903c8..0000000 --- a/field_json.go +++ /dev/null @@ -1,63 +0,0 @@ -package ens - -var _ Fielder = (*jsonBuilder)(nil) - -// JSON returns a new Field with type json that is serialized to the given object. -func JSON(name string) *jsonBuilder { - return &jsonBuilder{ - &FieldDescriptor{ - Name: name, - Type: JSONRawMessageType(), - }, - } -} - -// jsonBuilder is the builder for json fields. -type jsonBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *jsonBuilder) Comment(c string) *jsonBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *jsonBuilder) Nullable() *jsonBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.String("dir"). -// GoType(http.Dir("dir")) -func (b *jsonBuilder) GoType(typ any) *jsonBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *jsonBuilder) Optional() *jsonBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of Tags to the field object. -// -// field.String("dir"). -// Tags("yaml:"xxx"") -func (b *jsonBuilder) Tags(tags ...string) *jsonBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Field interface by returning its descriptor. -func (b *jsonBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(stringType) - b.inner.build(opt) - return b.inner -} diff --git a/field_slice.go b/field_slice.go deleted file mode 100644 index c992cdc..0000000 --- a/field_slice.go +++ /dev/null @@ -1,94 +0,0 @@ -package ens - -import ( - "reflect" -) - -// Strings returns a new JSON Field with type []string. -func Strings(name string) *sliceBuilder[string] { - return sb[string](name) -} - -// Ints returns a new JSON Field with type []int. -func Ints(name string) *sliceBuilder[int] { - return sb[int](name) -} - -// Floats returns a new JSON Field with type []float. -func Floats(name string) *sliceBuilder[float64] { - return sb[float64](name) -} - -type sliceType interface { - int | string | float64 -} - -// sliceBuilder is the builder for string slice fields. -type sliceBuilder[T sliceType] struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *sliceBuilder[T]) Comment(c string) *sliceBuilder[T] { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *sliceBuilder[T]) Nullable() *sliceBuilder[T] { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -func (b *sliceBuilder[T]) GoType(typ any) *sliceBuilder[T] { - return b -} - -// Optional indicates that this field is optional on create. -// Unlike edges, fields are required by default. -func (b *sliceBuilder[T]) Optional() *sliceBuilder[T] { - b.inner.Optional = true - return b -} - -// Tags adds a list of Tags to the field object. -// -// field.String("dir"). -// Tags("yaml:"xxx"") -func (b *sliceBuilder[T]) Tags(tags ...string) *sliceBuilder[T] { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Field interface by returning its descriptor. -func (b *sliceBuilder[T]) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(stringType) - b.inner.build(opt) - return b.inner -} - -// sb is a generic helper method to share code between Strings, Ints and Floats builder. -func sb[T sliceType](name string) *sliceBuilder[T] { - var typ []T - b := &FieldDescriptor{ - Name: name, - Type: &GoType{ - Type: TypeJSON, - }, - } - t := reflect.TypeOf(typ) - if t == nil { - return &sliceBuilder[T]{b} - } - b.Type.Ident = t.String() - b.Type.PkgPath = t.PkgPath() - // b.desc.goType(typ) - // b.desc.checkGoType(t) - switch t.Kind() { - case reflect.Slice, reflect.Array, reflect.Ptr, reflect.Map: - b.Type.Nullable = true - b.Type.PkgPath = pkgPath(t) - } - return &sliceBuilder[T]{b} -} diff --git a/field_string.go b/field_string.go deleted file mode 100644 index 589b89f..0000000 --- a/field_string.go +++ /dev/null @@ -1,80 +0,0 @@ -package ens - -import ( - "reflect" -) - -var _ Fielder = (*stringBuilder)(nil) -var stringType = reflect.TypeOf("") - -// String returns a new Field with type string. -// limitation on the size 255. -func String(name string) *stringBuilder { - return &stringBuilder{ - &FieldDescriptor{ - Name: name, - Type: StringType(), - }, - } -} - -// Decimal returns a new Field with type decimal. -// limitation on the size 255. -func Decimal(name string) *stringBuilder { - return &stringBuilder{ - &FieldDescriptor{ - Name: name, - Type: DecimalType(), - }, - } -} - -// stringBuilder is the builder for string fields. -type stringBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *stringBuilder) Comment(c string) *stringBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *stringBuilder) Nullable() *stringBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.String("dir"). -// GoType(http.Dir("dir")) -func (b *stringBuilder) GoType(typ any) *stringBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *stringBuilder) Optional() *stringBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of Tags to the field object. -// -// field.String("dir"). -// Tags("yaml:"xxx"") -func (b *stringBuilder) Tags(tags ...string) *stringBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Field interface by returning its descriptor. -func (b *stringBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(stringType) - b.inner.build(opt) - return b.inner -} diff --git a/field_time.go b/field_time.go deleted file mode 100644 index f907c03..0000000 --- a/field_time.go +++ /dev/null @@ -1,69 +0,0 @@ -package ens - -import ( - "reflect" - "time" -) - -var _ Fielder = (*timeBuilder)(nil) -var timeType = reflect.TypeOf(time.Time{}) - -// Time returns a new Field with type timestamp. -func Time(name string) *timeBuilder { - return &timeBuilder{ - &FieldDescriptor{ - Name: name, - Type: TimeType(), - }, - } -} - -// timeBuilder is the builder for time fields. -type timeBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *timeBuilder) Comment(c string) *timeBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *timeBuilder) Nullable() *timeBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.String("dir"). -// GoType(http.Dir("dir")) -func (b *timeBuilder) GoType(typ any) *timeBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *timeBuilder) Optional() *timeBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of Tags to the field object. -// -// field.String("dir"). -// Tags("yaml:"xxx"") -func (b *timeBuilder) Tags(tags ...string) *timeBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Field interface by returning its descriptor. -func (b *timeBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(timeType) - b.inner.build(opt) - return b.inner -} diff --git a/field_uint.go b/field_uint.go deleted file mode 100644 index c2bb233..0000000 --- a/field_uint.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*uintBuilder)(nil) -var uintType = reflect.TypeOf(uint(0)) - -// Uint returns a new Field with type uint. -func Uint(name string) *uintBuilder { - return &uintBuilder{ - &FieldDescriptor{ - Name: name, - Type: UintType(), - }, - } -} - -// uintBuilder is the builder for uint field. -type uintBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *uintBuilder) Comment(c string) *uintBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *uintBuilder) Nullable() *uintBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Uint("uint"). -// GoType(pkg.Uint(0)) -func (b *uintBuilder) GoType(typ any) *uintBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *uintBuilder) Optional() *uintBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Uint("uint"). -// Tags("yaml:"xxx"") -func (b *uintBuilder) Tags(tags ...string) *uintBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *uintBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(uintType) - b.inner.build(opt) - return b.inner -} diff --git a/field_uint16.go b/field_uint16.go deleted file mode 100644 index decfe13..0000000 --- a/field_uint16.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*uint16Builder)(nil) -var uint16Type = reflect.TypeOf(uint16(0)) - -// Uint16 returns a new Field with type uint16. -func Uint16(name string) *uint16Builder { - return &uint16Builder{ - &FieldDescriptor{ - Name: name, - Type: Uint16Type(), - }, - } -} - -// uint16Builder is the builder for uint16 field. -type uint16Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *uint16Builder) Comment(c string) *uint16Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *uint16Builder) Nullable() *uint16Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Uint16("uint16"). -// GoType(pkg.Uint16(0)) -func (b *uint16Builder) GoType(typ any) *uint16Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *uint16Builder) Optional() *uint16Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Uint16("uint16"). -// Tags("yaml:"xxx"") -func (b *uint16Builder) Tags(tags ...string) *uint16Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *uint16Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(uint16Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_uint32.go b/field_uint32.go deleted file mode 100644 index e8d1701..0000000 --- a/field_uint32.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*uint32Builder)(nil) -var uint32Type = reflect.TypeOf(uint32(0)) - -// Uint32 returns a new Field with type uint32. -func Uint32(name string) *uint32Builder { - return &uint32Builder{ - &FieldDescriptor{ - Name: name, - Type: Uint32Type(), - }, - } -} - -// uint32Builder is the builder for uint32 field. -type uint32Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *uint32Builder) Comment(c string) *uint32Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *uint32Builder) Nullable() *uint32Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Uint32("uint32"). -// GoType(pkg.Uint32(0)) -func (b *uint32Builder) GoType(typ any) *uint32Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *uint32Builder) Optional() *uint32Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Uint32("uint32"). -// Tags("yaml:"xxx"") -func (b *uint32Builder) Tags(tags ...string) *uint32Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *uint32Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(uint32Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_uint64.go b/field_uint64.go deleted file mode 100644 index 9b2aefc..0000000 --- a/field_uint64.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*uint64Builder)(nil) -var uint64Type = reflect.TypeOf(uint64(0)) - -// Uint64 returns a new Field with type uint64. -func Uint64(name string) *uint64Builder { - return &uint64Builder{ - &FieldDescriptor{ - Name: name, - Type: Uint64Type(), - }, - } -} - -// uint64Builder is the builder for uint64 field. -type uint64Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *uint64Builder) Comment(c string) *uint64Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *uint64Builder) Nullable() *uint64Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Uint64("uint64"). -// GoType(pkg.Uint64(0)) -func (b *uint64Builder) GoType(typ any) *uint64Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *uint64Builder) Optional() *uint64Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Uint64("uint64"). -// Tags("yaml:"xxx"") -func (b *uint64Builder) Tags(tags ...string) *uint64Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *uint64Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(uint64Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_uint8.go b/field_uint8.go deleted file mode 100644 index f18504a..0000000 --- a/field_uint8.go +++ /dev/null @@ -1,70 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -var _ Fielder = (*uint8Builder)(nil) -var uint8Type = reflect.TypeOf(uint8(0)) - -// Uint8 returns a new Field with type uint8. -func Uint8(name string) *uint8Builder { - return &uint8Builder{ - &FieldDescriptor{ - Name: name, - Type: Uint8Type(), - }, - } -} - -// uint8Builder is the builder for uint8 field. -type uint8Builder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *uint8Builder) Comment(c string) *uint8Builder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *uint8Builder) Nullable() *uint8Builder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.Uint8("uint8"). -// GoType(pkg.Uint8(0)) -func (b *uint8Builder) GoType(typ any) *uint8Builder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *uint8Builder) Optional() *uint8Builder { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.Uint8("uint8"). -// Tags("yaml:"xxx"") -func (b *uint8Builder) Tags(tags ...string) *uint8Builder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *uint8Builder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(uint8Type) - b.inner.build(opt) - return b.inner -} diff --git a/field_uuid.go b/field_uuid.go deleted file mode 100644 index 84ab931..0000000 --- a/field_uuid.go +++ /dev/null @@ -1,66 +0,0 @@ -package ens - -import ( - "database/sql/driver" -) - -var _ Fielder = (*uuidBuilder)(nil) - -// UUID returns a new Field with type UUID. -func UUID(name string, typ driver.Valuer) *uuidBuilder { - return &uuidBuilder{ - &FieldDescriptor{ - Name: name, - Type: NewGoType(TypeUUID, typ), - }} -} - -// uuidBuilder is the builder for uuid fields. -type uuidBuilder struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *uuidBuilder) Comment(c string) *uuidBuilder { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *uuidBuilder) Nullable() *uuidBuilder { - b.inner.Nullable = true - return b -} - -// GoType overrides the default Go type with a custom one. -// -// field.String("dir"). -// GoType(http.Dir("dir")) -func (b *uuidBuilder) GoType(typ any) *uuidBuilder { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *uuidBuilder) Optional() *uuidBuilder { - b.inner.Optional = true - return b -} - -// Tags adds a list of Tags to the field object. -// -// field.String("dir"). -// Tags("yaml:"xxx"") -func (b *uuidBuilder) Tags(tags ...string) *uuidBuilder { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Field interface by returning its descriptor. -func (b *uuidBuilder) Build(opt *Option) *FieldDescriptor { - // b.inner.checkGoType(stringType) - b.inner.build(opt) - return b.inner -} diff --git a/internal/float.tpl b/internal/float.tpl deleted file mode 100644 index da44819..0000000 --- a/internal/float.tpl +++ /dev/null @@ -1,78 +0,0 @@ -// Code generated by internal/float.tmpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -{{ $t := $.Kind }} - -var _ Fielder = (*{{ $t }}Builder)(nil) -var {{ $t }}Type = reflect.TypeOf({{ $t }}(0)) - -{{ $title := title $t.String }} - -// {{ $title }} returns a new Field with type {{ $t }}. -func {{ $title }}(name string) *{{ $t }}Builder { - return &{{ $t }}Builder{ - &FieldDescriptor{ - Name: name, - Type: {{ $title }}Type(), - }, - } -} - -{{ $builder := printf "%sBuilder" $t }} - -// {{ $builder }} is the builder for {{ $t }} fields. -type {{ $builder }} struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *{{ $builder }}) Comment(c string) *{{ $builder }} { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *{{ $builder }}) Nullable() *{{ $builder }} { - b.inner.Nullable = true - return b -} - -{{ $tt := title $t.String }} -// GoType overrides the default Go type with a custom one. -// -// field.{{ $tt }}("{{ $t }}"). -// GoType(pkg.{{ $tt }}(0)) -// -func (b *{{ $builder }}) GoType(typ any) *{{ $builder }} { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *{{ $builder }}) Optional() *{{ $builder }} { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.{{ $tt }}("{{ $t }}"). -// Tags("yaml:"xxx"") -func (b *{{ $builder }}) Tags(tags ...string) *{{ $builder }} { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *{{ $builder }}) Build(opt *Option) *FieldDescriptor { -// b.inner.checkGoType({{ $t }}Type) - b.inner.build(opt) - return b.inner -} diff --git a/internal/gen.go b/internal/gen.go deleted file mode 100644 index f1592e7..0000000 --- a/internal/gen.go +++ /dev/null @@ -1,82 +0,0 @@ -// gen is a codegen cmd for generating numeric build types from template. -package main - -import ( - "bytes" - "embed" - "go/format" - "log" - "os" - "strings" - "text/template" - - "github.com/things-go/ens" -) - -//go:embed integer.tpl float.tpl -var staticFile embed.FS - -var components = template.Must( - template.New("components"). - Funcs(template.FuncMap{ - "title": strings.Title, - "hasPrefix": strings.HasPrefix, - "toUpper": strings.ToUpper, - }). - ParseFS(staticFile, "*.tpl"), -) - -type Metadata struct { - Kind ens.Type -} - -type Numeric struct { - tpl *template.Template - fieldTypes []ens.Type -} - -func main() { - numerics := []Numeric{ - { - tpl: components.Lookup("integer.tpl"), - fieldTypes: []ens.Type{ - ens.TypeInt, - ens.TypeUint, - ens.TypeInt8, - ens.TypeInt16, - ens.TypeInt32, - ens.TypeInt64, - ens.TypeUint8, - ens.TypeUint16, - ens.TypeUint32, - ens.TypeUint64, - }, - }, - { - tpl: components.Lookup("float.tpl"), - fieldTypes: []ens.Type{ - ens.TypeFloat64, - ens.TypeFloat32, - }, - }, - } - - b := &bytes.Buffer{} - for _, v := range numerics { - for _, t := range v.fieldTypes { - b.Reset() - err := v.tpl.Execute(b, Metadata{Kind: t}) - if err != nil { - log.Fatal("executing template:", err) - } - buf, err := format.Source(b.Bytes()) - if err != nil { - log.Fatal("formatting output:", err) - } - filename := "field_" + t.String() + ".go" - if err := os.WriteFile(filename, buf, 0644); err != nil { - log.Fatal("writing go file:", err) - } - } - } -} diff --git a/internal/integer.tpl b/internal/integer.tpl deleted file mode 100644 index 977c9d0..0000000 --- a/internal/integer.tpl +++ /dev/null @@ -1,77 +0,0 @@ -// Code generated by internal/integer.tpl, DO NOT EDIT. - -package ens - -import ( - "reflect" -) - -{{ $t := $.Kind }} - -var _ Fielder = (*{{ $t }}Builder)(nil) -var {{ $t }}Type = reflect.TypeOf({{ $t }}(0)) - -{{ $title := title $t.String }} - -// {{ $title }} returns a new Field with type {{ $t }}. -func {{ $title }}(name string) *{{ $t }}Builder { - return &{{ $t }}Builder{ - &FieldDescriptor{ - Name: name, - Type: {{ $title }}Type(), - }, - } -} - -{{ $builder := printf "%sBuilder" $t }} - -// {{ $builder }} is the builder for {{ $t }} field. -type {{ $builder }} struct { - inner *FieldDescriptor -} - -// Comment sets the comment of the field. -func (b *{{ $builder }}) Comment(c string) *{{ $builder }} { - b.inner.Comment = c - return b -} - -// Nullable indicates that this field is a nullable. -func (b *{{ $builder }}) Nullable() *{{ $builder }} { - b.inner.Nullable = true - return b -} - -{{ $tt := title $t.String }} -// GoType overrides the default Go type with a custom one. -// -// field.{{ $tt }}("{{ $t }}"). -// GoType(pkg.{{ $tt }}(0)) -func (b *{{ $builder }}) GoType(typ any) *{{ $builder }} { - b.inner.goType(typ) - return b -} - -// Optional indicates that this field is optional. -// Unlike "Nullable" only fields, -// "Optional" fields are pointers in the generated struct. -func (b *{{ $builder }}) Optional() *{{ $builder }} { - b.inner.Optional = true - return b -} - -// Tags adds a list of tags to the field tag. -// -// field.{{ $tt }}("{{ $t }}"). -// Tags("yaml:"xxx"") -func (b *{{ $builder }}) Tags(tags ...string) *{{ $builder }} { - b.inner.Tags = append(b.inner.Tags, tags...) - return b -} - -// Build implements the Fielder interface by returning its descriptor. -func (b *{{ $builder }}) Build(opt *Option) *FieldDescriptor { -// b.inner.checkGoType({{ $t }}Type) - b.inner.build(opt) - return b.inner -}