Skip to content

Commit

Permalink
update extractTag to pickup key=value pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
sijms committed Apr 18, 2023
1 parent 1d0e698 commit bdf72ed
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 36 deletions.
4 changes: 2 additions & 2 deletions examples/UDT/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

type test1 struct {
Id int64 `oracle:"name:test_id"`
Name string `oracle:"name:test_name"`
Id int64 `udt:"test_id"`
Name string `udt:"test_name"`
}

func createUDT(conn *go_ora.Connection) error {
Expand Down
12 changes: 6 additions & 6 deletions examples/udt_pars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

type test1 struct {
Id int64 `oracle:"name:test_id"`
Name *sql.NullString `oracle:"name:test_name"`
Data1 string `oracle:"name:test_data1"`
Data2 string `oracle:"name:test_data2"`
Data3 string `oracle:"name:test_data3"`
Date time.Time `oracle:"name:test_date"`
Id int64 `udt:"test_id"`
Name *sql.NullString `udt:"test_name"`
Data1 string `udt:"test_data1"`
Data2 string `udt:"test_data2"`
Data3 string `udt:"test_data3"`
Date time.Time `udt:"test_date"`
}

func createTable(conn *go_ora.Connection) error {
Expand Down
40 changes: 23 additions & 17 deletions v2/udt.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,25 +342,31 @@ func (cust *customType) loadFieldMap() {
typ := cust.typ
for x := 0; x < typ.NumField(); x++ {
f := typ.Field(x)
tag := f.Tag.Get("oracle")
if len(tag) == 0 {
fieldID, _, _, _ := extractTag(f.Tag.Get("udt"))
if len(fieldID) == 0 {
continue
}
tag = strings.Trim(tag, "\"")
parts := strings.Split(tag, ",")
for _, part := range parts {
subs := strings.Split(part, ":")
if len(subs) == 0 {
continue
}
if strings.TrimSpace(strings.ToLower(subs[0])) == "name" {
if len(subs) == 1 {
continue
}
fieldID := strings.TrimSpace(strings.ToUpper(subs[1]))
cust.filedMap[fieldID] = x
}
}
fieldID = strings.ToUpper(fieldID)
cust.filedMap[fieldID] = x
//tag := f.Tag.Get("oracle")
//if len(tag) == 0 {
// continue
//}
//tag = strings.Trim(tag, "\"")
//parts := strings.Split(tag, ",")
//for _, part := range parts {
// subs := strings.Split(part, ":")
// if len(subs) == 0 {
// continue
// }
// if strings.TrimSpace(strings.ToLower(subs[0])) == "name" {
// if len(subs) == 1 {
// continue
// }
// fieldID := strings.TrimSpace(strings.ToUpper(subs[1]))
// cust.filedMap[fieldID] = x
// }
//}
}
}

Expand Down
70 changes: 59 additions & 11 deletions v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,76 @@ func parseSqlText(text string) ([]string, error) {

func extractTag(tag string) (name, _type string, size int, direction ParameterDirection) {
tag = strings.TrimSpace(tag)
direction = Input
extractKeyValue := func(text string) bool {
parts := strings.Split(text, "=")
if len(parts) != 2 {
return false
}
id := strings.ToLower(strings.TrimSpace(parts[0]))
value := strings.TrimSpace(parts[1])
switch id {
case "name":
name = value
case "type":
_type = value
case "size":
temp, _ := strconv.ParseInt(value, 10, 32)
size = int(temp)
case "direction":
fallthrough
case "dir":
value = strings.ToLower(value)
switch value {
case "in":
fallthrough
case "input":
direction = Input
case "out":
fallthrough
case "output":
direction = Output
case "inout":
direction = InOut
}
}
return true
}
if len(tag) == 0 {
return
}
tagFields := strings.Split(tag, ",")
if len(tagFields) > 0 {
name = tagFields[0]
if !extractKeyValue(tagFields[0]) {
name = tagFields[0]
}
}
if len(tagFields) > 1 {
_type = tagFields[1]
if !extractKeyValue(tagFields[1]) {
_type = tagFields[1]
}
}
if len(tagFields) > 2 {
temp, _ := strconv.ParseInt(tagFields[2], 10, 32)
size = int(temp)
if !extractKeyValue(tagFields[2]) {
temp, _ := strconv.ParseInt(tagFields[2], 10, 32)
size = int(temp)
}
}
if len(tagFields) > 3 {
dir := strings.ToLower(tagFields[3])
if dir == "in" || dir == "input" {
direction = Input
} else if dir == "out" || dir == "output" {
direction = Output
} else if dir == "inout" {
direction = InOut
if !extractKeyValue(tagFields[3]) {
dir := strings.ToLower(tagFields[3])
switch dir {
case "in":
fallthrough
case "input":
direction = Input
case "out":
fallthrough
case "output":
direction = Output
case "inout":
direction = InOut
}
}
}
return
Expand Down

0 comments on commit bdf72ed

Please sign in to comment.