Skip to content

Commit 49aa101

Browse files
authored
Add edgeql to go code generator (#236)
related to #182
1 parent a7d49fb commit 49aa101

File tree

170 files changed

+5338
-1640
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+5338
-1640
lines changed

.golangci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ run:
1818

1919
# list of build tags, all linters use it. Default is empty list.
2020
build-tags:
21+
- tools
2122

2223
# which dirs to skip: issues from them won't be reported;
2324
# can use regexp here: generated.*, regexp is applied on full path;
@@ -39,6 +40,7 @@ run:
3940
# on Windows.
4041
skip-files:
4142
- doc_test.go
43+
- cmd/edgeql-go/doc.go
4244

4345
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
4446
# If invoked with -mod=readonly, the go command is disallowed from the implicit

.licence-header.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
This source file is part of the EdgeDB open source project.
22

3-
Copyright 2020-present EdgeDB Inc. and the EdgeDB authors.
3+
Copyright EdgeDB Inc. and the EdgeDB authors.
44

55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ errors:
1919
exit 1 \
2020
)
2121
edb gen-errors-json --client | \
22-
go run internal/cmd/generr/*.go > \
23-
generatederrors.go
22+
go run internal/cmd/generr/definition.go > internal/client/errors_gen.go
23+
edb gen-errors-json --client | \
24+
go run internal/cmd/generr/export.go > errors_gen.go
2425
make format

cmd/edgeql-go/doc.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This source file is part of the EdgeDB open source project.
2+
//
3+
// Copyright EdgeDB Inc. and the EdgeDB authors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
// edgeql-go is a tool to generate go functions from edgeql queries. When run
18+
// in an EdgeDB project directory (or subdirectory) a *_edgeql.go source file
19+
// will be generated for each *.edgeql file. The generated go will have an
20+
// edgeqlFileName and edgeqlFileNameJSON function with typed arguments and
21+
// return value matching the query's arguments and result shape.
22+
//
23+
// # Install
24+
//
25+
// go install github.com/edgedb/edgedb-go/cmd/edgeql-go@latest
26+
//
27+
// See also [pinning tool dependencies].
28+
//
29+
// # Usage
30+
//
31+
// Typically this process would be run using [go generate] like this:
32+
//
33+
// //go:generate edgeql-go
34+
//
35+
// [pinning tool dependencies]: https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
36+
// [go generate]: https://go.dev/blog/generate
37+
package main

cmd/edgeql-go/endtoend_test.go

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// This source file is part of the EdgeDB open source project.
2+
//
3+
// Copyright EdgeDB Inc. and the EdgeDB authors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"io"
22+
"io/fs"
23+
"log"
24+
"os"
25+
"os/exec"
26+
"path/filepath"
27+
"strings"
28+
"sync"
29+
"testing"
30+
31+
edgedb "github.com/edgedb/edgedb-go/internal/client"
32+
"github.com/stretchr/testify/assert"
33+
"github.com/stretchr/testify/require"
34+
)
35+
36+
var dsn string
37+
38+
func TestMain(m *testing.M) {
39+
o := edgedb.TestClientOptions()
40+
pwd, ok := o.Password.Get()
41+
if !ok {
42+
log.Fatal("missing password")
43+
}
44+
dsn = fmt.Sprintf(
45+
"edgedb://%s:%s@%s:%d?tls_security=%s&tls_ca_file=%s",
46+
o.User,
47+
pwd,
48+
o.Host,
49+
o.Port,
50+
o.TLSOptions.SecurityMode,
51+
o.TLSOptions.CAFile,
52+
)
53+
os.Exit(m.Run())
54+
}
55+
56+
func TestEdgeQLGo(t *testing.T) {
57+
dir, err := os.MkdirTemp("", "edgeql-go-*")
58+
require.NoError(t, err)
59+
defer func() {
60+
assert.NoError(t, os.RemoveAll(dir))
61+
}()
62+
63+
t.Log("building edgeql-go")
64+
edgeqlGo := filepath.Join(dir, "edgeql-go")
65+
run(t, ".", "go", "build", "-o", edgeqlGo)
66+
67+
var wg sync.WaitGroup
68+
err = filepath.WalkDir(
69+
"testdata",
70+
func(src string, d fs.DirEntry, e error) error {
71+
require.NoError(t, e)
72+
if src == "testdata" {
73+
return nil
74+
}
75+
76+
dst := filepath.Join(dir, strings.TrimPrefix(src, "testdata"))
77+
if d.IsDir() {
78+
e = os.Mkdir(dst, os.ModePerm)
79+
require.NoError(t, e)
80+
} else {
81+
wg.Add(1)
82+
go func() {
83+
defer wg.Done()
84+
copyFile(t, dst, src)
85+
}()
86+
}
87+
return nil
88+
},
89+
)
90+
require.NoError(t, err)
91+
wg.Wait()
92+
93+
entries, err := os.ReadDir(dir)
94+
require.NoError(t, err)
95+
for _, entry := range entries {
96+
if entry.Name() == "edgeql-go" {
97+
continue
98+
}
99+
100+
t.Run(entry.Name(), func(t *testing.T) {
101+
projectDir := filepath.Join(dir, entry.Name())
102+
run(t, projectDir, edgeqlGo)
103+
run(t, projectDir, "go", "run", "./...")
104+
er := filepath.WalkDir(
105+
projectDir,
106+
func(f string, d fs.DirEntry, e error) error {
107+
require.NoError(t, e)
108+
if strings.HasSuffix(f, ".go.assert") {
109+
checkAssertFile(t, f)
110+
}
111+
if strings.HasSuffix(f, ".go") &&
112+
!strings.HasSuffix(f, "ignore.go") {
113+
checkGoFile(t, f)
114+
}
115+
return nil
116+
},
117+
)
118+
require.NoError(t, er)
119+
})
120+
}
121+
}
122+
123+
func checkAssertFile(t *testing.T, file string) {
124+
t.Helper()
125+
goFile := strings.TrimSuffix(file, ".assert")
126+
if assert.FileExistsf(t, goFile, "missing .go file for %s", file) {
127+
assertEqualFiles(t, file, goFile)
128+
}
129+
}
130+
131+
func checkGoFile(t *testing.T, file string) {
132+
t.Helper()
133+
assertFile := file + ".assert"
134+
if assert.FileExistsf(t, assertFile,
135+
"missing .go.assert file for %s", file,
136+
) {
137+
assertEqualFiles(t, assertFile, file)
138+
}
139+
}
140+
141+
func assertEqualFiles(t *testing.T, left, right string) {
142+
t.Helper()
143+
leftData, err := os.ReadFile(left)
144+
require.NoErrorf(t, err, "reading %s", left)
145+
146+
rightData, err := os.ReadFile(right)
147+
require.NoErrorf(t, err, "reading %s", right)
148+
149+
assert.Equal(t, string(leftData), string(rightData),
150+
"files are not equal: %s != %s", left, right,
151+
)
152+
}
153+
154+
func copyFile(t *testing.T, to, from string) {
155+
toFd, err := os.Create(to)
156+
require.NoError(t, err)
157+
defer func() {
158+
assert.NoError(t, toFd.Close())
159+
}()
160+
161+
fromFd, err := os.Open(from)
162+
require.NoError(t, err)
163+
defer func() {
164+
assert.NoError(t, fromFd.Close())
165+
}()
166+
167+
_, err = io.Copy(toFd, fromFd)
168+
require.NoError(t, err)
169+
}
170+
171+
func run(t *testing.T, dir, name string, args ...string) {
172+
cmd := exec.Command(name, args...)
173+
cmd.Dir = dir
174+
cmd.Stdout = os.Stdout
175+
cmd.Stderr = os.Stderr
176+
cmd.Env = append(os.Environ(), fmt.Sprintf("EDGEDB_DSN=%s", dsn))
177+
require.NoError(t, cmd.Run())
178+
}

0 commit comments

Comments
 (0)