summaryrefslogtreecommitdiff
path: root/helpers.go
blob: 1533750008659c4e40eb06d7be75b6acf489ef54 (plain)
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
package virtbuf

// functions to import and export the protobuf
// data to and from config files

import (
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/encoding/prototext"
	"google.golang.org/protobuf/proto"
)

func InitCluster() *Cluster {
	var c *Cluster
	c = new(Cluster)
	c.d = new(Droplets)
	c.H = new(Hypervisors)
	c.e = new(Events)
	return c
}

// human readable JSON
func (d *Droplets) FormatJSON() string {
	return protojson.Format(d)
}

func (d *Droplet) FormatJSON() string {
	return protojson.Format(d)
}

func (e *Events) FormatJSON() string {
	return protojson.Format(e)
}

func (h *Hypervisors) FormatJSON() string {
	return protojson.Format(h)
}

// apparently this isn't supposed to be used?
// https://protobuf.dev/reference/go/faq/#unstable-text
// this is a shame because this is much nicer output than JSON Format()
func (d *Droplets) FormatTEXT() string {
	return prototext.Format(d)
}

func (d *Droplet) FormatTEXT() string {
	return prototext.Format(d)
}

func (e *Events) FormatTEXT() string {
	return prototext.Format(e)
}

// marshal
func (d *Droplets) MarshalJSON() ([]byte, error) {
	return protojson.Marshal(d)
}

func (d *Droplet) MarshalJSON() ([]byte, error) {
	return protojson.Marshal(d)
}

func (e *Events) MarshalJSON() ([]byte, error) {
	return protojson.Marshal(e)
}

// unmarshal
func (d *Droplets) UnmarshalJSON(data []byte) error {
	return protojson.Unmarshal(data, d)
}

func (d *Droplet) UnmarshalJSON(data []byte) error {
	return protojson.Unmarshal(data, d)
}

func (e *Events) UnmarshalJSON(data []byte) error {
	return protojson.Unmarshal(data, e)
}

func (d *Droplet) Unmarshal(data []byte) error {
	return proto.Unmarshal(data, d)
}