Skip to content

Commit e2a6695

Browse files
committed
Refactor
1 parent 288d600 commit e2a6695

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

parser.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,7 @@ func GetString(data []byte, keys ...string) (val string, err error) {
394394
return string(v), nil
395395
}
396396

397-
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
398-
out, err := Unescape(v, stackbuf[:])
399-
400-
return string(out), err
397+
return ParseString(v)
401398
}
402399

403400
// GetFloat returns the value retrieved by `Get`, cast to a float64 if possible.
@@ -414,8 +411,7 @@ func GetFloat(data []byte, keys ...string) (val float64, err error) {
414411
return 0, fmt.Errorf("Value is not a number: %s", string(v))
415412
}
416413

417-
val, err = parseFloat(&v)
418-
return
414+
return ParseFloat(v)
419415
}
420416

421417
// GetInt returns the value retrieved by `Get`, cast to a float64 if possible.
@@ -431,11 +427,7 @@ func GetInt(data []byte, keys ...string) (val int64, err error) {
431427
return 0, fmt.Errorf("Value is not a number: %s", string(v))
432428
}
433429

434-
if val, ok := parseInt(v); !ok {
435-
return 0, MalformedValueError
436-
} else {
437-
return val, nil
438-
}
430+
return ParseInt(v)
439431
}
440432

441433
// GetBoolean returns the value retrieved by `Get`, cast to a bool if possible.
@@ -452,40 +444,50 @@ func GetBoolean(data []byte, keys ...string) (val bool, err error) {
452444
return false, fmt.Errorf("Value is not a boolean: %s", string(v))
453445
}
454446

455-
if v[0] == 't' {
456-
val = true
457-
} else {
458-
val = false
459-
}
460-
461-
return
447+
return ParseBoolean(v)
462448
}
463449

464450
// ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness)
465-
func ParseBoolean(vbytes []byte) bool {
466-
return (vbytes[0] == 't') // assumes value is already validated by Get(), etc. as signaled by jtype == Boolean
451+
func ParseBoolean(b []byte) (bool, error) {
452+
switch b[0] {
453+
case 't':
454+
return true, nil
455+
case 'f':
456+
return false, nil
457+
default:
458+
return false, MalformedValueError
459+
}
467460
}
468461

469462
// ParseString parses a String ValueType into a Go []byte (the main parsing work is unescaping the JSON string)
470-
func ParseStringAsBytes(vbytes []byte) ([]byte, error) {
463+
func parseStringAsBytes(b []byte) ([]byte, error) {
471464
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)
472-
return Unescape(vbytes, stackbuf[:])
465+
return Unescape(b, stackbuf[:])
473466
}
474467

475468
// ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)
476-
func ParseString(vbytes []byte) (string, error) {
477-
if vbytesUnesc, err := ParseStringAsBytes(vbytes); err != nil {
469+
func ParseString(b []byte) (string, error) {
470+
if bU, err := parseStringAsBytes(b); err != nil {
478471
return "", nil
479472
} else {
480-
return string(vbytesUnesc), nil
473+
return string(bU), nil
481474
}
482475
}
483476

484477
// ParseNumber parses a Number ValueType into a Go float64
485-
func ParseNumber(vbytes []byte) (float64, error) {
486-
if v, err := parseFloat(&vbytes); err != nil {
478+
func ParseFloat(b []byte) (float64, error) {
479+
if v, err := parseFloat(&b); err != nil {
487480
return 0, MalformedValueError
488481
} else {
489482
return v, nil
490483
}
491484
}
485+
486+
// ParseNumber parses a Number ValueType into a Go float64
487+
func ParseInt(b []byte) (int64, error) {
488+
if v, err := parseInt(b); !err {
489+
return 0, MalformedValueError
490+
} else {
491+
return v, nil
492+
}
493+
}

0 commit comments

Comments
 (0)