Comments (6)
Yup, indeed, intalizing the field seems to have worked:
if protoReq.Deployment == nil {
protoReq.Deployment = &DeploymentId{}
}
protoReq.Deployment.Name, err = runtime.String(val)
if err != nil {
return nil, err
}
The tricky bit is to initialize the field without knowing what the type is. Some reflection perhaps? Or template-generating a NewDeploymentField
that will act like Deployment.Reset()
from grpc-gateway.
The following function is my (probably misguided) stab at reflecting out of the problem. It instantiates all missing structs along the way.
func instantiateMissingStructs(structPtr interface{}, fieldPath string) (error) {
fieldNames := strings.Split(fieldPath, ".")
ptrToStruct := reflect.ValueOf(structPtr)
if ptrToStruct.Kind() != reflect.Ptr {
return fmt.Errorf("structPtr must be a pointer struct, is %v", ptrToStruct.Kind())
}
currentStruct := reflect.Indirect(ptrToStruct)
if currentStruct.Kind() != reflect.Struct {
return fmt.Errorf("structPtr must be a pointer to a struct, is %v.", ptrToStruct.Kind())
}
for _, name := range fieldNames {
fieldV := currentStruct.FieldByName(name)
if fieldV.Kind() != reflect.Ptr {
return fmt.Errorf("field %v must be a pointer", name)
}
concreteType := fieldV.Type().Elem()
if concreteType.Kind() != reflect.Struct {
return fmt.Errorf("field %v must be a pointer to a struct", name)
}
if fieldV.IsNil() {
fieldV.Set(reflect.New(concreteType))
}
currentStruct = reflect.Indirect(fieldV)
}
return nil
}
For example in the above example, this would instantiate the missing DeploymentId
struct under the Deployment
field.
instantiateMissingStructs(&protoReq, "Deployment")
Or if the URL reference was to deployment.app.id
, and the assignment was protoReq.Deployment.App.Id
, the following one would instantiate both DeploymentId
and AppId
:
instantiateMissingStructs(&protoReq, "Deployment.App")
Any idea where to hook this up? I guess gengo/grpc-gateway/protoc-gen-grpc-gateway/gengateway/generator.go
, but I don't know how to do the tests for it :(
from grpc-gateway.
So yea, if golang/protobuf#54 makes it instantiate a new type, this is trivial.
Otherwise, I think I'll make a PR that reuse the query filter magic.
from grpc-gateway.
So I didn't realise that the grpc-gateway already had a reflection instantiator for the query filtering. The above pull request contains the fix that reuses this.
from grpc-gateway.
@yugui, do you need anything from me in relation to #34 to get this fixed? :)
from grpc-gateway.
Sorry for my late reply. I have just merged #34.
On Tue, Aug 11, 2015 at 4:36 PM Michal Witkowski [email protected]
wrote:
@yugui https://github.com/yugui, do you need anything from me in
relation to #34 #34 to get
this fixed? :)—
Reply to this email directly or view it on GitHub
#32 (comment).
from grpc-gateway.
Related Issues (20)
- generate swagger schema title and description failed HOT 2
- Issue with importing "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway" HOT 3
- genopenapi: Stable openapi names for nested types HOT 1
- snake_case response HOT 1
- api_visibility doesn't transitively apply to rpc request/response messages HOT 2
- protoc-gen-grpc-gateway and protoc-gen-openapiv2 return incorrect version HOT 1
- Dependency on google.golang.org/grpc/health/grpc_health_v1 conflicts with custom proto marshallers HOT 5
- JSONPb marshaler doesn't use indent on non proto fields
- WriteHeader is not called on 200s HOT 1
- How to set cookie to http response header? Why it doesn't work. HOT 2
- Adding custom mux endpoint to openapi output HOT 3
- The API Gateway Vision HOT 2
- Support disabling default response rendering in bazel rule HOT 1
- Example and pattern annotations are not respect for query and path parameters HOT 8
- FieldMask as query param is not converted to snake case HOT 1
- an google.protobuf.Any type how to get openapiv2 output? HOT 1
- omit-enum-default-value Generates Invalid Spec for Enums with a Single Value HOT 1
- Content-Length not set in ForwardResponseMessage HOT 5
- Log level for errors is "info" HOT 1
- On `context deadline exceeded`, response headers aren't set HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grpc-gateway.