twitchtv / twirp

A simple RPC framework with protobuf service definitions
https://twitchtv.github.io/twirp/docs/intro.html
Apache License 2.0
7.19k stars 326 forks source link

ServerOptions and ClientOptions generic getters for generated code #316

Closed marioizquierdo closed 3 years ago

marioizquierdo commented 3 years ago

The twirp runtime should expose generic getters to different types, returning default values when not present, so we don't have to force library updates every time clients or server options add a new option.

Current usage from generated code:

pathPrefix := opts.PathPrefix()

The problem with this approach is that older versions of the twirp.ServerOptionts type does not include the PathPrefix() method, which forces consumers of the generated client to update twirp to latest version, even if they don't care about the PathPrefix option.

The alternative would look like this:

var pathPrefix string
ok := opts.GetOpt("pathPrefix", &pathPrefix)

The new GetOpt method returns true if the key was set on the options, and false if it was not set, which works even if the option doesn't exist in the given version of the runtime library.

Go Playground example: https://play.golang.org/p/61N1d-nyiIY