From 52da12d19a7b89e865003bb10da9fde203a16208 Mon Sep 17 00:00:00 2001 From: Codexiaoyi <874183200@qq.com> Date: Tue, 14 Dec 2021 02:08:48 +0000 Subject: [PATCH] add model register examples --- .CHANGELOG.md | 1 + model_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/.CHANGELOG.md b/.CHANGELOG.md index c7f4b70..f55b539 100644 --- a/.CHANGELOG.md +++ b/.CHANGELOG.md @@ -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) diff --git a/model_test.go b/model_test.go index 0e7301a..1e685d8 100644 --- a/model_test.go +++ b/model_test.go @@ -15,6 +15,7 @@ package eql import ( + "fmt" "reflect" "testing" @@ -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 +}