aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/goccy/go-json/internal/decoder/func.go
blob: 4cc12ca81f14251db8d075ecf7e60451c8a8ad90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package decoder

import (
	"bytes"
	"fmt"
	"unsafe"

	"github.com/goccy/go-json/internal/errors"
	"github.com/goccy/go-json/internal/runtime"
)

type funcDecoder struct {
	typ        *runtime.Type
	structName string
	fieldName  string
}

func newFuncDecoder(typ *runtime.Type, structName, fieldName string) *funcDecoder {
	fnDecoder := &funcDecoder{typ, structName, fieldName}
	return fnDecoder
}

func (d *funcDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
	s.skipWhiteSpace()
	start := s.cursor
	if err := s.skipValue(depth); err != nil {
		return err
	}
	src := s.buf[start:s.cursor]
	if len(src) > 0 {
		switch src[0] {
		case '"':
			return &errors.UnmarshalTypeError{
				Value:  "string",
				Type:   runtime.RType2Type(d.typ),
				Offset: s.totalOffset(),
			}
		case '[':
			return &errors.UnmarshalTypeError{
				Value:  "array",
				Type:   runtime.RType2Type(d.typ),
				Offset: s.totalOffset(),
			}
		case '{':
			return &errors.UnmarshalTypeError{
				Value:  "object",
				Type:   runtime.RType2Type(d.typ),
				Offset: s.totalOffset(),
			}
		case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			return &errors.UnmarshalTypeError{
				Value:  "number",
				Type:   runtime.RType2Type(d.typ),
				Offset: s.totalOffset(),
			}
		case 'n':
			if err := nullBytes(s); err != nil {
				return err
			}
			*(*unsafe.Pointer)(p) = nil
			return nil
		case 't':
			if err := trueBytes(s); err == nil {
				return &errors.UnmarshalTypeError{
					Value:  "boolean",
					Type:   runtime.RType2Type(d.typ),
					Offset: s.totalOffset(),
				}
			}
		case 'f':
			if err := falseBytes(s); err == nil {
				return &errors.UnmarshalTypeError{
					Value:  "boolean",
					Type:   runtime.RType2Type(d.typ),
					Offset: s.totalOffset(),
				}
			}
		}
	}
	return errors.ErrInvalidBeginningOfValue(s.buf[s.cursor], s.totalOffset())
}

func (d *funcDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
	buf := ctx.Buf
	cursor = skipWhiteSpace(buf, cursor)
	start := cursor
	end, err := skipValue(buf, cursor, depth)
	if err != nil {
		return 0, err
	}
	src := buf[start:end]
	if len(src) > 0 {
		switch src[0] {
		case '"':
			return 0, &errors.UnmarshalTypeError{
				Value:  "string",
				Type:   runtime.RType2Type(d.typ),
				Offset: start,
			}
		case '[':
			return 0, &errors.UnmarshalTypeError{
				Value:  "array",
				Type:   runtime.RType2Type(d.typ),
				Offset: start,
			}
		case '{':
			return 0, &errors.UnmarshalTypeError{
				Value:  "object",
				Type:   runtime.RType2Type(d.typ),
				Offset: start,
			}
		case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			return 0, &errors.UnmarshalTypeError{
				Value:  "number",
				Type:   runtime.RType2Type(d.typ),
				Offset: start,
			}
		case 'n':
			if bytes.Equal(src, nullbytes) {
				*(*unsafe.Pointer)(p) = nil
				return end, nil
			}
		case 't':
			if err := validateTrue(buf, start); err == nil {
				return 0, &errors.UnmarshalTypeError{
					Value:  "boolean",
					Type:   runtime.RType2Type(d.typ),
					Offset: start,
				}
			}
		case 'f':
			if err := validateFalse(buf, start); err == nil {
				return 0, &errors.UnmarshalTypeError{
					Value:  "boolean",
					Type:   runtime.RType2Type(d.typ),
					Offset: start,
				}
			}
		}
	}
	return cursor, errors.ErrInvalidBeginningOfValue(buf[cursor], cursor)
}

func (d *funcDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
	return nil, 0, fmt.Errorf("json: func decoder does not support decode path")
}