Skip to content

Commit d1a978b

Browse files
committed
[dev] refactoring
1 parent a26e035 commit d1a978b

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

postgresql.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,25 @@ func (m *PostgreSQL) Mark() string {
2121

2222
func (m *PostgreSQL) IsJSON(v string) (string, bool) {
2323
//table.json:field.name:type
24-
var table, field, name string
25-
var meta []string
26-
tp := "text"
27-
28-
parts := strings.SplitN(v, ".", 3)
29-
if len(parts) == 2 && strings.HasPrefix(parts[0], "json:") {
30-
// [0]:json:field, [1]:name:type
31-
field = parts[0][5:]
32-
meta = strings.SplitN(parts[1], ":", 2)
33-
} else if len(parts) == 3 && strings.HasPrefix(parts[1], "json:") {
34-
// [0]:table, [1]:json:field, [2]:name:type
35-
table = parts[0]
36-
field = parts[1][5:]
37-
meta = strings.SplitN(parts[2], ":", 2)
38-
} else {
24+
dot := strings.Index(v, ".")
25+
if (dot == -1 && v[:5] != "json:") || v[dot+1:dot+6] != "json:" {
3926
return v, false
4027
}
41-
name = meta[0]
42-
if len(meta) == 2 {
43-
tp = meta[1]
28+
29+
// separate table and field
30+
table := ""
31+
field := v
32+
if dot != -1 {
33+
table = v[:dot]
34+
field = v[dot+1:]
35+
}
36+
37+
// separate field name and meta info
38+
meta := strings.Split(field, ":")
39+
name := meta[1]
40+
tp := "text"
41+
if len(meta) == 3 {
42+
tp = meta[2]
4443
}
4544

4645
var s, e string
@@ -52,7 +51,7 @@ func (m *PostgreSQL) IsJSON(v string) (string, bool) {
5251
tp = "text"
5352
}
5453

55-
if len(parts) == 3 {
54+
if table != "" {
5655
name = fmt.Sprintf("%s(\"%s\".\"%s\"->'%s')::%s%s", s, table, field, name, tp, e)
5756
} else {
5857
name = fmt.Sprintf("%s(\"%s\"->'%s')::%s%s", s, field, name, tp, e)

0 commit comments

Comments
 (0)