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

edgeql-go generates private only code #245

Closed
joeyak opened this issue Dec 8, 2022 · 2 comments
Closed

edgeql-go generates private only code #245

joeyak opened this issue Dec 8, 2022 · 2 comments

Comments

@joeyak
Copy link

joeyak commented Dec 8, 2022

When edgeql-go is run, the generated function names and return values are lowercase, which makes them inaccessible.

I have select_enabled_auth.edgeql

SELECT Auth {
    id,
    access_token,
    refresh_token,
    token_expires
}
FILTER .user.channel_settings.enabled = true;

and a queries.go file to generate the code and export the queries

//go:generate edgeql-go

package database

var (
	SelectEnabledAuth = selectEnabledAuth
)

the generated code is

// Code generated by github.com/edgedb/edgedb-go/cmd/edgeql-go DO NOT EDIT.

package database

import (
	"context"
	_ "embed"
	"time"

	"github.com/edgedb/edgedb-go"
)

//go:embed select_enabled_auth.edgeql
var selectEnabledAuthCmd string

// selectEnabledAuth
// runs the query found in
// select_enabled_auth.edgeql
func selectEnabledAuth(
	ctx context.Context,
	client *edgedb.Client,
) ([]struct {
	id            edgedb.UUID `edgedb:"id"`
	access_token  string      `edgedb:"access_token"`
	refresh_token string      `edgedb:"refresh_token"`
	token_expires time.Time   `edgedb:"token_expires"`
}, error) {
	var result []struct {
		id            edgedb.UUID `edgedb:"id"`
		access_token  string      `edgedb:"access_token"`
		refresh_token string      `edgedb:"refresh_token"`
		token_expires time.Time   `edgedb:"token_expires"`
	}

	err := client.Query(
		ctx,
		selectEnabledAuthCmd,
		&result,
	)

	return result, err
}

// selectEnabledAuthJSON
// runs the query found in
// select_enabled_auth.edgeql
// returning the results as json encoded bytes
func selectEnabledAuthJSON(
	ctx context.Context,
	client *edgedb.Client,
) ([]byte, error) {
	var result []byte

	err := client.QueryJSON(
		ctx,
		selectEnabledAuthCmd,
		&result,
	)
	if err != nil {
		return nil, err
	}

	return result, nil
}

When I go to use the returned values, I cannot call on the fields. The generated files would work fine if it's in the same package, but I have the database layer exported out.
image

Expected behavior
I would expect the return values to have public field names so they can be accessed turning selectEnabledAuth into below. It should be pascal case, or at least start with an upper letter.

func selectEnabledAuth(
	ctx context.Context,
	client *edgedb.Client,
) ([]struct {
	Id            edgedb.UUID `edgedb:"id"`
	AccessToken  string      `edgedb:"access_token"`
	RefreshToken string      `edgedb:"refresh_token"`
	TokenExpires time.Time   `edgedb:"token_expires"`
}, error)
...

Versions:

  • OS : Windows 11
  • EdgeDB version: 2.7+81de4dd
  • EdgeDB CLI version: 2.2.6+af56767
  • edgedb-go version: v0.13.1
  • Go version: go version go1.19.3 windows/amd64
@fmoor
Copy link
Member

fmoor commented Dec 8, 2022

Thanks for the feedback @joeyak!

The generator intentionally doesn't change the case of properties/links in query shapes. EdgeDB is unopinionated about casing style since it can be used with clients of any language. If we alter the case in a query then it is possible for the altered names to collide with other names. For example you could have two properties access_token and AccessToken in a query shape. If we automatically converted snake to pascal case, this would result in a broken query function for the worst case and a confusing result type in the best case.

Because there are no constraints on property or link names you can define your schema or your query with upper case names. You could write your example query like this:

SELECT Auth {
    ID := .id,
    AccessToken := .access_token,
    RefreshToken := .refresh_token,
    TokenExpires := .token_expires
}
FILTER .user.channel_settings.enabled = true;

We are open to adding an opt-in flag that would enable a snake to pascal case transformation on the result type. I'll use this issue for adding this feature. We just don't want to make it the default behavior.

@joeyak
Copy link
Author

joeyak commented Dec 9, 2022

That sounds like a great idea. I've been trying to play around to figure out how to expose those (didn't think to capitalize the results like that), but I've already gotten the hang of exposing the private fields. Thanks for the help, I'll definitely update my queries!

fmoor added a commit that referenced this issue Feb 14, 2023
The new `-mixedcaps` option changes snake_case names in shapes to
MixedCaps names in go structs.

```
select User {
    first_name,
    last_name,
}
```

Becomes:
```go
type MyQueryResult struct {
    FirstName string `edgedb:"first_name"`
    LastName  string `edgedb:"last_name"`
}
```

Without this option enabled names are used exactly as they appear in
shapes. For example the query above would produce the following struct:
```go
type MyQueryResult struct {
    first_name string `edgedb:"first_name"`
    last_name  string `edgedb:"last_name"`
}
```

working on #245
fmoor added a commit that referenced this issue May 1, 2023
The new `-mixedcaps` option changes snake_case names in shapes to
MixedCaps names in go structs.

```
select User {
    first_name,
    last_name,
}
```

Becomes:
```go
type MyQueryResult struct {
    FirstName string `edgedb:"first_name"`
    LastName  string `edgedb:"last_name"`
}
```

Without this option enabled names are used exactly as they appear in
shapes. For example the query above would produce the following struct:
```go
type MyQueryResult struct {
    first_name string `edgedb:"first_name"`
    last_name  string `edgedb:"last_name"`
}
```

working on #245
@fmoor fmoor closed this as completed May 1, 2023
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

No branches or pull requests

2 participants