Skip to content

Commit 459cecb

Browse files
committed
proto: Port example.go to proto3
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 <[email protected]>
1 parent 932c50d commit 459cecb

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

proto/example.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@
33
package main
44

55
import (
6-
"encoding/json"
76
"log"
7+
"os"
88

99
oci "./go/"
10-
"github.com/golang/protobuf/proto"
10+
"github.com/golang/protobuf/jsonpb"
1111
)
1212

1313
func main() {
14-
s := oci.LinuxSpec{
14+
s := &oci.LinuxSpec{
1515
Spec: &oci.Spec{
16-
Platform: &oci.Platform{Os: proto.String("linux"), Arch: proto.String("x86_64")},
16+
Platform: &oci.Platform{Os: "linux", Arch: "x86_64"},
1717
Process: &oci.Process{
18-
Cwd: proto.String("/"),
18+
Cwd: "/",
1919
Env: []string{"TERM=linux"},
2020
},
2121
},
2222
}
2323

24-
buf, err := json.MarshalIndent(s, "", " ")
24+
marshaler := jsonpb.Marshaler{
25+
EnumsAsString: true,
26+
Indent: " ",
27+
}
28+
29+
err := marshaler.Marshal(os.Stdout, s)
2530
if err != nil {
2631
log.Fatal(err)
2732
}
28-
println(string(buf))
2933
}

0 commit comments

Comments
 (0)