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 2, 2023
1 parent fb3ab08 commit 7ef3c1b
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 107 deletions.
22 changes: 10 additions & 12 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 @@ -34,20 +30,22 @@ func GetNestedAttribute(attributes Attributes, keys ...string) (interface{}, boo
result = attributes
for _, k := range keys {
var a Attributes
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 {
switch r := result.(type) {
case BasicAttributes:
a = r
case Attributes:
a = r
case map[string]interface{}:
a = BasicAttributes(r)
default:
return nil, false
}

result, ok = a.Get(k)
if !ok {
return nil, false
}
}

return result, ok
}
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
37 changes: 27 additions & 10 deletions basculehttp/basicTokenFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ 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"

_ "github.com/goschtalt/yaml-decoder"
)

func TestBasicTokenFactory(t *testing.T) {
Expand Down Expand Up @@ -137,10 +140,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 +167,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, gc.Compile()
},
),
sallust.WithLogger(),
ProvideBasicTokenFactory(tc.key),
fx.Invoke(
func(in In) {
Expand All @@ -179,8 +196,8 @@ bad:
)
err := app.Err()
if tc.expectedErr == nil {
assert.NoError(err)
assert.True(len(result.Options) == 1)
require.NoError(err)
require.True(len(result.Options) == 1)
if tc.optionExpected {
require.NotNil(result.Options[0])
return
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
35 changes: 25 additions & 10 deletions basculehttp/bearerTokenFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ 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"

_ "github.com/goschtalt/yaml-decoder"
)

func TestBearerTokenFactory(t *testing.T) {
Expand Down Expand Up @@ -154,10 +157,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 +189,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, gc.Compile()
},
),
arrange.TestLogger(t),
arrange.ForViper(v),
sallust.WithLogger(),
ProvideBearerTokenFactory(tc.key, tc.optional),
fx.Invoke(
func(in In) {
Expand All @@ -202,8 +217,8 @@ good:
)
err := app.Err()
if tc.expectedErr == nil {
assert.NoError(err)
assert.True(len(result.Options) == 1)
require.NoError(err)
require.True(len(result.Options) == 1)
if tc.optionExpected {
require.NotNil(result.Options[0])
return
Expand Down
Loading

0 comments on commit 7ef3c1b

Please sign in to comment.