Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
S-YOU committed Aug 15, 2020
0 parents commit a6160d5
Show file tree
Hide file tree
Showing 28 changed files with 2,569 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 S-YOU

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
DB_SPANNER_SCHEMA := db/spanner.sql

## ENV
ROOT_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
GO_FILES_ALL = $(shell bin/golist --exclude-suffixes 'gen.go,_test.go,_mock.go,mock')

build-tools:
go build -o bin/golist github.com/s-you/golist
go build -o bin/yo go.mercari.io/yo
go build -o bin/goimports golang.org/x/tools/cmd/goimports
@[ -d .git ] && git checkout go.* || true

gen: yo-gen

yo-gen:
$(eval export PATH=$(shell pwd)/bin:$(PATH))
bin/yo generate $(DB_SPANNER_SCHEMA) -o internal/model \
--from-ddl \
--template-path ./templates/model \
--inflection-rule-file templates/inflection_rule.yml \
--suffix .gen.go \
--single-file
bin/yo generate $(DB_SPANNER_SCHEMA) -o internal/repository \
--from-ddl \
--template-path ./templates/repository \
--inflection-rule-file templates/inflection_rule.yml \
--custom-types-file templates/custom_types.yml \
--custom-type-package model \
--suffix .gen.go \
--single-file

### test
test:
go test -v -p 1 ./...

### fmt
fmt:
@bin/goimports -local github.com/s-you -l -w ${GO_FILES_ALL}

lint: vet
@LINT="$$(bin/goimports -local github.com/s-you -l -format-only ${GO_FILES_ALL})"; printf "$$LINT"; \
[ -z "$$LINT" ] && echo "lint ok"

### vet
vet:
go vet ./...
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# yo-templates
Empty file added bin/.gitkeep
Empty file.
39 changes: 39 additions & 0 deletions cmd/query/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
"fmt"
"log"

"github.com/s-you/yo-templates/internal/config"
"github.com/s-you/yo-templates/internal/pkg/spannerclient"
"github.com/s-you/yo-templates/internal/repository"
)

func main() {
conf, err := config.Load()
if err != nil {
panic(err)
}

ctx := context.Background()

sdb, err := spannerclient.Connect(ctx, conf.DB)
if err != nil {
panic(err)
}
defer sdb.Close()

u := repository.NewUserRepository(sdb)
if err := u.ExampleQuery(ctx); err != nil {
log.Fatal(err)
}

users, err := u.FindAll(ctx)
if err != nil {
log.Fatal(err)
}
for _, x := range users {
fmt.Println(x)
}
}
8 changes: 8 additions & 0 deletions db/spanner.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE `users` (
user_id STRING(36) NOT NULL,
name STRING(MAX) NOT NULL,
status INT64 NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
) PRIMARY KEY(user_id);
CREATE INDEX idx_users_name ON users(name);
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/s-you/yo-templates

go 1.15

require (
cloud.google.com/go/spanner v1.9.0
github.com/cespare/xxhash v1.1.0 // indirect
github.com/google/uuid v1.1.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/s-you/apierrors v0.0.0-20200815145538-274c30a720a8 // indirect
github.com/s-you/golist v0.0.0-20200822140259-fbfc6d6c8cca // indirect
github.com/s-you/spannerbuilder v0.0.0-20200905125047-67c25324bf59
go.mercari.io/yo v0.3.2 // indirect
google.golang.org/api v0.31.0
)
607 changes: 607 additions & 0 deletions go.sum

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import (
"github.com/kelseyhightower/envconfig"
)

type Config struct {
DB DB
}

func Load() (*Config, error) {
conf := &Config{}
if err := envconfig.Process("", conf); err != nil {
return nil, err
}
return conf, nil
}
18 changes: 18 additions & 0 deletions internal/config/spanner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package config

import (
"fmt"
)

type DB struct {
Project string `envconfig:"GOOGLE_CLOUD_PROJECT"`
Instance string `envconfig:"DB_SPANNER_INSTANCE"`
Database string `envconfig:"DB_SPANNER_DATABASE"`
Channels int `default:"4" split_words:"true"`
Debug bool `default:"false"`
Stats bool `default:"false"`
}

func (c DB) Format() string {
return fmt.Sprintf("projects/%s/instances/%s/databases/%s", c.Project, c.Instance, c.Database)
}
18 changes: 18 additions & 0 deletions internal/middleware/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middleware

import (
"context"
"log"

"github.com/s-you/yo-templates/internal/pkg/cache"
)

type cacheKey struct{}

func CacheFromContext(ctx context.Context) *cache.Cache {
if m, ok := ctx.Value(cacheKey{}).(*cache.Cache); ok {
return m
}
log.Println("cache did not set in context")
return cache.New()
}
156 changes: 156 additions & 0 deletions internal/model/model.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions internal/model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package model

import (
"time"

"github.com/s-you/yo-templates/internal/util"
)

type User struct {
UserID string
Name string
Status int64
CreatedAt time.Time
UpdatedAt time.Time
}

func (u *User) SetIdentity() (err error) {
if u.UserID == "" {
u.UserID, err = util.NewUUID()
}
return nil
}
25 changes: 25 additions & 0 deletions internal/pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cache

import (
"time"

gocache "github.com/patrickmn/go-cache"
)

func New() *Cache {
return &Cache{
cache: gocache.New(0, 10*time.Minute),
}
}

type Cache struct {
cache *gocache.Cache
}

func (c *Cache) Get(k string) (interface{}, bool) {
return c.cache.Get(k)
}

func (c *Cache) Set(k string, v interface{}) {
c.cache.Set(k, v, gocache.NoExpiration)
}
Loading

0 comments on commit a6160d5

Please sign in to comment.