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

Avoid problems with val being undefined in the federation template. #1760

Merged
merged 4 commits into from
Jan 12, 2022

Conversation

csilvers
Copy link
Contributor

@csilvers csilvers commented Jan 12, 2022

When running gqlgen over our schema, we were seeing errors like:

assignments/generated/graphql/service.go:300:4: val declared but not used

The generated code looks like this:

func entityResolverNameForMobileNavigation(ctx context.Context, rep map[string]interface{}) (string, error) {
        for {
                var (
                        m   map[string]interface{}
                        val interface{}
                        ok  bool
                )
                m = rep
                if _, ok = m["kaid"]; !ok {
                        break
                }
                m = rep
                if _, ok = m["language"]; !ok {
                        break
                }
                return "findMobileNavigationByKaidAndLanguage", nil
        }
        return "", fmt.Errorf("%w for MobileNavigation", ErrTypeNotFound)
}

Looking at the code, it's pretty clear that this happens when there
are multiple key-fields, but each of them has only one keyField.Field
entry. This is because the old code looked at len(keyFields) to
decide whether to declare the val variable, but looks at
len(keyField.Field) for each keyField to decide whether to use the
val variable.

The easiest solution, and the one I do in this PR, is to just declare
val all the time, and use a null-assignment to quiet the compiler
when it's not used.

We have added tests to ensure that this fixes the issue, which you can run:

  1. Go to plugins/federation
  2. Run the command go run github.com/99designs/gqlgen --config testdata/entityresolver/gqlgen.yml
  3. Verify no errors are generated.

Describe your PR and link to any relevant issues.

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)

When running gqlgen over our schema, we were seeing errors like:
```
assignments/generated/graphql/service.go:300:4: val declared but not used
```

The generated code looks like this:
```
func entityResolverNameForMobileNavigation(ctx context.Context, rep map[string]interface{}) (string, error) {
        for {
                var (
                        m   map[string]interface{}
                        val interface{}
                        ok  bool
                )
                m = rep
                if _, ok = m["kaid"]; !ok {
                        break
                }
                m = rep
                if _, ok = m["language"]; !ok {
                        break
                }
                return "findMobileNavigationByKaidAndLanguage", nil
        }
        return "", fmt.Errorf("%w for MobileNavigation", ErrTypeNotFound)
}
```

Looking at the code, it's pretty clear that this happens when there
are multiple key-fields, but each of them has only one keyField.Field
entry.  This is because the old code looked at `len(keyFields)` to
decide whether to declare the `val` variable, but looks at
`len(keyField.Field)` for each keyField to decide whether to use the
`val` variable.

The easiest solution, and the one I do in this PR, is to just declare
`val` all the time, and use a null-assignment to quiet the compiler
when it's not used.
@csilvers
Copy link
Contributor Author

csilvers commented Jan 12, 2022

I don't seem to able to suggest reviewers, but @carldunham you're probably the best person to review this! Also cc-ing @StevenACoffman and @MiguelCastillo for landing this when the time comes.

@coveralls
Copy link

coveralls commented Jan 12, 2022

Coverage Status

Coverage decreased (-0.02%) to 70.722% when pulling 208c4b1 on Khan:val-undef into 47015f1 on 99designs:master.

@carldunham
Copy link
Contributor

Good catch @csilvers! Could you add some unit tests that would fail without this fix?

Copy link
Collaborator

@StevenACoffman StevenACoffman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Seems a little difficult to make a meaningful unit test for.

@MiguelCastillo
Copy link
Collaborator

MiguelCastillo commented Jan 12, 2022

LGTM. Seems a little difficult to make a meaningful unit test for.

It shouldn't be too bad. We can create a unit test based on how @csilvers found it in the first place. Let me see if I can add one. But happy to do after in a separate PR if it turns out to be too painful.

From `plugins/federation`, run the following command and verify that no errors are produced

```
go run github.com/99designs/gqlgen --config testdata/entityresolver/gqlgen.yml
```
@MiguelCastillo
Copy link
Collaborator

MiguelCastillo commented Jan 12, 2022

Ok I added a test. I verified the issue happens without these changes and that the issue is fixed with these changes. Also - this is a very clever solution, Craig! I would have definitely tried adjusting the logic. This seems good enough but may circle back if a more clear fix comes to mind.

val interface{}
ok bool
)
_ = val
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the fix does its thing.

@MiguelCastillo MiguelCastillo merged commit b2a832d into 99designs:master Jan 12, 2022
@csilvers
Copy link
Contributor Author

Thanks for the quick review everyone! And extra thank you for adding the test @MiguelCastillo !

@csilvers csilvers deleted the val-undef branch January 12, 2022 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants