Skip to content

Commit 24b57d9

Browse files
committed
Add config.New(config.WithConfigKey()) to init config with a string.
1 parent c15d7d1 commit 24b57d9

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ func New(opts ...Option) (configData *Config, err error) {
8181
return
8282
}
8383

84+
if options.appKey != nil {
85+
configData.AppKey = *options.appKey
86+
}
87+
if options.appSecret != nil {
88+
configData.AppSecret = *options.appSecret
89+
}
90+
if options.accessToken != nil {
91+
configData.AccessToken = *options.accessToken
92+
}
93+
8494
if configData.Region == RegionCN {
8595
configData.HttpURL = cnHttpUrl
8696
configData.QuoteUrl = cnQuoteUrl

config/config_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package config_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/longbridgeapp/assert"
7+
"github.com/longportapp/openapi-go/config"
8+
)
9+
10+
func Test_withConfigKey(t *testing.T) {
11+
var c, err = config.New(config.WithConfigKey("appKey", "appSecret", "accessToken"))
12+
assert.NoError(t, err)
13+
assert.Equal(t, "appKey", c.AppKey)
14+
assert.Equal(t, "appSecret", c.AppSecret)
15+
assert.Equal(t, "accessToken", c.AccessToken)
16+
}

config/options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
type Options struct {
88
tp ConfigType
99
filePath string
10+
11+
appKey *string
12+
appSecret *string
13+
accessToken *string
1014
}
1115

1216
type Option func(*Options)
@@ -24,6 +28,15 @@ func WithFilePath(filePath string) Option {
2428
}
2529
}
2630

31+
// WithConfigKey config appKey, appSecret, accessToken
32+
func WithConfigKey(appKey string, appSecret string, accessToken string) Option {
33+
return func(o *Options) {
34+
o.appKey = &appKey
35+
o.appSecret = &appSecret
36+
o.accessToken = &accessToken
37+
}
38+
}
39+
2740
func newOptions(opt ...Option) *Options {
2841
opts := Options{
2942
tp: ConfigTypeEnv,

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/google/go-querystring v1.1.0
1111
github.com/jinzhu/copier v0.3.5
1212
github.com/joho/godotenv v1.4.0
13+
github.com/longbridgeapp/assert v0.1.0
1314
github.com/longportapp/openapi-protobufs/gen/go v0.2.1
1415
github.com/longportapp/openapi-protocol/go v0.3.0
1516
github.com/pkg/errors v0.9.1
@@ -19,7 +20,10 @@ require (
1920

2021
require (
2122
github.com/Allenxuxu/ringbuffer v0.0.11 // indirect
23+
github.com/davecgh/go-spew v1.1.1 // indirect
2224
github.com/gorilla/websocket v1.5.0 // indirect
2325
github.com/kr/text v0.2.0 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
27+
github.com/stretchr/testify v1.7.0 // indirect
2428
google.golang.org/protobuf v1.28.1 // indirect
2529
)

0 commit comments

Comments
 (0)