forked from amazon-ion/ion-hash-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_writer.go
385 lines (327 loc) · 9.68 KB
/
hash_writer.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package ionhash
import (
"math/big"
"github.com/amzn/ion-go/ion"
)
// A HashWriter writes a stream of Ion values and calculates its hash.
//
// The various Write methods write atomic values to the current output stream. Methods
// prefixed with Begin start writing a list, sexp, or struct respectively. Subsequent
// calls to Write will write values inside of the container until a matching
// End method is called, e.g.,
//
// var hw HashWriter
// hw.BeginSexp()
// {
// hw.WriteInt(1)
// hw.WriteSymbol("+")
// hw.WriteInt(1)
// }
// hw.EndSexp()
//
// When writing values inside a struct, the FieldName method must be called before
// each value to set the value's field name. The Annotation method may likewise
// be called before writing any value to add an annotation to the value.
//
// var hw HashWriter
// hw.Annotation("user")
// hw.BeginStruct()
// {
// hw.FieldName("id")
// hw.WriteString("foo")
// hw.FieldName("name")
// hw.WriteString("bar")
// }
// hw.EndStruct()
//
// When you're done writing values, you should call Finish to ensure everything has
// been flushed from in-memory buffers. While individual methods all return an error
// on failure, implementations will remember any errors, no-op subsequent calls, and
// return the previous error. This lets you keep code a bit cleaner by only checking
// the return value of the final method call (generally Finish).
//
// Sum will return the hash of the entire stream of Ion values that have been written thus far.
//
// var hw HashWriter
// writeSomeStuff(hw)
// if err := hw.Finish(); err != nil {
// return err
// }
// fmt.Printf("%v", hw.Sum(nil))
//
type HashWriter interface {
// Embed interface of Ion writer.
ion.Writer
// Remaining hashValue methods.
getFieldName() (*ion.SymbolToken, error)
getAnnotations() ([]ion.SymbolToken, error)
IsNull() bool
Type() ion.Type
value() (interface{}, error)
// Sum appends the current hash to b and returns the resulting slice.
// It resets the Hash to its initial state.
Sum(b []byte) ([]byte, error)
}
type hashWriter struct {
ionWriter ion.Writer
hasher hasher
currentFieldName *ion.SymbolToken
currentType ion.Type
currentValue interface{}
currentIsNull bool
annotations []ion.SymbolToken
}
// NewHashWriter takes an Ion Writer and a hash provider and returns a new HashWriter.
func NewHashWriter(ionWriter ion.Writer, hasherProvider IonHasherProvider) (HashWriter, error) {
newHasher, err := newHasher(hasherProvider)
if err != nil {
return nil, err
}
return &hashWriter{ionWriter: ionWriter, hasher: *newHasher}, nil
}
// FieldName sets the field name for the next value written.
// It may only be called while writing a struct.
func (hw *hashWriter) FieldName(val string) error {
token, err := ion.NewSymbolToken(ion.V1SystemSymbolTable, val)
if err != nil {
return err
}
hw.currentFieldName = &token
return hw.ionWriter.FieldName(val)
}
// Annotation adds an annotation to the next value written.
func (hw *hashWriter) Annotation(val ion.SymbolToken) error {
hw.annotations = append(hw.annotations, val)
return hw.ionWriter.Annotation(val)
}
// Annotations adds one or more annotations to the next value written.
func (hw *hashWriter) Annotations(values ...ion.SymbolToken) error {
hw.annotations = append(hw.annotations, values...)
return hw.ionWriter.Annotations(values...)
}
// WriteNull writes an untyped null value.
func (hw *hashWriter) WriteNull() error {
err := hw.hashScalar(ion.NullType, nil)
if err != nil {
return err
}
return hw.ionWriter.WriteNull()
}
// WriteNullType writes a null value with a type qualifier, e.g. null.bool.
func (hw *hashWriter) WriteNullType(ionType ion.Type) error {
err := hw.hashScalar(ionType, nil)
if err != nil {
return err
}
return hw.ionWriter.WriteNullType(ionType)
}
// WriteBool writes a boolean value.
func (hw *hashWriter) WriteBool(val bool) error {
err := hw.hashScalar(ion.BoolType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteBool(val)
}
// WriteInt writes an integer value.
func (hw *hashWriter) WriteInt(val int64) error {
err := hw.hashScalar(ion.IntType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteInt(val)
}
// WriteUint writes an unsigned integer value.
func (hw *hashWriter) WriteUint(val uint64) error {
err := hw.hashScalar(ion.IntType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteUint(val)
}
// WriteBigInt writes a big integer value.
func (hw *hashWriter) WriteBigInt(val *big.Int) error {
err := hw.hashScalar(ion.IntType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteBigInt(val)
}
// WriteFloat writes a floating-point value.
func (hw *hashWriter) WriteFloat(val float64) error {
err := hw.hashScalar(ion.FloatType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteFloat(val)
}
// WriteDecimal writes an arbitrary-precision decimal value.
func (hw *hashWriter) WriteDecimal(val *ion.Decimal) error {
err := hw.hashScalar(ion.DecimalType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteDecimal(val)
}
// WriteTimestamp writes a timestamp value.
func (hw *hashWriter) WriteTimestamp(val ion.Timestamp) error {
err := hw.hashScalar(ion.TimestampType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteTimestamp(val)
}
// WriteSymbol writes a symbol value.
func (hw *hashWriter) WriteSymbol(val string) error {
err := hw.hashScalar(ion.SymbolType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteSymbol(val)
}
// WriteString writes a string value.
func (hw *hashWriter) WriteString(val string) error {
err := hw.hashScalar(ion.StringType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteString(val)
}
// WriteClob writes a clob value.
func (hw *hashWriter) WriteClob(val []byte) error {
err := hw.hashScalar(ion.ClobType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteClob(val)
}
// WriteBlob writes a blob value.
func (hw *hashWriter) WriteBlob(val []byte) error {
err := hw.hashScalar(ion.BlobType, val)
if err != nil {
return err
}
return hw.ionWriter.WriteBlob(val)
}
// BeginList begins writing a list value.
func (hw *hashWriter) BeginList() error {
err := hw.stepIn(ion.ListType)
if err != nil {
return err
}
return hw.ionWriter.BeginList()
}
// EndList finishes writing a list value.
func (hw *hashWriter) EndList() error {
err := hw.hasher.stepOut()
if err != nil {
return err
}
return hw.ionWriter.EndList()
}
// BeginSexp begins writing an s-expression value.
func (hw *hashWriter) BeginSexp() error {
err := hw.stepIn(ion.SexpType)
if err != nil {
return err
}
return hw.ionWriter.BeginSexp()
}
// EndSexp finishes writing an s-expression value.
func (hw *hashWriter) EndSexp() error {
err := hw.hasher.stepOut()
if err != nil {
return err
}
return hw.ionWriter.EndSexp()
}
// BeginStruct begins writing a struct value.
func (hw *hashWriter) BeginStruct() error {
err := hw.stepIn(ion.StructType)
if err != nil {
return err
}
return hw.ionWriter.BeginStruct()
}
// EndStruct finishes writing a struct value.
func (hw *hashWriter) EndStruct() error {
err := hw.hasher.stepOut()
if err != nil {
return err
}
return hw.ionWriter.EndStruct()
}
// Finish finishes writing values and flushes any buffered data.
func (hw *hashWriter) Finish() error {
return hw.ionWriter.Finish()
}
// Sum appends the current hash to b and returns the resulting slice.
// It resets the Hash to its initial state.
func (hw *hashWriter) Sum(b []byte) ([]byte, error) {
return hw.hasher.sum(b)
}
func (hw *hashWriter) FieldNameSymbol(val ion.SymbolToken) error {
return hw.ionWriter.FieldNameSymbol(val)
}
// The following implements hashValue interface.
func (hw *hashWriter) getFieldName() (*ion.SymbolToken, error) {
return hw.currentFieldName, nil
}
func (hw *hashWriter) getAnnotations() ([]ion.SymbolToken, error) {
return hw.annotations, nil
}
// IsNull returns true if the current value is an explicit null. This may be true
// even if the Type is not NullType (for example, null.struct has type Struct).
func (hw *hashWriter) IsNull() bool {
return hw.currentIsNull
}
// Type returns the type of the Ion value the hashWriter is currently positioned on.
// It returns NoType if the hashWriter is positioned before or after a value.
func (hw *hashWriter) Type() ion.Type {
return hw.currentType
}
func (hw *hashWriter) value() (interface{}, error) {
return hw.currentValue, nil
}
// IsInStruct indicates if the writer is currently positioned inside a struct.
func (hw *hashWriter) IsInStruct() bool {
return hw.ionWriter.IsInStruct()
}
func (hw *hashWriter) hashScalar(ionType ion.Type, value interface{}) error {
hw.currentType = ionType
hw.currentValue = value
hw.currentIsNull = value == nil
err := hw.hasher.scalar(hw)
if err != nil {
return err
}
hw.currentFieldName = nil
hw.annotations = nil
return nil
}
func (hw *hashWriter) stepIn(ionType ion.Type) error {
hw.currentType = ionType
hw.currentValue = nil
hw.currentIsNull = false
err := hw.hasher.stepIn(hw)
if err != nil {
return err
}
hw.currentFieldName = nil
hw.annotations = nil
return nil
}