/
record_unknown.go
47 lines (39 loc) · 922 Bytes
/
record_unknown.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
package types
import (
"encoding/json"
"github.com/gocnpan/boxo/routing/http/internal/drjson"
)
var _ Record = &UnknownRecord{}
type UnknownRecord struct {
Schema string
// Bytes contains the raw JSON bytes that were used to unmarshal this record.
// This value can be used, for example, to unmarshal de record into a different
// type if Schema is of a known value.
Bytes []byte
}
func (ur *UnknownRecord) GetSchema() string {
return ur.Schema
}
func (ur *UnknownRecord) UnmarshalJSON(b []byte) error {
v := struct {
Schema string
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
ur.Schema = v.Schema
ur.Bytes = b
return nil
}
func (ur UnknownRecord) MarshalJSON() ([]byte, error) {
m := map[string]interface{}{}
if ur.Bytes != nil {
err := json.Unmarshal(ur.Bytes, &m)
if err != nil {
return nil, err
}
}
m["Schema"] = ur.Schema
return drjson.MarshalJSONBytes(m)
}