forked from pganalyze/pg_query_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsetree_list.go
48 lines (38 loc) · 978 Bytes
/
parsetree_list.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
package pg_query
import (
"encoding/hex"
"encoding/json"
"fmt"
nodes "github.com/lfittl/pg_query_go/nodes"
)
type ParsetreeList struct {
Statements []nodes.Node
}
func (input ParsetreeList) MarshalJSON() ([]byte, error) {
type ParsetreeListAlias ParsetreeList
return json.Marshal(input.Statements)
}
func (output *ParsetreeList) UnmarshalJSON(input []byte) (err error) {
var list []json.RawMessage
err = json.Unmarshal([]byte(input), &list)
if err != nil {
return
}
for _, nodeJson := range list {
var node nodes.Node
node, err = nodes.UnmarshalNodeJSON(nodeJson)
if err != nil {
return
}
output.Statements = append(output.Statements, node)
}
return
}
func (input ParsetreeList) Fingerprint() string {
const fingerprintVersion uint = 1
ctx := nodes.NewFingerprintHashContext()
for _, node := range input.Statements {
node.Fingerprint(ctx, nil, "")
}
return fmt.Sprintf("%02x%s", fingerprintVersion, hex.EncodeToString(ctx.Sum()))
}