Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Ignore Columns #49

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Use `bytebufferpool` for builder](https://github.com/gotomicro/eql/pull/39)
- [Refactor: move Insert function into db.file](https://github.com/gotomicro/eql/pull/28)
- [Selector implementation, excluding WHERE and HAVING clauses](https://github.com/gotomicro/eql/pull/32)
- [Ignore columns by Tag and Option](https://github.com/gotomicro/eql/pull/49)
- [Selector WHERE clause](https://github.com/gotomicro/eql/pull/40)
- [Support Aggregate Functions](https://github.com/gotomicro/eql/pull/37)
- [Updater implementation, excluding WHERE clause](https://github.com/gotomicro/eql/pull/36)
Expand Down
43 changes: 38 additions & 5 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ type ColumnMeta struct {
isAutoIncrement bool
}

type tableMetaOption func(meta *TableMeta)
// TableMetaOption represents options of TableMeta, this options will cover default cover.
type TableMetaOption func(meta *TableMeta)

// MetaRegistry stores table metadata
type MetaRegistry interface {
Get(table interface{}) (*TableMeta, error)
Register(table interface{}, opts ...tableMetaOption) (*TableMeta, error)
Register(table interface{}, opts ...TableMetaOption) (*TableMeta, error)
}

// tagMetaRegistry is the default implementation based on tag eql
Expand All @@ -63,7 +64,7 @@ func (t *tagMetaRegistry) Get(table interface{}) (*TableMeta, error) {

// Register function generates a metadata for each column and places it in a thread-safe mapping to facilitate direct access to the metadata.
// And the metadata can be modified by user-defined methods opts
func (t *tagMetaRegistry) Register(table interface{}, opts ...tableMetaOption) (*TableMeta, error) {
func (t *tagMetaRegistry) Register(table interface{}, opts ...TableMetaOption) (*TableMeta, error) {
rtype := reflect.TypeOf(table)
v := rtype.Elem()
columnMetas := []*ColumnMeta{}
Expand All @@ -72,8 +73,21 @@ func (t *tagMetaRegistry) Register(table interface{}, opts ...tableMetaOption) (
for i := 0; i < lens; i++ {
structField := v.Field(i)
tag := structField.Tag.Get("eql")
isAuto := strings.Contains(tag, "auto_increment")
isKey := strings.Contains(tag, "primary_key")
var isKey, isAuto, isIgnore bool
for _, t := range strings.Split(tag, ",") {
switch t {
case "primary_key":
isKey = true
case "auto_increment":
isAuto = true
case "-":
isIgnore = true
}
}
if isIgnore {
// skip the field.
continue
}
columnMeta := &ColumnMeta{
columnName: internal.UnderscoreName(structField.Name),
fieldName: structField.Name,
Expand All @@ -95,5 +109,24 @@ func (t *tagMetaRegistry) Register(table interface{}, opts ...tableMetaOption) (
}
t.metas.Store(rtype, tableMeta)
return tableMeta, nil
}

// IgnoreFieldsOption function provide an option to ignore some fields when register table.
func IgnoreFieldsOption(fieldNames ...string) TableMetaOption {
return func(meta *TableMeta) {
for _, field := range fieldNames {
//has field in the TableMeta
if _, ok := meta.fieldMap[field]; ok {
// delete field in columns slice
for index, column := range meta.columns {
if column.fieldName == field {
meta.columns = append(meta.columns[:index], meta.columns[index+1:]...)
break
}
}
// delete field in fieldMap
delete(meta.fieldMap, field)
}
}
}
}
32 changes: 32 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,35 @@ func TestTagMetaRegistry(t *testing.T) {
assert.Equal(t, reflect.TypeOf(int8(0)), idMetaLastAge.typ)

}

func TestIgnoreFieldsOption(t *testing.T) {
tm := &TestIgnoreModel{}
registry := &tagMetaRegistry{}
meta, err := registry.Register(tm, IgnoreFieldsOption("Id", "FirstName"))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, len(meta.columns))
assert.Equal(t, 1, len(meta.fieldMap))
assert.Equal(t, reflect.TypeOf(tm), meta.typ)
assert.Equal(t, "test_ignore_model", meta.tableName)

_, hasId := meta.fieldMap["Id"]
assert.False(t, hasId)

_, hasFirstName := meta.fieldMap["FirstName"]
assert.False(t, hasFirstName)

_, hasAge := meta.fieldMap["Age"]
assert.False(t, hasAge)

_, hasLastName := meta.fieldMap["LastName"]
assert.True(t, hasLastName)
}

type TestIgnoreModel struct {
Id int64 `eql:"auto_increment,primary_key,-"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer that symbol - has highest priority. So if we find -, we just ignore it.

FirstName string
Age int8 `eql:"-"`
LastName string
}