Skip to content

Commit

Permalink
Add config.New(config.WithConfigKey()) to init config with a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Dec 12, 2023
1 parent c15d7d1 commit 24b57d9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ func New(opts ...Option) (configData *Config, err error) {
return
}

if options.appKey != nil {
configData.AppKey = *options.appKey
}
if options.appSecret != nil {
configData.AppSecret = *options.appSecret
}
if options.accessToken != nil {
configData.AccessToken = *options.accessToken
}

if configData.Region == RegionCN {
configData.HttpURL = cnHttpUrl
configData.QuoteUrl = cnQuoteUrl
Expand Down
16 changes: 16 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package config_test

import (
"testing"

"github.com/longbridgeapp/assert"
"github.com/longportapp/openapi-go/config"
)

func Test_withConfigKey(t *testing.T) {
var c, err = config.New(config.WithConfigKey("appKey", "appSecret", "accessToken"))
assert.NoError(t, err)
assert.Equal(t, "appKey", c.AppKey)
assert.Equal(t, "appSecret", c.AppSecret)
assert.Equal(t, "accessToken", c.AccessToken)
}
13 changes: 13 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
type Options struct {
tp ConfigType
filePath string

appKey *string
appSecret *string
accessToken *string
}

type Option func(*Options)
Expand All @@ -24,6 +28,15 @@ func WithFilePath(filePath string) Option {
}
}

// WithConfigKey config appKey, appSecret, accessToken
func WithConfigKey(appKey string, appSecret string, accessToken string) Option {
return func(o *Options) {
o.appKey = &appKey
o.appSecret = &appSecret
o.accessToken = &accessToken
}
}

func newOptions(opt ...Option) *Options {
opts := Options{
tp: ConfigTypeEnv,
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/google/go-querystring v1.1.0
github.com/jinzhu/copier v0.3.5
github.com/joho/godotenv v1.4.0
github.com/longbridgeapp/assert v0.1.0
github.com/longportapp/openapi-protobufs/gen/go v0.2.1
github.com/longportapp/openapi-protocol/go v0.3.0
github.com/pkg/errors v0.9.1
Expand All @@ -19,7 +20,10 @@ require (

require (
github.com/Allenxuxu/ringbuffer v0.0.11 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

0 comments on commit 24b57d9

Please sign in to comment.