Skip to content

Commit b62f403

Browse files
committed
[dev] isJSON
1 parent 67fd354 commit b62f403

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

postgresql.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,44 @@ func (m *PostgreSQL) Mark() string {
2121

2222
func (m *PostgreSQL) IsJSON(v string) (string, bool) {
2323
//table.json:field.name:type
24-
parts := strings.Split(v, ".")
25-
offset := len(parts) - 2
26-
if offset < 0 || !strings.HasPrefix(parts[0+offset], "json:") {
27-
return v, false
28-
}
29-
30-
var table string
31-
var tp string = "text"
32-
33-
if offset == 1 {
24+
var table, field, name, tp string
25+
26+
parts := strings.SplitN(v, ".", 3)
27+
if len(parts) == 2 && strings.HasPrefix(parts[0], "json:") {
28+
// [0]:json:field, [1]:name:type
29+
field = parts[0][5:]
30+
i := strings.Index(parts[1], ":")
31+
if i > 0 {
32+
name = parts[1][:i]
33+
tp = parts[1][i+1:]
34+
} else {
35+
name = parts[1]
36+
}
37+
} else if len(parts) == 3 && strings.HasPrefix(parts[1], "json:") {
38+
// [0]:table, [1]:json:field, [2]:name:type
3439
table = parts[0]
40+
field = parts[1][5:]
41+
i := strings.Index(parts[2], ":")
42+
if i > 0 {
43+
name = parts[2][:i]
44+
tp = parts[2][i+1:]
45+
} else {
46+
name = parts[2]
47+
}
48+
} else {
49+
return v, false
3550
}
3651

37-
// json:field
38-
field := parts[0+offset][5:]
39-
40-
// name:type
41-
parts = strings.Split(parts[1+offset], ":")
42-
if len(parts) == 2 {
43-
tp = parts[1]
44-
}
45-
name := parts[0]
46-
4752
var s, e string
4853
if tp == "date" {
4954
s = "CAST("
5055
e = " AS DATE)"
5156
tp = "text"
57+
} else if tp == "" {
58+
tp = "text"
5259
}
5360

54-
if table != "" {
61+
if len(parts) == 3 {
5562
name = fmt.Sprintf("%s(\"%s\".\"%s\"->'%s')::%s%s", s, table, field, name, tp, e)
5663
} else {
5764
name = fmt.Sprintf("%s(\"%s\"->'%s')::%s%s", s, field, name, tp, e)

0 commit comments

Comments
 (0)