forked from helm/helm-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
60 lines (53 loc) · 1.62 KB
/
codec.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
// Package codec provides a JSON/YAML codec for Manifests.
//
// Usage:
//
// // Decode one manifest from a JSON file.
// man, err := JSON.Decode(b).One()
// // Decode all of the manifests out of this file.
// manifests, err := YAML.Decode(b).All()
// err := YAML.Encode(filename).One("hello")
// // Encode multiple objects to one file (as separate docs).
// err := YAML.Encode(filename).All("one", "two", "three")
package codec
import (
"io"
"io/ioutil"
)
// JSON is the default JSON encoder/decoder.
var JSON jsonCodec
// YAML is the default YAML encoder/decoder.
var YAML yamlCodec
// Encoder describes something capable of encoding to a given format.
//
// An Encoder should be able to encode one object to an output stream, or
// many objects to an output stream.
//
// For example, a single YAML file can contain multiple YAML objects, and
// a single JSONList file can contain many JSON objects.
type Encoder interface {
// Write one object to one file
One(interface{}) error
// Write all objects to one file
All(...interface{}) error
}
// Decoder decodes an encoded representation into one or many objects.
type Decoder interface {
// Get one object from a file.
One() (*Object, error)
// Get all objects from a file.
All() ([]*Object, error)
}
// Codec has an encoder and a decoder for a particular encoding.
type Codec interface {
Encode(io.Writer) Encoder
Decode([]byte) Decoder
}
// DecodeFile returns a decoder pre-populated with the file contents.
func DecodeFile(filename string, c Codec) (Decoder, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return c.Decode(data), nil
}