aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty/cty/set/gob.go
blob: da2978f655dd631efe8bf45d79075a792d13a8f1 (plain) (blame)
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
package set

import (
	"bytes"
	"encoding/gob"
	"fmt"
)

// GobEncode is an implementation of the interface gob.GobEncoder, allowing
// sets to be included in structures encoded via gob.
//
// The set rules are included in the serialized value, so the caller must
// register its concrete rules type with gob.Register before using a
// set in a gob, and possibly also implement GobEncode/GobDecode to customize
// how any parameters are persisted.
//
// The set elements are also included, so if they are of non-primitive types
// they too must be registered with gob.
//
// If the produced gob values will persist for a long time, the caller must
// ensure compatibility of the rules implementation. In particular, if the
// definition of element equivalence changes between encoding and decoding
// then two distinct stored elements may be considered equivalent on decoding,
// causing the recovered set to have fewer elements than when it was stored.
func (s Set) GobEncode() ([]byte, error) {
	gs := gobSet{
		Version: 0,
		Rules:   s.rules,
		Values:  s.Values(),
	}

	buf := &bytes.Buffer{}
	enc := gob.NewEncoder(buf)
	err := enc.Encode(gs)
	if err != nil {
		return nil, fmt.Errorf("error encoding set.Set: %s", err)
	}

	return buf.Bytes(), nil
}

// GobDecode is the opposite of GobEncode. See GobEncode for information
// on the requirements for and caveats of including set values in gobs.
func (s *Set) GobDecode(buf []byte) error {
	r := bytes.NewReader(buf)
	dec := gob.NewDecoder(r)

	var gs gobSet
	err := dec.Decode(&gs)
	if err != nil {
		return fmt.Errorf("error decoding set.Set: %s", err)
	}
	if gs.Version != 0 {
		return fmt.Errorf("unsupported set.Set encoding version %d; need 0", gs.Version)
	}

	victim := NewSetFromSlice(gs.Rules, gs.Values)
	s.vals = victim.vals
	s.rules = victim.rules
	return nil
}

type gobSet struct {
	Version int
	Rules   Rules

	// The bucket-based representation is for efficient in-memory access, but
	// for serialization it's enough to just retain the values themselves,
	// which we can re-bucket using the rules (which may have changed!) when
	// we re-inflate.
	Values []interface{}
}

func init() {
	gob.Register([]interface{}(nil))
}