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

add model register examples #54

Merged
merged 1 commit into from
Dec 21, 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 @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

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

you' better print column name and the metadata, so users can understand the example's detail

// 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
}