11package continuity
22
33import (
4+ "bytes"
45 "fmt"
56 "io"
67 "log"
78 "os"
89 "sort"
910
11+ "github.com/golang/protobuf/jsonpb"
1012 "github.com/golang/protobuf/proto"
1113 pb "github.com/stevvooe/continuity/proto"
1214)
1315
16+ const (
17+ // MediaTypeManifestV0Protobuf is the media type for manifest formatted as protobuf.
18+ // The format is unstable during v0.
19+ MediaTypeManifestV0Protobuf = "application/vnd.continuity.manifest.v0+pb"
20+ // MediaTypeManifestV0JSON is the media type for manifest formatted as JSON.
21+ // JSON is marshalled from protobuf using jsonpb.Marshaler
22+ // ({EnumAsInts = false, EmitDefaults = false, OrigName = false})
23+ // The format is unstable during v0.
24+ MediaTypeManifestV0JSON = "application/vnd.continuity.manifest.v0+json"
25+ )
26+
1427// Manifest provides the contents of a manifest. Users of this struct should
1528// not typically modify any fields directly.
1629type Manifest struct {
1730 // Resources specifies all the resources for a manifest in order by path.
1831 Resources []Resource
1932}
2033
21- func Unmarshal (p []byte ) (* Manifest , error ) {
22- var bm pb.Manifest
23-
24- if err := proto .Unmarshal (p , & bm ); err != nil {
25- return nil , err
26- }
27-
34+ func manifestFromProto (bm * pb.Manifest ) (* Manifest , error ) {
2835 var m Manifest
2936 for _ , b := range bm .Resource {
3037 r , err := fromProto (b )
@@ -34,26 +41,48 @@ func Unmarshal(p []byte) (*Manifest, error) {
3441
3542 m .Resources = append (m .Resources , r )
3643 }
37-
3844 return & m , nil
3945}
4046
41- func Marshal ( m * Manifest ) ( []byte , error ) {
47+ func Unmarshal ( p []byte ) ( * Manifest , error ) {
4248 var bm pb.Manifest
43- for _ , resource := range m . Resources {
44- bm . Resource = append ( bm . Resource , toProto ( resource ))
49+ if err := proto . Unmarshal ( p , & bm ); err != nil {
50+ return nil , err
4551 }
52+ return manifestFromProto (& bm )
53+ }
4654
47- return proto .Marshal (& bm )
55+ func UnmarshalJSON (p []byte ) (* Manifest , error ) {
56+ var bm pb.Manifest
57+ if err := jsonpb .Unmarshal (bytes .NewReader (p ), & bm ); err != nil {
58+ return nil , err
59+ }
60+ return manifestFromProto (& bm )
4861}
4962
50- func MarshalText ( w io. Writer , m * Manifest ) error {
63+ func manifestToProto ( m * Manifest ) * pb. Manifest {
5164 var bm pb.Manifest
5265 for _ , resource := range m .Resources {
5366 bm .Resource = append (bm .Resource , toProto (resource ))
5467 }
68+ return & bm
69+ }
5570
56- return proto .MarshalText (w , & bm )
71+ func Marshal (m * Manifest ) ([]byte , error ) {
72+ return proto .Marshal (manifestToProto (m ))
73+ }
74+
75+ func MarshalText (w io.Writer , m * Manifest ) error {
76+ return proto .MarshalText (w , manifestToProto (m ))
77+ }
78+
79+ func MarshalJSON (m * Manifest ) ([]byte , error ) {
80+ var b bytes.Buffer
81+ marshaler := & jsonpb.Marshaler {}
82+ if err := marshaler .Marshal (& b , manifestToProto (m )); err != nil {
83+ return nil , err
84+ }
85+ return b .Bytes (), nil
5786}
5887
5988// BuildManifest creates the manifest for the given context
0 commit comments