forked from segmentio/objconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
143 lines (120 loc) · 5.09 KB
/
parse.go
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
package objconv
import (
"io"
"time"
)
// The Parser interface must be implemented by types that provide decoding of a
// specific format (like json, resp, ...).
//
// Parsers are not expected to be safe for use by multiple goroutines.
type Parser interface {
Buffered() io.Reader
// ParseType is called by a decoder to ask the parser what is the type of
// the next value that can be parsed.
//
// ParseType must be idempotent, it must be possible to call it multiple
// without actually changing the state of the parser.
ParseType() (Type, error)
// ParseNil parses a nil value.
ParseNil() error
// ParseBool parses a boolean value.
ParseBool() (bool, error)
// ParseInt parses an integer value.
ParseInt() (int64, error)
// ParseUint parses an unsigned integer value.
ParseUint() (uint64, error)
// ParseFloat parses a floating point value.
ParseFloat() (float64, error)
// ParseString parses a string value.
//
// The string is returned as a byte slice because it is expected to be
// pointing at an internal memory buffer, the decoder will make a copy of
// the value. This design allows more memory allocation optimizations.
ParseString() ([]byte, error)
// ParseBytes parses a byte array value.
//
// The returned byte slice is expected to be pointing at an internal memory
// buffer, the decoder will make a copy of the value. This design allows more
// memory allocation optimizations.
ParseBytes() ([]byte, error)
// ParseTime parses a time value.
ParseTime() (time.Time, error)
// ParseDuration parses a duration value.
ParseDuration() (time.Duration, error)
// ParseError parses an error value.
ParseError() (error, error)
// ParseArrayBegin is called by the array-decoding algorithm when it starts.
//
// The method should return the length of the array being decoded, or a
// negative value if it is unknown (some formats like json don't keep track
// of the length of the array).
ParseArrayBegin() (int, error)
// ParseArrayEnd is called by the array-decoding algorithm when it
// completes.
//
// The method receives the iteration counter as argument, which indicates
// how many values were decoded from the array.
ParseArrayEnd(int) error
// ParseArrayNext is called by the array-decoding algorithm between each
// value parsed in the array.
//
// The method receives the iteration counter as argument, which indicates
// how many values were decoded from the array.
//
// If the ParseArrayBegin method returned a negative value this method
// should return objconv.End to indicated that there is no more elements to
// parse in the array. In this case the method is also called right before
// decoding the first element ot handle the case where the array is empty
// and the end-of-array marker can be read right away.
ParseArrayNext(int) error
// ParseMapBegin is called by the map-decoding algorithm when it starts.
//
// The method should return the length of the map being decoded, or a
// negative value if it is unknown (some formats like json don't keep track
// of the length of the map).
ParseMapBegin() (int, error)
// ParseMapEnd is called by the map-decoding algorithm when it completes.
//
// The method receives the iteration counter as argument, which indicates
// how many values were decoded from the map.
ParseMapEnd(int) error
// ParseMapValue is called by the map-decoding algorithm after parsing a key
// but before parsing the associated value.
//
// The method receives the iteration counter as argument, which indicates
// how many values were decoded from the map.
ParseMapValue(int) error
// ParseMapNext is called by the map-decoding algorithm between each
// value parsed in the map.
//
// The method receives the iteration counter as argument, which indicates
// how many values were decoded from the map.
//
// If the ParseMapBegin method returned a negative value this method should
// return objconv.End to indicated that there is no more elements to parse
// in the map. In this case the method is also called right before decoding
// the first element ot handle the case where the array is empty and the
// end-of-map marker can be read right away.
ParseMapNext(int) error
}
// The bytesDecoder interface may optionnaly be implemented by a Parser to
// provide an extra step in decoding a byte slice. This is sometimes necessary
// if the associated Emitter has transformed bytes slices because the format is
// not capable of representing binary data.
type bytesDecoder interface {
// DecodeBytes is called when the destination variable for a string or a
// byte slice is a byte slice, allowing the parser to apply a transformation
// before the value is stored.
DecodeBytes([]byte) ([]byte, error)
}
// The textParser interface may be implemented by parsers of human-readable
// formats. Such parsers instruct the encoder to prefer using
// encoding.TextUnmarshaler over encoding.BinaryUnmarshaler for example.
type textParser interface {
// EmitsText returns true if the parser produces a human-readable format.
TextParser() bool
}
func isTextParser(parser Parser) bool {
p, _ := parser.(textParser)
return p != nil && p.TextParser()
}