Skip to content

Instantly share code, notes, and snippets.

@pcn
Created January 15, 2019 15:40
Show Gist options
  • Select an option

  • Save pcn/61d2511cfb2f8fb8b2b81f2a77f80d3c to your computer and use it in GitHub Desktop.

Select an option

Save pcn/61d2511cfb2f8fb8b2b81f2a77f80d3c to your computer and use it in GitHub Desktop.

Revisions

  1. pcn created this gist Jan 15, 2019.
    85 changes: 85 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    package main

    import (
    "context"
    "fmt"
    "reflect"

    docopt "github.com/docopt/docopt-go"

    "github.com/gogo/protobuf/jsonpb"
    "github.com/gogo/protobuf/proto"
    _ "github.com/golang/protobuf/ptypes/wrappers"
    "github.com/stripe/skycfg"
    yaml "gopkg.in/yaml.v2"

    gogo_proto "github.com/gogo/protobuf/proto"

    _ "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
    _ "github.com/envoyproxy/go-control-plane/envoy/api/v2"
    _ "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v2"
    )

    type protoRegistry struct{}

    func (*protoRegistry) UnstableProtoMessageType(name string) (reflect.Type, error) {
    if t := proto.MessageType(name); t != nil {
    return t, nil
    }
    if t := gogo_proto.MessageType(name); t != nil {
    return t, nil
    }
    return nil, nil
    }

    func (*protoRegistry) UnstableEnumValueMap(name string) map[string]int32 {
    if ev := proto.EnumValueMap(name); ev != nil {
    return ev
    }
    if ev := gogo_proto.EnumValueMap(name); ev != nil {
    return ev
    }
    return nil
    }

    func main() {

    usage := `envoy-proto: creates an envoy configuration from a skycfg file
    Usage:
    envoy-proto <input-file>
    envoy-proto -h | --help
    Options:
    <input-file> skycfg file to be read
    -h --help Show this screen.`

    arguments, _ := docopt.ParseDoc(usage)

    ctx := context.Background()
    config, err := skycfg.Load(ctx, arguments["<input-file>"].(string), skycfg.WithProtoRegistry(&protoRegistry{}))
    if err != nil {
    panic(err)
    }
    messages, err := config.Main(ctx)
    if err != nil {
    panic(err)
    }
    for _, msg := range messages {
    var jsonMarshaler = &jsonpb.Marshaler{OrigName: true}

    marshaled, err := jsonMarshaler.MarshalToString(msg)
    sep := ""
    var yamlMap yaml.MapSlice
    if err := yaml.Unmarshal([]byte(marshaled), &yamlMap); err != nil {
    panic(fmt.Sprintf("yaml.Unmarshal: %v", err))
    }
    yamlMarshaled, err := yaml.Marshal(yamlMap)
    if err != nil {
    panic(fmt.Sprintf("yaml.Marshal: %v", err))
    }
    marshaled = string(yamlMarshaled)
    sep = "---\n"
    fmt.Printf("%s%s\n", sep, marshaled)
    }
    }
    11 changes: 11 additions & 0 deletions time_reproducer.sky
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    pb = proto.package("google.protobuf")
    v2 = proto.package("envoy.api.v2")

    def duration():
    return v2.Cluster(
    lb_policy=v2.Cluster.LbPolicy.ROUND_ROBIN, # RR is the default, but let's be explicit
    type=v2.Cluster.DiscoveryType.STRICT_DNS,
    connect_timeout=pb.Duration(seconds=0, nanos=500000000), #0.5 seconds
    name="no_duration")

    duration()
    23 changes: 23 additions & 0 deletions what_I_see.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    Did a fresh "go get" on the above to get skycfg from master; see the following:
    ```
    |S|spacey@masonjar:~/go/src/github.com/pcn/reproducer$ (cd ../../stripe/skycfg/; git log | head)
    commit fd6aafa80b336fb3a4a5e35c8510d7bb375fa862
    Author: dilyevsky <ilyevsky@gmail.com>
    Date: Wed Jan 9 16:03:17 2019 -0800
    Support gogoproto.stdduration option. (#35)
    Support `gogoproto.stdduration` gogo proto option that generates plain `time.Duration` as proto field via implicit conversion from gogo's `google.protobuf.Duration` proto.
    Example usage:
    ```python
    ```
    Here's what the run looks like:
    ```
    |S|spacey@masonjar:~/go/src/github.com/pcn/reproducer$ ./reproducer time_reproducer.sky
    panic: TypeError: value <google.protobuf.Duration nanos:500000000 > (type `google.protobuf.Duration') can't be assigned to type `"time".Duration'.
    goroutine 1 [running]:
    main.main()
    /home/spacey/go/src/github.com/pcn/reproducer/main.go:62 +0x65b
    ```