File tree 12 files changed +168
-10
lines changed
day2-reflect-schema/schema
day4-chain-operation/schema
12 files changed +168
-10
lines changed Original file line number Diff line number Diff line change @@ -37,12 +37,23 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
37
37
return fieldValues
38
38
}
39
39
40
+ type ITableName interface {
41
+ TableName () string
42
+ }
43
+
40
44
// Parse a struct to a Schema instance
41
45
func Parse (dest interface {}, d dialect.Dialect ) * Schema {
42
46
modelType := reflect .Indirect (reflect .ValueOf (dest )).Type ()
47
+ var tableName string
48
+ t , ok := dest .(ITableName )
49
+ if ! ok {
50
+ tableName = modelType .Name ()
51
+ } else {
52
+ tableName = t .TableName ()
53
+ }
43
54
schema := & Schema {
44
55
Model : dest ,
45
- Name : modelType . Name () ,
56
+ Name : tableName ,
46
57
fieldMap : make (map [string ]* Field ),
47
58
}
48
59
Original file line number Diff line number Diff line change @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
33
33
t .Fatal ("failed to get values" )
34
34
}
35
35
}
36
+
37
+ type UserTest struct {
38
+ Name string `geeorm:"PRIMARY KEY"`
39
+ Age int
40
+ }
41
+
42
+ func (u * UserTest ) TableName () string {
43
+ return "ns_user_test"
44
+ }
45
+
46
+ func TestSchema_TableName (t * testing.T ) {
47
+ schema := Parse (& UserTest {}, TestDial )
48
+ if schema .Name != "ns_user_test" || len (schema .Fields ) != 2 {
49
+ t .Fatal ("failed to parse User struct" )
50
+ }
51
+ }
Original file line number Diff line number Diff line change @@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
37
37
return fieldValues
38
38
}
39
39
40
+ type ITableName interface {
41
+ TableName () string
42
+ }
43
+
40
44
// Parse a struct to a Schema instance
41
45
func Parse (dest interface {}, d dialect.Dialect ) * Schema {
42
46
modelType := reflect .Indirect (reflect .ValueOf (dest )).Type ()
47
+ var tableName string
48
+ t , ok := dest .(ITableName )
49
+ if ! ok {
50
+ tableName = modelType .Name ()
51
+ } else {
52
+ tableName = t .TableName ()
53
+ }
43
54
schema := & Schema {
44
55
Model : dest ,
45
- Name : modelType . Name () ,
56
+ Name : tableName ,
46
57
fieldMap : make (map [string ]* Field ),
47
58
}
48
-
49
59
for i := 0 ; i < modelType .NumField (); i ++ {
50
60
p := modelType .Field (i )
51
61
if ! p .Anonymous && ast .IsExported (p .Name ) {
Original file line number Diff line number Diff line change @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
33
33
t .Fatal ("failed to get values" )
34
34
}
35
35
}
36
+
37
+ type UserTest struct {
38
+ Name string `geeorm:"PRIMARY KEY"`
39
+ Age int
40
+ }
41
+
42
+ func (u * UserTest ) TableName () string {
43
+ return "ns_user_test"
44
+ }
45
+
46
+ func TestSchema_TableName (t * testing.T ) {
47
+ schema := Parse (& UserTest {}, TestDial )
48
+ if schema .Name != "ns_user_test" || len (schema .Fields ) != 2 {
49
+ t .Fatal ("failed to parse User struct" )
50
+ }
51
+ }
Original file line number Diff line number Diff line change @@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
37
37
return fieldValues
38
38
}
39
39
40
+ type ITableName interface {
41
+ TableName () string
42
+ }
43
+
40
44
// Parse a struct to a Schema instance
41
45
func Parse (dest interface {}, d dialect.Dialect ) * Schema {
42
46
modelType := reflect .Indirect (reflect .ValueOf (dest )).Type ()
47
+ var tableName string
48
+ t , ok := dest .(ITableName )
49
+ if ! ok {
50
+ tableName = modelType .Name ()
51
+ } else {
52
+ tableName = t .TableName ()
53
+ }
43
54
schema := & Schema {
44
55
Model : dest ,
45
- Name : modelType . Name () ,
56
+ Name : tableName ,
46
57
fieldMap : make (map [string ]* Field ),
47
58
}
48
-
49
59
for i := 0 ; i < modelType .NumField (); i ++ {
50
60
p := modelType .Field (i )
51
61
if ! p .Anonymous && ast .IsExported (p .Name ) {
Original file line number Diff line number Diff line change @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
33
33
t .Fatal ("failed to get values" )
34
34
}
35
35
}
36
+
37
+ type UserTest struct {
38
+ Name string `geeorm:"PRIMARY KEY"`
39
+ Age int
40
+ }
41
+
42
+ func (u * UserTest ) TableName () string {
43
+ return "ns_user_test"
44
+ }
45
+
46
+ func TestSchema_TableName (t * testing.T ) {
47
+ schema := Parse (& UserTest {}, TestDial )
48
+ if schema .Name != "ns_user_test" || len (schema .Fields ) != 2 {
49
+ t .Fatal ("failed to parse User struct" )
50
+ }
51
+ }
Original file line number Diff line number Diff line change @@ -37,12 +37,23 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
37
37
return fieldValues
38
38
}
39
39
40
+ type ITableName interface {
41
+ TableName () string
42
+ }
43
+
40
44
// Parse a struct to a Schema instance
41
45
func Parse (dest interface {}, d dialect.Dialect ) * Schema {
42
46
modelType := reflect .Indirect (reflect .ValueOf (dest )).Type ()
47
+ var tableName string
48
+ t , ok := dest .(ITableName )
49
+ if ! ok {
50
+ tableName = modelType .Name ()
51
+ } else {
52
+ tableName = t .TableName ()
53
+ }
43
54
schema := & Schema {
44
55
Model : dest ,
45
- Name : modelType . Name () ,
56
+ Name : tableName ,
46
57
fieldMap : make (map [string ]* Field ),
47
58
}
48
59
Original file line number Diff line number Diff line change @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
33
33
t .Fatal ("failed to get values" )
34
34
}
35
35
}
36
+
37
+ type UserTest struct {
38
+ Name string `geeorm:"PRIMARY KEY"`
39
+ Age int
40
+ }
41
+
42
+ func (u * UserTest ) TableName () string {
43
+ return "ns_user_test"
44
+ }
45
+
46
+ func TestSchema_TableName (t * testing.T ) {
47
+ schema := Parse (& UserTest {}, TestDial )
48
+ if schema .Name != "ns_user_test" || len (schema .Fields ) != 2 {
49
+ t .Fatal ("failed to parse User struct" )
50
+ }
51
+ }
Original file line number Diff line number Diff line change @@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
37
37
return fieldValues
38
38
}
39
39
40
+ type ITableName interface {
41
+ TableName () string
42
+ }
43
+
40
44
// Parse a struct to a Schema instance
41
45
func Parse (dest interface {}, d dialect.Dialect ) * Schema {
42
46
modelType := reflect .Indirect (reflect .ValueOf (dest )).Type ()
47
+ var tableName string
48
+ t , ok := dest .(ITableName )
49
+ if ! ok {
50
+ tableName = modelType .Name ()
51
+ } else {
52
+ tableName = t .TableName ()
53
+ }
43
54
schema := & Schema {
44
55
Model : dest ,
45
- Name : modelType . Name () ,
56
+ Name : tableName ,
46
57
fieldMap : make (map [string ]* Field ),
47
58
}
48
-
49
59
for i := 0 ; i < modelType .NumField (); i ++ {
50
60
p := modelType .Field (i )
51
61
if ! p .Anonymous && ast .IsExported (p .Name ) {
Original file line number Diff line number Diff line change @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
33
33
t .Fatal ("failed to get values" )
34
34
}
35
35
}
36
+
37
+ type UserTest struct {
38
+ Name string `geeorm:"PRIMARY KEY"`
39
+ Age int
40
+ }
41
+
42
+ func (u * UserTest ) TableName () string {
43
+ return "ns_user_test"
44
+ }
45
+
46
+ func TestSchema_TableName (t * testing.T ) {
47
+ schema := Parse (& UserTest {}, TestDial )
48
+ if schema .Name != "ns_user_test" || len (schema .Fields ) != 2 {
49
+ t .Fatal ("failed to parse User struct" )
50
+ }
51
+ }
Original file line number Diff line number Diff line change @@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
37
37
return fieldValues
38
38
}
39
39
40
+ type ITableName interface {
41
+ TableName () string
42
+ }
43
+
40
44
// Parse a struct to a Schema instance
41
45
func Parse (dest interface {}, d dialect.Dialect ) * Schema {
42
46
modelType := reflect .Indirect (reflect .ValueOf (dest )).Type ()
47
+ var tableName string
48
+ t , ok := dest .(ITableName )
49
+ if ! ok {
50
+ tableName = modelType .Name ()
51
+ } else {
52
+ tableName = t .TableName ()
53
+ }
43
54
schema := & Schema {
44
55
Model : dest ,
45
- Name : modelType . Name () ,
56
+ Name : tableName ,
46
57
fieldMap : make (map [string ]* Field ),
47
58
}
48
-
49
59
for i := 0 ; i < modelType .NumField (); i ++ {
50
60
p := modelType .Field (i )
51
61
if ! p .Anonymous && ast .IsExported (p .Name ) {
Original file line number Diff line number Diff line change @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
33
33
t .Fatal ("failed to get values" )
34
34
}
35
35
}
36
+
37
+ type UserTest struct {
38
+ Name string `geeorm:"PRIMARY KEY"`
39
+ Age int
40
+ }
41
+
42
+ func (u * UserTest ) TableName () string {
43
+ return "ns_user_test"
44
+ }
45
+
46
+ func TestSchema_TableName (t * testing.T ) {
47
+ schema := Parse (& UserTest {}, TestDial )
48
+ if schema .Name != "ns_user_test" || len (schema .Fields ) != 2 {
49
+ t .Fatal ("failed to parse User struct" )
50
+ }
51
+ }
You can’t perform that action at this time.
0 commit comments