Skip to content

Commit

Permalink
add BigQuery emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Jun 23, 2022
1 parent 0fbed8c commit a2ae972
Show file tree
Hide file tree
Showing 26 changed files with 12,230 additions and 0 deletions.
41 changes: 41 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module github.com/goccy/bigquery-emulator

go 1.17

require (
cloud.google.com/go/bigquery v1.32.0
github.com/go-playground/validator/v10 v10.11.0
github.com/goccy/go-yaml v1.9.5
github.com/goccy/go-zetasql v0.2.5
github.com/goccy/go-zetasqlite v0.1.0
github.com/gorilla/mux v1.8.0
google.golang.org/api v0.81.0
)

require (
cloud.google.com/go v0.100.2 // indirect
cloud.google.com/go/compute v1.6.1 // indirect
cloud.google.com/go/iam v0.3.0 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.13 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/grpc v1.46.2 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)
696 changes: 696 additions & 0 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions internal/cmd/generator/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/goccy/bigquery-emulator/internal/cmd/generator

go 1.17
Empty file added internal/cmd/generator/go.sum
Empty file.
130 changes: 130 additions & 0 deletions internal/cmd/generator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"text/template"
)

//go:embed resources/bigquery-api.json
var bigqueryAPIJSON []byte

func main() {
if err := run(os.Args); err != nil {
log.Fatalf("%+v", err)
}
}

type BigQueryAPI struct {
Resources map[string]*Resource `json:"resources"`
}

type Resource struct {
Methods map[string]*Method `json:"methods"`
}

type Method struct {
HTTPMethod string `json:"httpMethod"`
Parameters map[string]*Parameter `json:"parameters"`
Path string `json:"path"`
FlatPath string `json:"flatPath"`
Scope []string `json:"scope"`
}

type Parameter struct {
Type string `json:"type"`
Location string `json:"location"`
Required bool `json:"required"`
}

type handlerParam struct {
Path string
HTTPMethod string
HandlerName string
}

func run(args []string) error {
var v BigQueryAPI
if err := json.Unmarshal(bigqueryAPIJSON, &v); err != nil {
return err
}
tmpl, err := template.New("").Parse(`// Code generated by internal/cmd/generator. DO NOT EDIT!
package server
import "net/http"
type Handler struct {
Path string
HTTPMethod string
Handler http.Handler
}
var handlers = []*Handler{
{{- range . }}
{
Path: "{{ .Path }}",
HTTPMethod: "{{ .HTTPMethod }}",
Handler: &{{ .HandlerName }}Handler{},
},
{{- end }}
}
var (
{{- range . }}
_ http.Handler = &{{ .HandlerName }}Handler{}
{{- end }}
)
{{- range . }}
type {{ .HandlerName }}Handler struct {
}
{{ end }}
`)
handlerParams := []*handlerParam{}
for resourceName, resource := range v.Resources {
for methodName, method := range resource.Methods {
path := method.FlatPath
if path == "" {
path = method.Path
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
camelName := strings.ToUpper(string(methodName[0])) + methodName[1:]
handlerParams = append(handlerParams, &handlerParam{
Path: path,
HTTPMethod: method.HTTPMethod,
HandlerName: fmt.Sprintf("%s%s", resourceName, camelName),
})
}
}
sort.Slice(handlerParams, func(i, j int) bool {
return handlerParams[i].HandlerName < handlerParams[j].HandlerName
})
var b bytes.Buffer
if err := tmpl.Execute(&b, handlerParams); err != nil {
return err
}
path := filepath.Join(repoRoot(), "server", "handler_gen.go")
buf, err := format.Source(b.Bytes())
if err != nil {
return err
}
return ioutil.WriteFile(path, buf, 0644)
}

func repoRoot() string {
_, file, _, _ := runtime.Caller(0)
relativePathFromRepoRoot := filepath.Join("internal", "cmd", "generator")
return strings.TrimSuffix(filepath.Dir(file), relativePathFromRepoRoot)
}
Loading

0 comments on commit a2ae972

Please sign in to comment.