Skip to content

Commit a2ae972

Browse files
committed
add BigQuery emulator
1 parent 0fbed8c commit a2ae972

File tree

26 files changed

+12230
-0
lines changed

26 files changed

+12230
-0
lines changed

go.mod

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module github.com/goccy/bigquery-emulator
2+
3+
go 1.17
4+
5+
require (
6+
cloud.google.com/go/bigquery v1.32.0
7+
github.com/go-playground/validator/v10 v10.11.0
8+
github.com/goccy/go-yaml v1.9.5
9+
github.com/goccy/go-zetasql v0.2.5
10+
github.com/goccy/go-zetasqlite v0.1.0
11+
github.com/gorilla/mux v1.8.0
12+
google.golang.org/api v0.81.0
13+
)
14+
15+
require (
16+
cloud.google.com/go v0.100.2 // indirect
17+
cloud.google.com/go/compute v1.6.1 // indirect
18+
cloud.google.com/go/iam v0.3.0 // indirect
19+
github.com/fatih/color v1.10.0 // indirect
20+
github.com/go-playground/locales v0.14.0 // indirect
21+
github.com/go-playground/universal-translator v0.18.0 // indirect
22+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
23+
github.com/golang/protobuf v1.5.2 // indirect
24+
github.com/google/go-cmp v0.5.8 // indirect
25+
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
26+
github.com/leodido/go-urn v1.2.1 // indirect
27+
github.com/mattn/go-colorable v0.1.12 // indirect
28+
github.com/mattn/go-isatty v0.0.14 // indirect
29+
github.com/mattn/go-sqlite3 v1.14.13 // indirect
30+
go.opencensus.io v0.23.0 // indirect
31+
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
32+
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
33+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
34+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
35+
golang.org/x/text v0.3.7 // indirect
36+
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
37+
google.golang.org/appengine v1.6.7 // indirect
38+
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
39+
google.golang.org/grpc v1.46.2 // indirect
40+
google.golang.org/protobuf v1.28.0 // indirect
41+
)

go.sum

Lines changed: 696 additions & 0 deletions
Large diffs are not rendered by default.

internal/cmd/generator/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/goccy/bigquery-emulator/internal/cmd/generator
2+
3+
go 1.17

internal/cmd/generator/go.sum

Whitespace-only changes.

internal/cmd/generator/main.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
"encoding/json"
7+
"fmt"
8+
"go/format"
9+
"io/ioutil"
10+
"log"
11+
"os"
12+
"path/filepath"
13+
"runtime"
14+
"sort"
15+
"strings"
16+
"text/template"
17+
)
18+
19+
//go:embed resources/bigquery-api.json
20+
var bigqueryAPIJSON []byte
21+
22+
func main() {
23+
if err := run(os.Args); err != nil {
24+
log.Fatalf("%+v", err)
25+
}
26+
}
27+
28+
type BigQueryAPI struct {
29+
Resources map[string]*Resource `json:"resources"`
30+
}
31+
32+
type Resource struct {
33+
Methods map[string]*Method `json:"methods"`
34+
}
35+
36+
type Method struct {
37+
HTTPMethod string `json:"httpMethod"`
38+
Parameters map[string]*Parameter `json:"parameters"`
39+
Path string `json:"path"`
40+
FlatPath string `json:"flatPath"`
41+
Scope []string `json:"scope"`
42+
}
43+
44+
type Parameter struct {
45+
Type string `json:"type"`
46+
Location string `json:"location"`
47+
Required bool `json:"required"`
48+
}
49+
50+
type handlerParam struct {
51+
Path string
52+
HTTPMethod string
53+
HandlerName string
54+
}
55+
56+
func run(args []string) error {
57+
var v BigQueryAPI
58+
if err := json.Unmarshal(bigqueryAPIJSON, &v); err != nil {
59+
return err
60+
}
61+
tmpl, err := template.New("").Parse(`// Code generated by internal/cmd/generator. DO NOT EDIT!
62+
package server
63+
64+
import "net/http"
65+
66+
type Handler struct {
67+
Path string
68+
HTTPMethod string
69+
Handler http.Handler
70+
}
71+
72+
var handlers = []*Handler{
73+
{{- range . }}
74+
{
75+
Path: "{{ .Path }}",
76+
HTTPMethod: "{{ .HTTPMethod }}",
77+
Handler: &{{ .HandlerName }}Handler{},
78+
},
79+
{{- end }}
80+
}
81+
82+
var (
83+
{{- range . }}
84+
_ http.Handler = &{{ .HandlerName }}Handler{}
85+
{{- end }}
86+
)
87+
88+
{{- range . }}
89+
type {{ .HandlerName }}Handler struct {
90+
}
91+
{{ end }}
92+
`)
93+
handlerParams := []*handlerParam{}
94+
for resourceName, resource := range v.Resources {
95+
for methodName, method := range resource.Methods {
96+
path := method.FlatPath
97+
if path == "" {
98+
path = method.Path
99+
}
100+
if !strings.HasPrefix(path, "/") {
101+
path = "/" + path
102+
}
103+
camelName := strings.ToUpper(string(methodName[0])) + methodName[1:]
104+
handlerParams = append(handlerParams, &handlerParam{
105+
Path: path,
106+
HTTPMethod: method.HTTPMethod,
107+
HandlerName: fmt.Sprintf("%s%s", resourceName, camelName),
108+
})
109+
}
110+
}
111+
sort.Slice(handlerParams, func(i, j int) bool {
112+
return handlerParams[i].HandlerName < handlerParams[j].HandlerName
113+
})
114+
var b bytes.Buffer
115+
if err := tmpl.Execute(&b, handlerParams); err != nil {
116+
return err
117+
}
118+
path := filepath.Join(repoRoot(), "server", "handler_gen.go")
119+
buf, err := format.Source(b.Bytes())
120+
if err != nil {
121+
return err
122+
}
123+
return ioutil.WriteFile(path, buf, 0644)
124+
}
125+
126+
func repoRoot() string {
127+
_, file, _, _ := runtime.Caller(0)
128+
relativePathFromRepoRoot := filepath.Join("internal", "cmd", "generator")
129+
return strings.TrimSuffix(filepath.Dir(file), relativePathFromRepoRoot)
130+
}

0 commit comments

Comments
 (0)