Skip to content

Commit

Permalink
Merge pull request #54 from Codexiaoyi/ignore-example
Browse files Browse the repository at this point in the history
add model register examples
  • Loading branch information
flycash authored Dec 21, 2021
2 parents 9e86e78 + 52da12d commit adb2445
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Updater Definition](https://github.com/gotomicro/eql/pull/8)
- [Rft: remove NilAsNullFunc](https://github.com/gotomicro/eql/pull/48)
- [Metadata API](https://github.com/gotomicro/eql/pull/16)
- [Add model register examples](https://github.com/gotomicro/eql/pull/54)
- [tagMetaRegistry: default implementation of MetaRegistry](https://github.com/gotomicro/eql/pull/25)
- [Rft: remove defaultRegistry](https://github.com/gotomicro/eql/pull/46)
- [Use `bytebufferpool` for builder](https://github.com/gotomicro/eql/pull/39)
Expand Down
56 changes: 56 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package eql

import (
"fmt"
"reflect"
"testing"

Expand Down Expand Up @@ -88,3 +89,58 @@ type TestIgnoreModel struct {
Age int8 `eql:"-"`
LastName string
}

func ExampleMetaRegistry_Get() {
tm := &TestModel{}
registry := &tagMetaRegistry{}
meta, _ := registry.Get(tm)
fmt.Printf("table name: %v\n", meta.tableName)

// Output:
// table name: test_model
}

func ExampleMetaRegistry_Register() {
// case1 without TableMetaOption
tm := &TestModel{}
registry := &tagMetaRegistry{}
meta, _ := registry.Register(tm)
fmt.Printf(`
case1:
table name:%s
column names:%s,%s,%s,%s
`, meta.tableName, meta.columns[0].columnName, meta.columns[1].columnName, meta.columns[2].columnName, meta.columns[3].columnName)

// case2 use Tag to ignore field
tim := &TestIgnoreModel{}
registry = &tagMetaRegistry{}
meta, _ = registry.Register(tim)
fmt.Printf(`
case2:
table name:%s
column names:%s,%s
`, meta.tableName, meta.columns[0].columnName, meta.columns[1].columnName)

// case3 use IgnoreFieldOption to ignore field
tim = &TestIgnoreModel{}
registry = &tagMetaRegistry{}
meta, _ = registry.Register(tim, IgnoreFieldsOption("FirstName"))
fmt.Printf(`
case3:
table name:%s
column names:%s
`, meta.tableName, meta.columns[0].columnName)

// Output:
// case1:
// table name:test_model
// column names:id,first_name,age,last_name
//
// case2:
// table name:test_ignore_model
// column names:first_name,last_name
//
// case3:
// table name:test_ignore_model
// column names:last_name
}

0 comments on commit adb2445

Please sign in to comment.