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

refactor: corp package #1402

Merged
merged 18 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ docs/cli: .bin/clidoc
https://github.com/containous/traefik/releases/download/v2.3.0-rc4/traefik_v2.3.0-rc4_linux_amd64.tar.gz \
tar -zxvf traefik_${traefik_version}_linux_${arch}.tar.gz

.bin/cli: go.mod go.sum Makefile
go build -o .bin/cli -tags sqlite github.com/ory/cli
.bin/ory: Makefile
bash <(curl https://raw.githubusercontent.com/ory/cli/master/install.sh) -b .bin v0.0.53
touch -a -m .bin/ory

node_modules: package.json Makefile
npm ci
Expand Down Expand Up @@ -85,12 +86,12 @@ test-coverage: .bin/go-acc .bin/goveralls

# Generates the SDK
.PHONY: sdk
sdk: .bin/swagger .bin/cli node_modules
sdk: .bin/swagger .bin/ory node_modules
swagger generate spec -m -o spec/swagger.json -x github.com/ory/kratos-client-go
cli dev swagger sanitize ./spec/swagger.json
ory dev swagger sanitize ./spec/swagger.json
swagger validate ./spec/swagger.json
CIRCLE_PROJECT_USERNAME=ory CIRCLE_PROJECT_REPONAME=kratos \
cli dev openapi migrate \
ory dev openapi migrate \
-p https://raw.githubusercontent.com/ory/x/master/healthx/openapi/patch.yaml \
-p file://.schema/openapi/patches/meta.yaml \
-p file://.schema/openapi/patches/schema.yaml \
Expand Down Expand Up @@ -148,16 +149,16 @@ test-e2e: node_modules test-resetdb
test/e2e/run.sh mysql

.PHONY: migrations-sync
migrations-sync: .bin/cli
cli dev pop migration sync persistence/sql/migrations/templates persistence/sql/migratest/testdata
migrations-sync: .bin/ory
ory dev pop migration sync persistence/sql/migrations/templates persistence/sql/migratest/testdata

.PHONY: migrations-render
migrations-render: .bin/cli
cli dev pop migration render persistence/sql/migrations/templates persistence/sql/migrations/sql
migrations-render: .bin/ory
ory dev pop migration render persistence/sql/migrations/templates persistence/sql/migrations/sql

.PHONY: migrations-render-replace
migrations-render-replace: .bin/cli
cli dev pop migration render -r persistence/sql/migrations/templates persistence/sql/migrations/sql
migrations-render-replace: .bin/ory
ory dev pop migration render -r persistence/sql/migrations/templates persistence/sql/migrations/sql

.PHONY: migratest-refresh
migratest-refresh:
Expand Down
20 changes: 14 additions & 6 deletions corp/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,30 @@ type Contextualizer interface {
ContextualizeNID(ctx context.Context, fallback uuid.UUID) uuid.UUID
}

var DefaultContextualizer Contextualizer = noopContextualizer{}
var c Contextualizer = nil

// These global functions call the respective method on DefaultContextualizer
func SetContextualizer(cc Contextualizer) {
if _, ok := cc.(*ContextNoOp); ok && c != nil {
return
}

c = cc
}

// These global functions call the respective method on Context

func ContextualizeTableName(ctx context.Context, name string) string {
return DefaultContextualizer.ContextualizeTableName(ctx, name)
return c.ContextualizeTableName(ctx, name)
}

func ContextualizeMiddleware(ctx context.Context) func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
return DefaultContextualizer.ContextualizeMiddleware(ctx)
return c.ContextualizeMiddleware(ctx)
}

func ContextualizeConfig(ctx context.Context, fb *config.Config) *config.Config {
return DefaultContextualizer.ContextualizeConfig(ctx, fb)
return c.ContextualizeConfig(ctx, fb)
}

func ContextualizeNID(ctx context.Context, fallback uuid.UUID) uuid.UUID {
return DefaultContextualizer.ContextualizeNID(ctx, fallback)
return c.ContextualizeNID(ctx, fallback)
}
33 changes: 33 additions & 0 deletions corp/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package corp

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type contextMock struct {
ContextNoOp
}

func (*contextMock) ContextualizeTableName(_ context.Context, name string) string {
return "foo"
}

func TestSetContextualizer(t *testing.T) {
ctx := context.Background()
require.Panics(t, func() {
ContextualizeTableName(ctx, "")
})

SetContextualizer(new(ContextNoOp))
assert.Equal(t, "bar", ContextualizeTableName(ctx, "bar"))

SetContextualizer(new(contextMock))
assert.Equal(t, "foo", ContextualizeTableName(ctx, "bar"))

SetContextualizer(new(ContextNoOp))
assert.Equal(t, "foo", ContextualizeTableName(ctx, "bar"))
}
7 changes: 0 additions & 7 deletions corp/go.mod

