Comments (4)
func NewWithViper(viper *viper.Viper) (*Goss, error) {}
这种方式可否满足你的需要?
from goss.
func NewWithViper(viper *viper.Viper) (*Goss, error) {}这种方式可否满足你的需要?
可以,作为一个Golang新手,我有一个疑问,如果其他程序是把配置文件解析到了结构体中,类似这样
type DriverType struct {
Driver string `yaml:"driver"`
Aliyun Aliyun `yaml:"aliyun"`
Tencent Tencent `yaml:"tencent"`
Qiniu Qiniu `yaml:"qiniu"`
Huawei Huawei `yaml:"huawei"`
S3 S3 `yaml:"s3"`
Minio Minio `yaml:"minio"`
}
type Aliyun struct {
Endpoint string `yaml:"endpoint"`
Bucket string `yaml:"bucket"`
AccessKeyID string `yaml:"access_key_id"`
AccessKeySecret string `yaml:"access_key_secret"`
}
type Tencent struct {
URL string `yaml:"url"`
SecretID string `yaml:"secret_id"`
SecretKey string `yaml:"secret_key"`
}
type Qiniu struct {
Bucket string `yaml:"bucket"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
Zone string `yaml:"zone"`
Domain string `yaml:"domain"`
Private string `yaml:"private"`
}
type Huawei struct {
Endpoint string `yaml:"endpoint"`
Location string `yaml:"location"`
Bucket string `yaml:"bucket"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
}
type S3 struct {
Endpoint string `yaml:"endpoint"`
Region string `yaml:"region"`
Bucket string `yaml:"bucket"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
}
type Minio struct {
Endpoint string `yaml:"endpoint"`
Bucket string `yaml:"bucket"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
UseSsl bool `yaml:"use_ssl"`
}
您提供的这种方式是不是就不能使用了?
from goss.
办法是有的,就是不太优雅,你可以参考一下下面的这种写法:
type Minio struct {
Endpoint string `yaml:"endpoint"`
Bucket string `yaml:"bucket"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
UseSsl bool `yaml:"use_ssl"`
}
func newViper(name string, value interface{}) *viper.Viper {
v := viper.New()
r := reflect.ValueOf(value)
for i := 0; i < r.NumField(); i++ {
v.Set(name+"."+r.Type().Field(i).Tag.Get("yaml"), r.Field(i).Interface())
}
return v
}
func TestMinio(t *testing.T) {
m := Minio{
Endpoint: "aa",
Bucket: "bb",
AccessKey: "cc",
SecretKey: "dd",
UseSsl: true,
}
v := newViper("minio", m)
assert.Equal(t, "aa", v.GetString("minio.endpoint"))
assert.Equal(t, "bb", v.GetString("minio.bucket"))
assert.Equal(t, "cc", v.GetString("minio.access_key"))
assert.Equal(t, "dd", v.GetString("minio.secret_key"))
assert.Equal(t, true, v.GetBool("minio.use_ssl"))
}
可以根据配置的结构体和 oss 类型来创建一个 *viper.Viper
类型的对象。
这里用到了反射,不过一般来说应用内只会初始化一次,所以问题应该不大。
from goss.
明白了,感谢解答。
from goss.
Related Issues (4)
- 使用S3协议无法存入MinIO,能支持MinIO吗? HOT 1
- 请问七牛云怎么配置 HOT 2
- 【需求】新增获取OSS上传凭证
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goss.