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

Edgedb json value be mapped to json.RawMessage go types instead of []byte. #315

Open
chirino opened this issue Jun 9, 2024 · 3 comments

Comments

@chirino
Copy link

chirino commented Jun 9, 2024

No description provided.

@fmoor
Copy link
Member

fmoor commented Jun 10, 2024

Thanks for opening the issue! I'm not sure I understand what this is asking for though. json.RawMessage is a []byte type and can be used in place of []byte with the EdgeDB client.

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

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

func main() {
	ctx := context.Background()
	client, err := edgedb.CreateClient(ctx, edgedb.Options{})
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	var result struct {
		Thing json.RawMessage `edgedb:"thing"`
	}
	err = client.QuerySingle(ctx, `SELECT { thing := to_json('{"hello": "world"}') }`, &result)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(result.Thing))
}

Maybe there are some scenarios where this doesn't work? Let us know. Thanks!

@chirino
Copy link
Author

chirino commented Jun 11, 2024

if you put that query into an example.edgeql the generator produces these types:

type ExampleResult struct {
	Thing []byte `edgedb:"thing"`
}

It would be better if it generated:

type ExampleResult struct {
	Thing json.RawMessage `edgedb:"thing"`
}

json marshaling a json.RawMessage does the thing you would expect. json Marshaling []byte, marshals as list of numbers.

and if thing was optional, it uses edgedb.OptionalBytes. That json marshals to some weird encoded string.

@fmoor
Copy link
Member

fmoor commented Jun 11, 2024

Ok I see. That makes sense.

It turns out that we can decode to json.RawMessage but we currently don't support encoding to it. Before potentially changing the generator I think we should support both operations. I'll try to work out a good way of doing this.

If it happens that the only thing you want to do with your query results is json encode them, then you can use the JSON variant of the generated function. This also has a performance improvement because the query result comes from the server as json bytes and doesn't need to be marshaled again. I understand though that there are many times that json encoding is often only one of the things that a query result is used for.

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