Skip to content

Commit

Permalink
fix: fix field builder, remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Mar 15, 2024
1 parent 96e17a9 commit 5e0fbe3
Show file tree
Hide file tree
Showing 25 changed files with 111 additions and 1,727 deletions.
81 changes: 0 additions & 81 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
68 changes: 0 additions & 68 deletions field_bool.go

This file was deleted.

111 changes: 111 additions & 0 deletions field_builder.go
Original file line number Diff line number Diff line change
@@ -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
}
68 changes: 0 additions & 68 deletions field_bytes.go

This file was deleted.

Loading

0 comments on commit 5e0fbe3

Please sign in to comment.