Giter Site home page Giter Site logo

Comments (6)

mwitkow avatar mwitkow commented on May 2, 2024

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.

mwitkow avatar mwitkow commented on May 2, 2024

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.

mwitkow avatar mwitkow commented on May 2, 2024

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.

mwitkow avatar mwitkow commented on May 2, 2024

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.

mwitkow avatar mwitkow commented on May 2, 2024

@yugui, do you need anything from me in relation to #34 to get this fixed? :)

from grpc-gateway.

yugui avatar yugui commented on May 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.