fix config nil error

This commit is contained in:
Jeff
2019-09-23 14:00:56 +08:00
committed by zryfish
parent 3dca9feb0f
commit 468f7eddca
18 changed files with 68 additions and 23 deletions

View File

@@ -11,6 +11,11 @@ func NewAlertingOptions() *AlertingOptions {
}
func (s *AlertingOptions) ApplyTo(options *AlertingOptions) {
if options == nil {
options = s
return
}
if s.Endpoint != "" {
options.Endpoint = s.Endpoint
}

View File

@@ -25,7 +25,12 @@ func NewDevopsOptions() *DevopsOptions {
// ApplyTo apply configuration to another options
func (s *DevopsOptions) ApplyTo(options *DevopsOptions) {
if s.Host != "" && options != nil {
if options == nil {
options = s
return
}
if s.Host != "" {
reflectutils.Override(options, s)
}
}

View File

@@ -26,6 +26,11 @@ func NewElasticSearchOptions() *ElasticSearchOptions {
}
func (s *ElasticSearchOptions) ApplyTo(options *ElasticSearchOptions) {
if options == nil {
options = s
return
}
if s.Host != "" {
reflectutils.Override(options, s)
}

View File

@@ -46,6 +46,10 @@ func (k *KubernetesOptions) Validate() []error {
}
func (k *KubernetesOptions) ApplyTo(options *KubernetesOptions) {
if options == nil {
options = k
return
}
reflectutils.Override(options, k)
}

View File

@@ -16,6 +16,10 @@ func NewKubeSphereOptions() *KubeSphereOptions {
}
func (s *KubeSphereOptions) ApplyTo(options *KubeSphereOptions) {
if options == nil {
options = s
return
}
if s.AccountServer != "" {
options.AccountServer = s.AccountServer
}

View File

@@ -31,6 +31,11 @@ func (l *LdapOptions) Validate() []error {
}
func (l *LdapOptions) ApplyTo(options *LdapOptions) {
if options == nil {
options = l
return
}
if l.Host != "" {
reflectutils.Override(options, l)
}

View File

@@ -34,6 +34,10 @@ func (m *MySQLOptions) Validate() []error {
}
func (m *MySQLOptions) ApplyTo(options *MySQLOptions) {
if options == nil {
options = m
return
}
reflectutils.Override(options, m)
}

View File

@@ -11,6 +11,11 @@ func NewNotificationOptions() *NotificationOptions {
}
func (s *NotificationOptions) ApplyTo(options *NotificationOptions) {
if options == nil {
options = s
return
}
if s.Endpoint != "" {
options.Endpoint = s.Endpoint
}

View File

@@ -19,6 +19,11 @@ func NewOpenPitrixOptions() *OpenPitrixOptions {
}
func (s *OpenPitrixOptions) ApplyTo(options *OpenPitrixOptions) {
if options == nil {
options = s
return
}
if s.APIServer != "" {
reflectutils.Override(options, s)
}

View File

@@ -23,6 +23,11 @@ func (s *PrometheusOptions) Validate() []error {
}
func (s *PrometheusOptions) ApplyTo(options *PrometheusOptions) {
if options == nil {
options = s
return
}
if s.Endpoint != "" {
options.Endpoint = s.Endpoint
}

View File

@@ -44,6 +44,11 @@ func (r *RedisOptions) Validate() []error {
// ApplyTo apply to another options if it's a enabled option(non empty host)
func (r *RedisOptions) ApplyTo(options *RedisOptions) {
if options == nil {
options = r
return
}
if r.Host != "" {
reflectutils.Override(options, r)
}

View File

@@ -40,6 +40,11 @@ func (s *S3Options) Validate() []error {
// ApplyTo overrides options if it's valid, which endpoint is not empty
func (s *S3Options) ApplyTo(options *S3Options) {
if options == nil {
options = s
return
}
if s.Endpoint != "" {
reflectutils.Override(options, s)
}

View File

@@ -31,6 +31,7 @@ func (s *ServiceMeshOptions) Validate() []error {
func (s *ServiceMeshOptions) ApplyTo(options *ServiceMeshOptions) {
if options == nil {
options = s
return
}

View File

@@ -28,6 +28,7 @@ func (s *SonarQubeOptions) Validate() []error {
func (s *SonarQubeOptions) ApplyTo(options *SonarQubeOptions) {
if options == nil {
options = s
return
}

View File

@@ -37,22 +37,23 @@ func In(value interface{}, container interface{}) bool {
}
func Override(left interface{}, right interface{}) {
if left == nil || right == nil {
if reflect.ValueOf(left).IsNil() || reflect.ValueOf(right).IsNil() {
return
}
if reflect.ValueOf(left).Type().Kind() != reflect.Ptr ||
reflect.ValueOf(right).Type().Kind() != reflect.Ptr {
reflect.ValueOf(right).Type().Kind() != reflect.Ptr ||
reflect.ValueOf(left).Kind() != reflect.ValueOf(right).Kind() {
return
}
old := reflect.ValueOf(left).Elem()
new := reflect.ValueOf(right).Elem()
oldVal := reflect.ValueOf(left).Elem()
newVal := reflect.ValueOf(right).Elem()
for i := 0; i < old.NumField(); i++ {
val := new.Field(i).Interface()
for i := 0; i < oldVal.NumField(); i++ {
val := newVal.Field(i).Interface()
if !reflect.DeepEqual(val, reflect.Zero(reflect.TypeOf(val)).Interface()) {
old.Field(i).Set(reflect.ValueOf(val))
oldVal.Field(i).Set(reflect.ValueOf(val))
}
}
}