Skip to content

Commit

Permalink
proto: Port example.go to proto3
Browse files Browse the repository at this point in the history
Using jsonpb [1,2].

Drop the proto.String bits to avoid errors like:

  ./example.go:17: cannot use proto.String("linux") (type *string) as
    type string in field value

And use a reference to s to avoid:

  ./example.go:29: cannot use s (type oci.LinuxSpec) as type
     proto.Message in argument to marshaler.Marshal: oci.LinuxSpec
     does not implement proto.Message (ProtoMessage method has pointer
     receiver)

[1]: golang/protobuf#44
[2]: http://godoc.org/github.com/golang/protobuf/jsonpb

Signed-off-by: W. Trevor King <wking@tremily.us>
  • Loading branch information
wking committed Sep 29, 2015
1 parent 932c50d commit 459cecb
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions proto/example.go
Expand Up @@ -3,27 +3,31 @@
package main

import (
"encoding/json"
"log"
"os"

oci "./go/"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/jsonpb"
)

func main() {
s := oci.LinuxSpec{
s := &oci.LinuxSpec{
Spec: &oci.Spec{
Platform: &oci.Platform{Os: proto.String("linux"), Arch: proto.String("x86_64")},
Platform: &oci.Platform{Os: "linux", Arch: "x86_64"},
Process: &oci.Process{
Cwd: proto.String("/"),
Cwd: "/",
Env: []string{"TERM=linux"},
},
},
}

buf, err := json.MarshalIndent(s, "", " ")
marshaler := jsonpb.Marshaler{
EnumsAsString: true,
Indent: " ",
}

err := marshaler.Marshal(os.Stdout, s)
if err != nil {
log.Fatal(err)
}
println(string(buf))
}

0 comments on commit 459cecb

Please sign in to comment.