This file was deleted.

2 changes: 0 additions & 2 deletions corp/go.sum

This file was deleted.

10 changes: 5 additions & 5 deletions corp/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
"github.com/ory/kratos/driver/config"
)

type noopContextualizer struct{}
type ContextNoOp struct{}

func (noopContextualizer) ContextualizeTableName(_ context.Context, name string) string {
func (*ContextNoOp) ContextualizeTableName(_ context.Context, name string) string {
return name
}

func (noopContextualizer) ContextualizeMiddleware(_ context.Context) func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
func (*ContextNoOp) ContextualizeMiddleware(_ context.Context) func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
return func(w http.ResponseWriter, r *http.Request, n http.HandlerFunc) {
n(w, r)
}
}

func (noopContextualizer) ContextualizeConfig(ctx context.Context, fb *config.Config) *config.Config {
func (*ContextNoOp) ContextualizeConfig(ctx context.Context, fb *config.Config) *config.Config {
return fb
}

func (noopContextualizer) ContextualizeNID(_ context.Context, fallback uuid.UUID) uuid.UUID {
func (*ContextNoOp) ContextualizeNID(_ context.Context, fallback uuid.UUID) uuid.UUID {
return fallback
}
23 changes: 13 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ replace gopkg.in/DataDog/dd-trace-go.v1 => gopkg.in/DataDog/dd-trace-go.v1 v1.27
// official SDK, allowing for the Ory CLI to consume Ory Kratos' CLI commands.
replace github.com/ory/kratos-client-go => ./internal/httpclient

// Use the internal name for tablename generation
replace github.com/ory/kratos/corp => ./corp

replace github.com/ory/cli => github.com/ory/cli v0.0.53

replace go.mongodb.org/mongo-driver => go.mongodb.org/mongo-driver v1.4.6

replace github.com/gobuffalo/pop/v5 => github.com/gobuffalo/pop/v5 v5.3.2-0.20210412125924-f5ad9021d6ac

replace github.com/mattn/go-sqlite3 => github.com/mattn/go-sqlite3 v1.14.7

replace github.com/ory/x => github.com/ory/x v0.0.250

require (
github.com/DataDog/datadog-go v4.7.0+incompatible // indirect
github.com/HdrHistogram/hdrhistogram-go v1.1.0 // indirect
github.com/Masterminds/sprig/v3 v3.0.0
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0
github.com/bwmarrin/discordgo v0.23.0
github.com/bxcodec/faker/v3 v3.3.1
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/containerd/containerd v1.5.2 // indirect
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/davidrjonas/semver-cli v0.0.0-20190116233701-ee19a9a0dda6
github.com/fatih/color v1.9.0
Expand All @@ -48,7 +48,7 @@ require (
github.com/hashicorp/consul/api v1.5.0
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/hashicorp/golang-lru v0.5.4
github.com/imdario/mergo v0.3.7
github.com/imdario/mergo v0.3.11
github.com/inhies/go-bytesize v0.0.0-20201103132853-d0aed0d254f8
github.com/jteeuwen/go-bindata v3.0.7+incompatible
github.com/julienschmidt/httprouter v1.3.0
Expand All @@ -59,20 +59,20 @@ require (
github.com/mikefarah/yq v1.15.0
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe
github.com/morikuni/aec v1.0.0 // indirect
github.com/ory/analytics-go/v4 v4.0.0
github.com/ory/cli v0.0.50
github.com/ory/dockertest/v3 v3.6.5
github.com/ory/go-acc v0.2.6
github.com/ory/go-convenience v0.1.0
github.com/ory/graceful v0.1.1
github.com/ory/herodot v0.9.6
github.com/ory/jsonschema/v3 v3.0.3
github.com/ory/kratos-client-go v0.6.3-alpha.1
github.com/ory/kratos/corp v0.0.0-00010101000000-000000000000
github.com/ory/mail/v3 v3.0.0
github.com/ory/nosurf v1.2.4
github.com/ory/x v0.0.247
github.com/ory/x v0.0.250
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.6.0
github.com/sirupsen/logrus v1.8.1
Expand All @@ -83,9 +83,12 @@ require (
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.7.1
github.com/tidwall/sjson v1.1.5
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/urfave/negroni v1.0.0
github.com/ziutek/mymysql v1.5.4 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/oauth2 v0.0.0-20210323180902-22b0adad7558
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/tools v0.1.0
gopkg.in/gorp.v1 v1.7.2 // indirect
)
Loading