parseHstore parses the string representation of an hstore column (the same you would get from an ordinary SELECT) into two slices of keys and values. it is used internally in the default parsing of hstores.
func ( string) ( []string, []Text, error) {
if == "" {
return
}
:= bytes.Buffer{}
:= []string{}
:= []Text{}
:= newHSP()
, := .Consume()
:= hsPrefor ! {
switch {
casehsPre:
if == '"' {
= hsKey
} else {
= errors.New("String does not begin with \"")
}
casehsKey:
switch {
case'"': //End of the key
= append(, .String())
= bytes.Buffer{}
= hsSepcase'\\': //Potential escaped character
, := .Consume()
switch {
case :
= errors.New("Found EOS in key, expecting character or \"")
case == '"', == '\\':
.WriteRune()
default:
.WriteRune()
.WriteRune()
}
default: //Any other character
.WriteRune()
}
casehsSep:
if == '=' {
, = .Consume()
switch {
case :
= errors.New("Found EOS after '=', expecting '>'")
case == '>':
, = .Consume()
switch {
case :
= errors.New("Found EOS after '=>', expecting '\"' or 'NULL'")
case == '"':
= hsValcase == 'N':
= hsNuldefault:
= fmt.Errorf("Invalid character '%c' after '=>', expecting '\"' or 'NULL'", )
}
default:
= fmt.Errorf("Invalid character after '=', expecting '>'")
}
} else {
= fmt.Errorf("Invalid character '%c' after value, expecting '='", )
}
casehsVal:
switch {
case'"': //End of the value
= append(, Text{String: .String(), Status: Present})
= bytes.Buffer{}
= hsNextcase'\\': //Potential escaped character
, := .Consume()
switch {
case :
= errors.New("Found EOS in key, expecting character or \"")
case == '"', == '\\':
.WriteRune()
default:
.WriteRune()
.WriteRune()
}
default: //Any other character
.WriteRune()
}
casehsNul:
:= make([]rune, 3)
[0] =
for := 1; < 3; ++ {
, = .Consume()
if {
= errors.New("Found EOS in NULL value")
return
}
[] =
}
if [0] == 'U' && [1] == 'L' && [2] == 'L' {
= append(, Text{Status: Null})
= hsNext
} else {
= fmt.Errorf("Invalid NULL value: 'N%s'", string())
}
casehsNext:
if == ',' {
, = .Consume()
switch {
case :
= errors.New("Found EOS after ',', expcting space")
case (unicode.IsSpace()):
, = .Consume()
= hsKeydefault:
= fmt.Errorf("Invalid character '%c' after ', ', expecting \"", )
}
} else {
= fmt.Errorf("Invalid character '%c' after value, expecting ','", )
}
}
if != nil {
return
}
, = .Consume()
}
if != hsNext {
= errors.New("Improperly formatted hstore")
return
}
=
=
return
}
Scan implements the database/sql Scanner interface.
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.