Skip to content

Commit

Permalink
Add single-quote string escape support
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaflik committed Aug 22, 2024
1 parent 56f95dd commit 3ea5988
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
27 changes: 20 additions & 7 deletions lib/column/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [

var bracketOpen, stringOpen bool

var foundNameOffset int
var foundNameLen int
var foundValueOffset int
var foundValueLen int
var skippedValueTokens []int
var indexFound bool
var valueIndex = 0

Expand All @@ -96,17 +97,22 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
break
// when inside a bracket, we can start capture value inside single quotes
case bracketOpen && token == '\'' && !stringOpen:
foundNameOffset = c + 1
foundValueOffset = c + 1
stringOpen = true
break
// close the string and capture the value
case token == '\'' && stringOpen:
stringOpen = false
foundNameLen = c - foundNameOffset
foundValueLen = c - foundValueOffset
break
// escape character, skip the next character
case token == '\\' && stringOpen:
skippedValueTokens = append(skippedValueTokens, c-foundValueOffset)
c++
break
// capture optional index. `=` token is followed with an integer index
case token == '=' && !stringOpen:
if foundNameLen == 0 {
if foundValueLen == 0 {
return
}

Expand All @@ -125,9 +131,10 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
}
valueIndex = idx
indexFound = true
break
// capture the value and index when a comma or closing bracket is found
case (token == ',' || token == ')') && !stringOpen:
if foundNameLen == 0 {
if foundValueLen == 0 {
return
}

Expand All @@ -145,9 +152,15 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
return
}

foundName := src[foundValueOffset : foundValueOffset+foundValueLen]
for _, skipped := range skippedValueTokens {
foundName = append(foundName[:skipped], foundName[skipped+1:]...)
}

indexes = append(indexes, valueIndex)
values = append(values, string(src[foundNameOffset:foundNameOffset+foundNameLen]))
values = append(values, string(foundName))
indexFound = false
break
}
}

Expand Down
16 changes: 8 additions & 8 deletions lib/column/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ func TestExtractEnumNamedValues(t *testing.T) {
5: "b",
},
},
//{
// name: "Enum8 with escaped quotes",
// chType: "Enum8('a\\'b'=1)",
// expectedType: "Enum8",
// expectedValues: map[int]string{
// 1: "a'b",
// },
//},
{
name: "Enum8 with escaped quotes",
chType: `Enum8('a\'b'=1)`,
expectedType: "Enum8",
expectedValues: map[int]string{
1: "a'b",
},
},
{
name: "Enum8 with invalid index",
chType: "Enum8('a'=1,'b'=256)",
Expand Down

0 comments on commit 3ea5988

Please sign in to comment.