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

Check for Nils when Resolving Embedded Pointers #363

Open
mathewbyrne opened this Issue Oct 2, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@mathewbyrne
Collaborator

mathewbyrne commented Oct 2, 2018

Expected Behaviour

If I map to a model with an embedded pointer, and the resolved instance of that model has a nil as the pointer value, then the resolver for that value should return nil and be subject to normal null bubbling behaviour.

type MyModel struct {
    *SharedFields
}

type SharedFields {
    Title string
}
type MyModel {
    title: String
}

Actual Behavior

Nil pointer dereference panic.

Currently we rely on embed magic to resolve the field for us. So in the example above the resolver would just access:

return MyModel.Title

when it should probably do something like the following:

if MyModel.SharedFields != nil {
    return MyModel.SharedFields.Title
}

This is a bit more complicated than it looks, because currently codegen does not have the required metadata to unpack the field. Additionally, embeds can be recursive, meaning that you may need to generate code like:

// Model
if MyModel.SharedFields != nil {
    return MyModel.SharedFields.Title
}

type MyModel struct {
    *SharedFields
}

type SharedFields {
    SharedFieldsEmbedded
}

type SharedFieldsEmbedded {
    *SharedFieldsEmbeddedPointer
}

type SharedFieldsEmbeddedPointer {
    Title string
}

// generated.go
if MyModel.SharedFields != nil &&  MyModel.SharedFields.SharedFieldsEmbedded.SharedFieldsEmbeddedPointer != nil {
    return MyModel.SharedFields.SharedFieldsEmbedded.SharedFieldsEmbeddedPointer.Title
}

@mathewbyrne mathewbyrne added the bug label Oct 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment