mirror of
https://codeberg.org/superseriousbusiness/gotosocial.git
synced 2024-12-23 17:40:20 +03:00
a156188b3e
* update dependencies, bump Go version to 1.19 * bump test image Go version * update golangci-lint * update gotosocial-drone-build * sign * linting, go fmt * update swagger docs * update swagger docs * whitespace * update contributing.md * fuckin whoopsie doopsie * linterino, linteroni * fix followrequest test not starting processor * fix other api/client tests not starting processor * fix remaining tests where processor not started * bump go-runners version * don't check last-webfingered-at, processor may have updated this * update swagger command * update bun to latest version * fix embed to work the same as before with new bun Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
112 lines
2.6 KiB
Go
112 lines
2.6 KiB
Go
package decoder
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"unsafe"
|
|
|
|
"github.com/goccy/go-json/internal/errors"
|
|
)
|
|
|
|
type numberDecoder struct {
|
|
stringDecoder *stringDecoder
|
|
op func(unsafe.Pointer, json.Number)
|
|
structName string
|
|
fieldName string
|
|
}
|
|
|
|
func newNumberDecoder(structName, fieldName string, op func(unsafe.Pointer, json.Number)) *numberDecoder {
|
|
return &numberDecoder{
|
|
stringDecoder: newStringDecoder(structName, fieldName),
|
|
op: op,
|
|
structName: structName,
|
|
fieldName: fieldName,
|
|
}
|
|
}
|
|
|
|
func (d *numberDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
|
|
bytes, err := d.decodeStreamByte(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
|
|
return errors.ErrSyntax(err.Error(), s.totalOffset())
|
|
}
|
|
d.op(p, json.Number(string(bytes)))
|
|
s.reset()
|
|
return nil
|
|
}
|
|
|
|
func (d *numberDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
|
|
bytes, c, err := d.decodeByte(ctx.Buf, cursor)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&bytes)), 64); err != nil {
|
|
return 0, errors.ErrSyntax(err.Error(), c)
|
|
}
|
|
cursor = c
|
|
s := *(*string)(unsafe.Pointer(&bytes))
|
|
d.op(p, json.Number(s))
|
|
return cursor, nil
|
|
}
|
|
|
|
func (d *numberDecoder) decodeStreamByte(s *Stream) ([]byte, error) {
|
|
start := s.cursor
|
|
for {
|
|
switch s.char() {
|
|
case ' ', '\n', '\t', '\r':
|
|
s.cursor++
|
|
continue
|
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
return floatBytes(s), nil
|
|
case 'n':
|
|
if err := nullBytes(s); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, nil
|
|
case '"':
|
|
return d.stringDecoder.decodeStreamByte(s)
|
|
case nul:
|
|
if s.read() {
|
|
continue
|
|
}
|
|
goto ERROR
|
|
default:
|
|
goto ERROR
|
|
}
|
|
}
|
|
ERROR:
|
|
if s.cursor == start {
|
|
return nil, errors.ErrInvalidBeginningOfValue(s.char(), s.totalOffset())
|
|
}
|
|
return nil, errors.ErrUnexpectedEndOfJSON("json.Number", s.totalOffset())
|
|
}
|
|
|
|
func (d *numberDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
|
|
for {
|
|
switch buf[cursor] {
|
|
case ' ', '\n', '\t', '\r':
|
|
cursor++
|
|
continue
|
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
start := cursor
|
|
cursor++
|
|
for floatTable[buf[cursor]] {
|
|
cursor++
|
|
}
|
|
num := buf[start:cursor]
|
|
return num, cursor, nil
|
|
case 'n':
|
|
if err := validateNull(buf, cursor); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
cursor += 4
|
|
return nil, cursor, nil
|
|
case '"':
|
|
return d.stringDecoder.decodeByte(buf, cursor)
|
|
default:
|
|
return nil, 0, errors.ErrUnexpectedEndOfJSON("json.Number", cursor)
|
|
}
|
|
}
|
|
}
|