39 lines
854 B
Go
39 lines
854 B
Go
package db
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/liwei1dao/lego/utils/mapstructure"
|
|
)
|
|
|
|
type Option func(*Options)
|
|
type Options struct {
|
|
MongodbUrl string
|
|
MongodbDatabase string
|
|
}
|
|
|
|
func newOptions(config map[string]interface{}, opts ...Option) (Options, error) {
|
|
options := Options{}
|
|
if config != nil {
|
|
mapstructure.Decode(config, &options)
|
|
}
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
if len(options.MongodbUrl) == 0 || len(options.MongodbDatabase) == 0 {
|
|
return options, errors.New("MongodbUrl or MongodbDatabase is null")
|
|
}
|
|
return options, nil
|
|
}
|
|
|
|
func newOptionsByOption(opts ...Option) (Options, error) {
|
|
options := Options{}
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
if len(options.MongodbUrl) == 0 || len(options.MongodbDatabase) == 0 {
|
|
return options, errors.New("MongodbUrl or MongodbDatabase is null")
|
|
}
|
|
return options, nil
|
|
}
|