From 8700152b6cbbf6a221e7ca59ef55e21ef73cf580 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 2 Jan 2016 22:58:49 +0800 Subject: [PATCH] resolved #67 --- VERSION | 2 +- engine.go | 2 +- helpers.go | 20 ++++++++++++++++++++ helpers_test.go | 22 ++++++++++++++++++++++ xorm.go | 2 +- 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 helpers_test.go diff --git a/VERSION b/VERSION index 041712a5..2efec6a8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -xorm v0.4.4.1230 +xorm v0.4.5.0102 diff --git a/engine.go b/engine.go index 676101a3..5188d27e 100644 --- a/engine.go +++ b/engine.go @@ -759,7 +759,7 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table { if ormTagStr != "" { col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false, IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]bool)} - tags := strings.Split(ormTagStr, " ") + tags := splitTag(ormTagStr) if len(tags) > 0 { if tags[0] == "-" { diff --git a/helpers.go b/helpers.go index 1ac6ad62..62cc4383 100644 --- a/helpers.go +++ b/helpers.go @@ -15,6 +15,26 @@ import ( "github.com/go-xorm/core" ) +func splitTag(tag string) (tags []string) { + tag = strings.TrimSpace(tag) + var hasQuote = false + var lastIdx = 0 + for i, t := range tag { + if t == '\'' { + hasQuote = !hasQuote + } else if t == ' ' { + if lastIdx < i && !hasQuote { + tags = append(tags, strings.TrimSpace(tag[lastIdx:i])) + lastIdx = i + 1 + } + } + } + if lastIdx < len(tag) { + tags = append(tags, strings.TrimSpace(tag[lastIdx:len(tag)])) + } + return +} + type zeroable interface { IsZero() bool } diff --git a/helpers_test.go b/helpers_test.go new file mode 100644 index 00000000..7d17383d --- /dev/null +++ b/helpers_test.go @@ -0,0 +1,22 @@ +package xorm + +import "testing" + +func TestSplitTag(t *testing.T) { + var cases = []struct { + tag string + tags []string + }{ + {"not null default '2000-01-01 00:00:00' TIMESTAMP", []string{"not", "null", "default", "'2000-01-01 00:00:00'", "TIMESTAMP"}}, + {"TEXT", []string{"TEXT"}}, + {"default('2000-01-01 00:00:00')", []string{"default('2000-01-01 00:00:00')"}}, + {"json binary", []string{"json", "binary"}}, + } + + for _, kase := range cases { + tags := splitTag(kase.tag) + if !sliceEq(tags, kase.tags) { + t.Fatalf("[%d]%v is not equal [%d]%v", len(tags), tags, len(kase.tags), kase.tags) + } + } +} diff --git a/xorm.go b/xorm.go index 9d42ab28..4a13edc6 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.4.1230" + Version string = "0.4.5.0102" ) func regDrvsNDialects() bool {