Skip to content

Commit 16a5ab2

Browse files
committed
Merge branch 'master' into key-each
# Conflicts: # parser_test.go
2 parents 4f386e2 + 2248061 commit 16a5ab2

File tree

2 files changed

+210
-65
lines changed

2 files changed

+210
-65
lines changed

parser.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func GetFloat(data []byte, keys ...string) (val float64, err error) {
522522
return ParseFloat(v)
523523
}
524524

525-
// GetInt returns the value retrieved by `Get`, cast to a float64 if possible.
525+
// GetInt returns the value retrieved by `Get`, cast to a int64 if possible.
526526
// If key data type do not match, it will return an error.
527527
func GetInt(data []byte, keys ...string) (val int64, err error) {
528528
v, t, _, e := Get(data, keys...)
@@ -557,25 +557,20 @@ func GetBoolean(data []byte, keys ...string) (val bool, err error) {
557557

558558
// ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness)
559559
func ParseBoolean(b []byte) (bool, error) {
560-
switch b[0] {
561-
case 't':
560+
switch {
561+
case bytes.Equal(b, trueLiteral):
562562
return true, nil
563-
case 'f':
563+
case bytes.Equal(b, falseLiteral):
564564
return false, nil
565565
default:
566566
return false, MalformedValueError
567567
}
568568
}
569569

570-
// ParseString parses a String ValueType into a Go []byte (the main parsing work is unescaping the JSON string)
571-
func parseStringAsBytes(b []byte) ([]byte, error) {
572-
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings (hopefully; the Go compiler might just always kick stackbuf[:] into the heap)
573-
return Unescape(b, stackbuf[:])
574-
}
575-
576570
// ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)
577571
func ParseString(b []byte) (string, error) {
578-
if bU, err := parseStringAsBytes(b); err != nil {
572+
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
573+
if bU, err := Unescape(b, stackbuf[:]); err != nil {
579574
return "", nil
580575
} else {
581576
return string(bU), nil
@@ -591,11 +586,11 @@ func ParseFloat(b []byte) (float64, error) {
591586
}
592587
}
593588

594-
// ParseNumber parses a Number ValueType into a Go float64
589+
// ParseInt parses a Number ValueType into a Go int64
595590
func ParseInt(b []byte) (int64, error) {
596-
if v, err := parseInt(b); !err {
591+
if v, ok := parseInt(b); !ok {
597592
return 0, MalformedValueError
598593
} else {
599594
return v, nil
600595
}
601-
}
596+
}

0 commit comments

Comments
 (0)