Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use clientConfig struct for unmarshalling yarpc clients instead of generic classConfig #618

Merged
merged 4 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ for config_file in ${config_files}; do
dir=$(dirname "$config_file")
yaml_files=$(find "$dir" -name "*.yaml")
for yaml_file in ${yaml_files}; do
thrift_file=$(yq -r '.. | .thriftFile? | select(strings | endswith(".thrift"))' "$yaml_file")
thrift_file=$(yq -r '.. | .idlFile? | select(strings | endswith(".thrift"))' "$yaml_file")
[[ -z ${thrift_file} ]] && continue
[[ ${THRIFTRW_SRCS} == *${thrift_file}* ]] && continue
THRIFTRW_SRCS+=" $CONFIG_DIR/idl/$thrift_file"
Expand Down
29 changes: 18 additions & 11 deletions codegen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ type clientConfig interface {
h *PackageHelper) (*ClientSpec, error)
}

// ClientThriftConfig is the "config" field in the client-config.yaml for http
// client and tchannel client.
type ClientThriftConfig struct {
// ClientIDLConfig is the "config" field in the client-config.yaml for
// HTTP/TChannel/gRPC clients.
type ClientIDLConfig struct {
ExposedMethods map[string]string `yaml:"exposedMethods" json:"exposedMethods" validate:"exposedMethods"`
ThriftFile string `yaml:"thriftFile" json:"thriftFile" validate:"nonzero"`
ThriftFileSha string `yaml:"thriftFileSha,omitempty" json:"thriftFileSha"`
IDLFile string `yaml:"idlFile" json:"idlFile" validate:"nonzero"`
IDLFileSha string `yaml:"idlFileSha,omitempty" json:"idlFileSha"`
SidecarRouter string `yaml:"sidecarRouter" json:"sidecarRouter"`
Fixture *Fixture `yaml:"fixture,omitempty" json:"fixture"`
}
Expand Down Expand Up @@ -85,8 +85,8 @@ func validateExposedMethods(v interface{}, param string) error {
// HTTPClientConfig represents the "config" field for a HTTP client-config.yaml
type HTTPClientConfig struct {
ClassConfigBase `yaml:",inline" json:",inline"`
Dependencies Dependencies `yaml:"dependencies,omitempty" json:"dependencies"`
Config *ClientThriftConfig `yaml:"config" json:"config" validate:"nonzero"`
Dependencies Dependencies `yaml:"dependencies,omitempty" json:"dependencies"`
Config *ClientIDLConfig `yaml:"config" json:"config" validate:"nonzero"`
}

func newHTTPClientConfig(raw []byte) (*HTTPClientConfig, error) {
Expand All @@ -107,12 +107,12 @@ func newHTTPClientConfig(raw []byte) (*HTTPClientConfig, error) {

func newClientSpec(
clientType string,
config *ClientThriftConfig,
config *ClientIDLConfig,
instance *ModuleInstance,
h *PackageHelper,
annotate bool,
) (*ClientSpec, error) {
thriftFile := filepath.Join(h.ThriftIDLPath(), config.ThriftFile)
thriftFile := filepath.Join(h.ThriftIDLPath(), config.IDLFile)
mspec, err := NewModuleSpec(thriftFile, annotate, false, h)

if err != nil {
Expand Down Expand Up @@ -151,8 +151,8 @@ func (c *HTTPClientConfig) NewClientSpec(
// TChannelClientConfig represents the "config" field for a TChannel client-config.yaml
type TChannelClientConfig struct {
ClassConfigBase `yaml:",inline" json:",inline"`
Dependencies Dependencies `yaml:"dependencies,omitempty" json:"dependencies"`
Config *ClientThriftConfig `yaml:"config" json:"config" validate:"nonzero"`
Dependencies Dependencies `yaml:"dependencies,omitempty" json:"dependencies"`
Config *ClientIDLConfig `yaml:"config" json:"config" validate:"nonzero"`
}

func newTChannelClientConfig(raw []byte) (*TChannelClientConfig, error) {
Expand Down Expand Up @@ -224,6 +224,13 @@ func (c *CustomClientConfig) NewClientSpec(
return spec, nil
}

// GRPCClientConfig represents the "config" field for a gRPC client-config.yaml.
type GRPCClientConfig struct {
ClassConfigBase `yaml:",inline" json:",inline"`
Dependencies Dependencies `yaml:"dependencies,omitempty" json:"dependencies"`
Config *ClientIDLConfig `yaml:"config" json:"config" validate:"nonzero"`
}

func clientType(raw []byte) (string, error) {
clientConfig := ClassConfigBase{}
if err := yaml.Unmarshal(raw, &clientConfig); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions codegen/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ config:
assert.Error(t, err)
assert.Equal(
t,
fmt.Sprintf("%s client config validation failed: Config.ThriftFile: zero value", clientType),
fmt.Sprintf("%s client config validation failed: Config.IDLFile: zero value", clientType),
err.Error())
}

Expand Down Expand Up @@ -273,12 +273,12 @@ func TestNewClientConfigGetHTTPClient(t *testing.T) {
Dependencies: Dependencies{
Client: []string{"a", "b"},
},
Config: &ClientThriftConfig{
Config: &ClientIDLConfig{
ExposedMethods: map[string]string{
"a": "method",
},
ThriftFileSha: "thriftFileSha",
ThriftFile: "clients/bar/bar.thrift",
IDLFileSha: "thriftFileSha",
IDLFile: "clients/bar/bar.thrift",
SidecarRouter: "sidecar",
Fixture: &Fixture{
ImportPath: "import",
Expand All @@ -302,12 +302,12 @@ func TestNewClientConfigGetTChannelClient(t *testing.T) {
Dependencies: Dependencies{
Client: []string{"a", "b"},
},
Config: &ClientThriftConfig{
Config: &ClientIDLConfig{
ExposedMethods: map[string]string{
"a": "method",
},
ThriftFileSha: "thriftFileSha",
ThriftFile: "clients/bar/bar.thrift",
IDLFileSha: "thriftFileSha",
IDLFile: "clients/bar/bar.thrift",
SidecarRouter: "sidecar",
Fixture: &Fixture{
ImportPath: "import",
Expand Down
12 changes: 2 additions & 10 deletions codegen/module_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ func (g *YarpcClientGenerator) ComputeSpec(
func (g *YarpcClientGenerator) Generate(
instance *ModuleInstance,
) (*BuildResult, error) {
clientConfig := &ClassConfig{}
clientConfig := &GRPCClientConfig{}
if err := yaml.Unmarshal(instance.YAMLFileRaw, &clientConfig); err != nil {
return nil, errors.Wrapf(
err,
Expand All @@ -821,15 +821,7 @@ func (g *YarpcClientGenerator) Generate(
Instance: instance,
}

v, ok := clientConfig.Config["protoFile"]
if !ok {
return nil, errors.Errorf(
"Missing \"protoFile\" field in %q YAML config",
instance.InstanceName,
)
}
protoFile := v.(string)
parts := strings.Split(protoFile, "/")
parts := strings.Split(clientConfig.Config.IDLFile, "/")
genDir := strings.Join(parts[0:len(parts)-1], "/")

data.GenPkg = path.Join(
Expand Down
5 changes: 3 additions & 2 deletions codegen/runner/pre-steps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ for config_file in ${config_files}; do
processor="jq"
fi

proto_file=$(${processor} -r '.. | .protoFile? | select(strings | endswith(".proto"))' "$yaml_file")
proto_file=$(${processor} -r '.. | .idlFile? | select(strings | endswith(".proto"))' "$yaml_file")
if [[ ! -z ${proto_file} ]] && [[ ${found_protos} != *${proto_file}* ]]; then
found_protos+=" $proto_file"
proto_dir=$(dirname "$proto_file")
Expand Down Expand Up @@ -144,9 +144,10 @@ for config_file in ${config_files}; do
fi

thrift_file=$(${processor} -r '.. | .thriftFile? | select(strings | endswith(".thrift"))' "$yaml_file")
thrift_file+=$(${processor} -r '.. | .idlFile? | select(strings | endswith(".thrift"))' "$yaml_file")

# process .proto files
proto_file=$(${processor} -r '.. | .protoFile? | select(strings | endswith(".proto"))' "$yaml_file")
proto_file=$(${processor} -r '.. | .idlFile? | select(strings | endswith(".proto"))' "$yaml_file")
if [[ ! -z ${proto_file} ]] && [[ ${found_protos} != *${proto_file}* ]]; then
found_protos+=" $proto_file"
proto_file="$IDL_DIR/$proto_file"
Expand Down
2 changes: 1 addition & 1 deletion codegen/type_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ func (c *TypeConverter) genStructConverter(
// toField.Type.TypeCode().String(), toField.Name,
// )

// pkgName, err := h.TypePackageName(toField.Type.ThriftFile())
// pkgName, err := h.TypePackageName(toField.Type.IDLFile())
// if err != nil {
// return nil, err
// }
Expand Down
4 changes: 2 additions & 2 deletions docs/client_config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"config": {
"type": "object",
"properties": {
"thriftFile": {
"idlFile": {
"type": "string",
"description": "Path to client thrift file, relative to idl path",
"examples": [
"clients/contacts/contacts.thrift"
]
},
"thriftFileSha": {
"idlFileSha": {
"type": "string",
"description": "Sha of the thrift file, reserved but currently not used",
"examples": [
Expand Down
4 changes: 2 additions & 2 deletions examples/example-gateway/clients/bar/client-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "bar",
"type": "http",
"config": {
"thriftFile": "clients/bar/bar.thrift",
"thriftFileSha": "{{placeholder}}",
"idlFile": "clients/bar/bar.thrift",
"idlFileSha": "{{placeholder}}",
"exposedMethods": {
"Normal": "Bar::normal",
"NormalRecur": "Bar::normalRecur",
Expand Down
4 changes: 2 additions & 2 deletions examples/example-gateway/clients/baz/client-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ config:
TransHeadersNoReq: SimpleService::transHeadersNoReq
TransHeadersType: SimpleService::transHeadersType
URLTest: SimpleService::urlTest
thriftFile: clients/baz/baz.thrift
thriftFileSha: '{{placeholder}}'
idlFile: clients/baz/baz.thrift
idlFileSha: '{{placeholder}}'
genTestServer: true
name: baz
type: tchannel
4 changes: 2 additions & 2 deletions examples/example-gateway/clients/contacts/client-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ config:
scenarios:
SaveContacts:
- success
thriftFile: clients/contacts/contacts.thrift
thriftFileSha: '{{placeholder}}'
idlFile: clients/contacts/contacts.thrift
idlFileSha: '{{placeholder}}'
name: contacts
type: http
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ config:
exposedMethods:
EchoString: Corge::echoString
sidecarRouter: default
thriftFile: clients/corge/corge.thrift
thriftFileSha: '{{placeholder}}'
idlFile: clients/corge/corge.thrift
idlFileSha: '{{placeholder}}'
name: corge-http
type: http
4 changes: 2 additions & 2 deletions examples/example-gateway/clients/corge/client-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ config:
exposedMethods:
EchoString: Corge::echoString
sidecarRouter: default
thriftFile: clients/corge/corge.thrift
thriftFileSha: '{{placeholder}}'
idlFile: clients/corge/corge.thrift
idlFileSha: '{{placeholder}}'
name: corge
type: tchannel
2 changes: 1 addition & 1 deletion examples/example-gateway/clients/echo/client-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: echo
type: grpc
config:
protoFile: clients/echo/echo.proto
idlFile: clients/echo/echo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ config:
exposedMethods:
AddCredentials: GoogleNowService::addCredentials
CheckCredentials: GoogleNowService::checkCredentials
thriftFile: clients/googlenow/googlenow.thrift
thriftFileSha: '{{placeholder}}'
idlFile: clients/googlenow/googlenow.thrift
idlFileSha: '{{placeholder}}'
name: google-now
type: http
4 changes: 2 additions & 2 deletions examples/example-gateway/clients/multi/client-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ config:
exposedMethods:
HelloA: ServiceABack::hello
HelloB: ServiceBBack::hello
thriftFile: clients/multi/multi.thrift
thriftFileSha: '{{placeholder}}'
idlFile: clients/multi/multi.thrift
idlFileSha: '{{placeholder}}'
name: multi
type: http
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "withexceptions",
"type": "http",
"config": {
"thriftFile": "clients/withexceptions/withexceptions.thrift",
"thriftFileSha": "{{placeholder}}",
"idlFile": "clients/withexceptions/withexceptions.thrift",
"idlFileSha": "{{placeholder}}",
"exposedMethods": {
"Func1": "WithExceptions::Func1"
}
Expand Down