forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
165 lines (140 loc) · 3.56 KB
/
entry.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
// Copyright (C) 2013-2017, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
// Use of this source code is governed by GPLv3 found in the LICENSE file
//----------------------------------------------------------------------------------------
// implements chain entry structures and functions
package holochain
import (
"bytes"
"encoding/binary"
"encoding/gob"
"encoding/json"
"github.com/lestrrat/go-jsschema"
"github.com/lestrrat/go-jsval"
"github.com/lestrrat/go-jsval/builder"
"io"
)
const (
DNAEntryType = "_dna"
KeyEntryType = "_key"
)
// EntryDef struct holds an entry definition
type EntryDef struct {
Name string
DataFormat string
Schema string // file name of schema or language schema directive
SchemaHash Hash
validator SchemaValidator
}
// Entry describes serialization and deserialziation of entry data
type Entry interface {
Marshal() ([]byte, error)
Unmarshal([]byte) error
Content() interface{}
}
// SchemaValidator interface for schema validation
type SchemaValidator interface {
Validate(interface{}) error
}
// GobEntry is a structure for implementing Gob encoding of Entry content
type GobEntry struct {
C interface{}
}
// JSONEntry is a structure for implementing JSON encoding of Entry content
type JSONEntry struct {
C interface{}
}
// ByteEncoder encodes anything using gob
func ByteEncoder(data interface{}) (b []byte, err error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(data)
if err != nil {
return
}
b = buf.Bytes()
return
}
// ByteEncoder decodes data encoded by ByteEncoder
func ByteDecoder(b []byte, to interface{}) (err error) {
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
err = dec.Decode(to)
return
}
// MarshalEntry serializes an entry to a writer
func MarshalEntry(writer io.Writer, e Entry) (err error) {
var b []byte
b, err = e.Marshal()
l := uint64(len(b))
err = binary.Write(writer, binary.LittleEndian, l)
if err != nil {
return
}
err = binary.Write(writer, binary.LittleEndian, b)
return
}
// UnmarshalEntry unserializes an entry from a reader
func UnmarshalEntry(reader io.Reader) (e Entry, err error) {
var l uint64
err = binary.Read(reader, binary.LittleEndian, &l)
if err != nil {
return
}
var b = make([]byte, l)
err = binary.Read(reader, binary.LittleEndian, b)
if err != nil {
return
}
var g GobEntry
err = g.Unmarshal(b)
e = &g
return
}
// implementation of Entry interface with gobs
func (e *GobEntry) Marshal() (b []byte, err error) {
b, err = ByteEncoder(&e.C)
return
}
func (e *GobEntry) Unmarshal(b []byte) (err error) {
err = ByteDecoder(b, &e.C)
return
}
func (e *GobEntry) Content() interface{} { return e.C }
// implementation of Entry interface with JSON
func (e *JSONEntry) Marshal() (b []byte, err error) {
j, err := json.Marshal(e.C)
if err != nil {
return
}
b = []byte(j)
return
}
func (e *JSONEntry) Unmarshal(b []byte) (err error) {
err = json.Unmarshal(b, &e.C)
return
}
func (e *JSONEntry) Content() interface{} { return e.C }
type JSONSchemaValidator struct {
v *jsval.JSVal
}
// implementation of SchemaValidator with JSONSchema
func (v *JSONSchemaValidator) Validate(entry interface{}) (err error) {
err = v.v.Validate(entry)
return
}
// BuildJSONSchemaValidator builds a validator in an EntryDef
func (d *EntryDef) BuildJSONSchemaValidator(path string) (err error) {
var s *schema.Schema
s, err = schema.ReadFile(path + "/" + d.Schema)
if err != nil {
return
}
b := builder.New()
var v JSONSchemaValidator
v.v, err = b.Build(s)
if err == nil {
v.v.SetName(d.Schema)
d.validator = &v
}
return
}