Skip to content

Commit

Permalink
feat: remove arrange and add goschtalt
Browse files Browse the repository at this point in the history
  • Loading branch information
denopink committed Nov 1, 2023
1 parent fb3ab08 commit 06bfccf
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 88 deletions.
17 changes: 7 additions & 10 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

package bascule

import (
"github.com/xmidt-org/arrange"
)

type BasicAttributes map[string]interface{}

func (a BasicAttributes) Get(key string) (interface{}, bool) {
Expand Down Expand Up @@ -37,13 +33,14 @@ func GetNestedAttribute(attributes Attributes, keys ...string) (interface{}, boo
if result == nil {
return nil, false
}
ok = arrange.TryConvert(result,
func(attr Attributes) { a = attr },
func(m map[string]interface{}) { a = BasicAttributes(m) },
)
if !ok {
return nil, false

switch result.(type) {
case BasicAttributes:
a = attributes.(BasicAttributes)
case Attributes:
a = attributes
}

result, ok = a.Get(k)
if !ok {
return nil, false
Expand Down
6 changes: 3 additions & 3 deletions basculechecks/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package basculechecks

import (
"github.com/xmidt-org/arrange"
"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/bascule"
"go.uber.org/fx"
)
Expand All @@ -31,7 +31,7 @@ func ProvideMetricValidator(optional bool) fx.Option {
func ProvideCapabilitiesMapValidator(key string) fx.Option {
return fx.Options(
fx.Provide(
arrange.UnmarshalKey(key, CapabilitiesMapConfig{}),
goschtalt.UnmarshalFunc[CapabilitiesMapConfig](key),
NewCapabilitiesMap,
),
ProvideMetricValidator(false),
Expand All @@ -44,7 +44,7 @@ func ProvideCapabilitiesMapValidator(key string) fx.Option {
func ProvideRegexCapabilitiesValidator(key string) fx.Option {
return fx.Options(
fx.Provide(
arrange.UnmarshalKey(key, CapabilitiesValidatorConfig{}),
goschtalt.UnmarshalFunc[CapabilitiesValidatorConfig](key),
NewCapabilitiesValidator,
),
ProvideMetricValidator(true),
Expand Down
4 changes: 2 additions & 2 deletions basculehttp/basicTokenFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"fmt"
"net/http"

"github.com/xmidt-org/arrange"
"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/bascule"
"go.uber.org/fx"
)
Expand Down Expand Up @@ -110,7 +110,7 @@ func ProvideBasicTokenFactory(key string) fx.Option {
return fx.Provide(
fx.Annotated{
Name: "encoded_basic_auths",
Target: arrange.UnmarshalKey(key, EncodedBasicKeys{}),
Target: goschtalt.UnmarshalFunc[EncodedBasicKeys](key),
},
fx.Annotated{
Group: "bascule_constructor_options",
Expand Down
31 changes: 23 additions & 8 deletions basculehttp/basicTokenFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"net/http/httptest"
"strings"
"testing"
"testing/fstest"

"github.com/spf13/viper"
"github.com/goschtalt/goschtalt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/arrange"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/sallust"
"go.uber.org/fx"
)

Expand Down Expand Up @@ -137,10 +138,6 @@ good:
bad:
basic: ["AAAAAAAA"]
`
v := viper.New()
v.SetConfigType("yaml")
require.NoError(t, v.ReadConfig(strings.NewReader(yaml)))

tests := []struct {
description string
key string
Expand Down Expand Up @@ -168,8 +165,26 @@ bad:
assert := assert.New(t)
require := require.New(t)
app := fx.New(
arrange.TestLogger(t),
arrange.ForViper(v),
fx.Provide(
func() (c sallust.Config) {
return sallust.Config{}
},
func() (*goschtalt.Config, error) {
fs := fstest.MapFS{
"test.yml": &fstest.MapFile{
Data: []byte(yaml),
Mode: 0644,
},
}
gc, err := goschtalt.New(goschtalt.AddDir(fs, "."))
if err != nil {
return nil, err
}

return gc, nil
},
),
sallust.WithLogger(),
ProvideBasicTokenFactory(tc.key),
fx.Invoke(
func(in In) {
Expand Down
7 changes: 3 additions & 4 deletions basculehttp/bearerTokenFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net/http"

"github.com/golang-jwt/jwt"
"github.com/xmidt-org/arrange"
"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/clortho"
"github.com/xmidt-org/clortho/clorthofx"
Expand Down Expand Up @@ -104,9 +104,8 @@ func ProvideBearerTokenFactory(configKey string, optional bool) fx.Option {
clorthofx.Provide(),
fx.Provide(
fx.Annotated{
Name: "jwt_leeway",
Target: arrange.UnmarshalKey(fmt.Sprintf("%s.leeway", configKey),
bascule.Leeway{}),
Name: "jwt_leeway",
Target: goschtalt.UnmarshalFunc[bascule.Leeway](fmt.Sprintf("%s.leeway", configKey)),
},
fx.Annotated{
Group: "bascule_constructor_options",
Expand Down
29 changes: 21 additions & 8 deletions basculehttp/bearerTokenFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"net/http/httptest"
"strings"
"testing"
"testing/fstest"

"github.com/golang-jwt/jwt"
"github.com/spf13/viper"
"github.com/goschtalt/goschtalt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/arrange"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/sallust"
"go.uber.org/fx"
)

Expand Down Expand Up @@ -154,10 +155,6 @@ good:
purpose: 0
updateInterval: 604800000000000
`
v := viper.New()
v.SetConfigType("yaml")
require.NoError(t, v.ReadConfig(strings.NewReader(yaml)))

tests := []struct {
description string
key string
Expand Down Expand Up @@ -190,9 +187,25 @@ good:
return "default"
},
},
func() (c sallust.Config) {
return sallust.Config{}
},
func() (*goschtalt.Config, error) {
fs := fstest.MapFS{
"test.yml": &fstest.MapFile{
Data: []byte(yaml),
Mode: 0644,
},
}
gc, err := goschtalt.New(goschtalt.AddDir(fs, "."))
if err != nil {
return nil, err
}

return gc, nil
},
),
arrange.TestLogger(t),
arrange.ForViper(v),
sallust.WithLogger(),
ProvideBearerTokenFactory(tc.key, tc.optional),
fx.Invoke(
func(in In) {
Expand Down
53 changes: 39 additions & 14 deletions basculehttp/provide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
package basculehttp

import (
"strings"
"testing"
"testing/fstest"

"github.com/goschtalt/goschtalt"
"github.com/justinas/alice"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/arrange"
"github.com/xmidt-org/bascule/basculechecks"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/touchstone"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
Expand All @@ -36,9 +36,6 @@ capabilities:
".*/b/.*": "nm"
default: "eh"
`
v := viper.New()
v.SetConfigType("yaml")
require.NoError(v.ReadConfig(strings.NewReader(yaml)))
l, err := zap.NewDevelopment()
require.NoError(err)

Expand All @@ -51,17 +48,33 @@ capabilities:
t,

// supplying dependencies
arrange.LoggerFunc(l.Sugar().Infof),
sallust.WithLogger(),
fx.Supply(l),
arrange.ForViper(v),
touchstone.Provide(),
fx.Provide(
func() (c sallust.Config) {
return sallust.Config{}
},
fx.Annotated{
Name: "default_key_id",
Target: func() string {
return "current"
},
},
func() (*goschtalt.Config, error) {
fs := fstest.MapFS{
"test.yml": &fstest.MapFile{
Data: []byte(yaml),
Mode: 0644,
},
}
gc, err := goschtalt.New(goschtalt.AddDir(fs, "."))
if err != nil {
return nil, err
}

return gc, nil
},
),

// the parts we care about
Expand Down Expand Up @@ -129,10 +142,6 @@ capabilities:
t.Run(desc, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

v := viper.New()
v.SetConfigType("yaml")
require.NoError(v.ReadConfig(strings.NewReader(yaml)))
l, err := zap.NewDevelopment()
require.NoError(err)

Expand All @@ -141,17 +150,33 @@ capabilities:
t,

// supplying dependencies
arrange.LoggerFunc(l.Sugar().Infof),
sallust.WithLogger(),
fx.Supply(l),
arrange.ForViper(v),
touchstone.Provide(),
fx.Provide(
func() (c sallust.Config) {
return sallust.Config{}
},
fx.Annotated{
Name: "default_key_id",
Target: func() string {
return "current"
},
},
func() (*goschtalt.Config, error) {
fs := fstest.MapFS{
"test.yml": &fstest.MapFile{
Data: []byte(yaml),
Mode: 0644,
},
}
gc, err := goschtalt.New(goschtalt.AddDir(fs, "."))
if err != nil {
return nil, err
}

return gc, nil
},
),
// the parts we care about
ProvideMetrics(),
Expand Down
Loading

0 comments on commit 06bfccf

Please sign in to comment.