Automatic Merge
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
9a67ae9ac6
Коммит
c100fc2135
@@ -6,7 +6,6 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
)
|
||||
@@ -72,7 +71,7 @@ func (cd ConfigDiffs) Sanitize() ConfigDiffs {
|
||||
return cd
|
||||
}
|
||||
|
||||
func diff(base, actual reflect.Value, structField reflect.StructField, label string, tag, tagValue string) ([]ConfigDiff, error) {
|
||||
func diff(base, actual reflect.Value, label string) ([]ConfigDiff, error) {
|
||||
var diffs []ConfigDiff
|
||||
|
||||
if base.IsZero() && actual.IsZero() {
|
||||
@@ -101,42 +100,17 @@ func diff(base, actual reflect.Value, structField reflect.StructField, label str
|
||||
return nil, fmt.Errorf("not same type %s %s", baseType, actualType)
|
||||
}
|
||||
|
||||
// skip if not tag scoped, field does not have any tags or if it's just empty
|
||||
if tag != "" && string(structField.Tag) != "" && structField.Name != "" {
|
||||
// we are getting the diffs scoped with a specific tag
|
||||
// therefore we first lookup if the field has the tag, if not we skip
|
||||
// to check if it's changed or not as it's out of the scope
|
||||
val, ok := structField.Tag.Lookup(tag)
|
||||
if !ok {
|
||||
return diffs, nil
|
||||
}
|
||||
|
||||
// tag scope also cares about the tag value, if we don't have the value
|
||||
// there is no need to get the diff
|
||||
if !strings.Contains(val, tagValue) {
|
||||
return diffs, nil
|
||||
}
|
||||
|
||||
// prevent going further scoping according this tag because we are already
|
||||
// scoped the struct on higher level
|
||||
if baseType.Kind() == reflect.Struct {
|
||||
tag = ""
|
||||
}
|
||||
}
|
||||
|
||||
switch baseType.Kind() {
|
||||
case reflect.Struct:
|
||||
if base.NumField() != actual.NumField() {
|
||||
return nil, fmt.Errorf("not same number of fields in struct")
|
||||
}
|
||||
|
||||
for i := 0; i < base.NumField(); i++ {
|
||||
fieldLabel := baseType.Field(i).Name
|
||||
if label != "" {
|
||||
fieldLabel = label + "." + fieldLabel
|
||||
}
|
||||
|
||||
d, err := diff(base.Field(i), actual.Field(i), actualType.Field(i), fieldLabel, tag, tagValue)
|
||||
d, err := diff(base.Field(i), actual.Field(i), fieldLabel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -155,24 +129,13 @@ func diff(base, actual reflect.Value, structField reflect.StructField, label str
|
||||
return diffs, nil
|
||||
}
|
||||
|
||||
// Diff returns the diff between two configs
|
||||
func Diff(base, actual *model.Config) (ConfigDiffs, error) {
|
||||
if base == nil || actual == nil {
|
||||
return nil, fmt.Errorf("input configs should not be nil")
|
||||
}
|
||||
baseVal := reflect.Indirect(reflect.ValueOf(base))
|
||||
actualVal := reflect.Indirect(reflect.ValueOf(actual))
|
||||
return diff(baseVal, actualVal, reflect.StructField{}, "", "", "")
|
||||
}
|
||||
|
||||
// DiffTags behaves similar with Diff but it is scoped against a tag and it's value
|
||||
func DiffTags(base, actual *model.Config, tag, value string) (ConfigDiffs, error) {
|
||||
if base == nil || actual == nil {
|
||||
return nil, fmt.Errorf("input configs should not be nil")
|
||||
}
|
||||
baseVal := reflect.Indirect(reflect.ValueOf(base))
|
||||
actualVal := reflect.Indirect(reflect.ValueOf(actual))
|
||||
return diff(baseVal, actualVal, reflect.StructField{}, "", tag, value)
|
||||
return diff(baseVal, actualVal, "")
|
||||
}
|
||||
|
||||
func (cd ConfigDiffs) String() string {
|
||||
|
||||
@@ -958,105 +958,3 @@ func TestDiff(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiffTags(t *testing.T) {
|
||||
tcs := []struct {
|
||||
name string
|
||||
base *model.Config
|
||||
actual *model.Config
|
||||
diffs ConfigDiffs
|
||||
tag string
|
||||
value string
|
||||
err string
|
||||
}{
|
||||
{
|
||||
name: "changed field is not in scope",
|
||||
base: defaultConfigGen(),
|
||||
actual: func() *model.Config {
|
||||
cfg := defaultConfigGen()
|
||||
cfg.ServiceSettings.EnableLinkPreviews = model.NewBool(false)
|
||||
return cfg
|
||||
}(),
|
||||
diffs: nil,
|
||||
tag: "access",
|
||||
value: "cloud_restrictable",
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "changed field is in scope",
|
||||
base: defaultConfigGen(),
|
||||
actual: func() *model.Config {
|
||||
cfg := defaultConfigGen()
|
||||
cfg.ServiceSettings.ReadTimeout = model.NewInt(500)
|
||||
return cfg
|
||||
}(),
|
||||
diffs: ConfigDiffs{
|
||||
{
|
||||
Path: "ServiceSettings.ReadTimeout",
|
||||
BaseVal: 300,
|
||||
ActualVal: 500,
|
||||
},
|
||||
},
|
||||
tag: "access",
|
||||
value: "cloud_restrictable",
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "changed struct is in scope",
|
||||
base: defaultConfigGen(),
|
||||
actual: func() *model.Config {
|
||||
cfg := defaultConfigGen()
|
||||
cfg.BleveSettings.BatchSize = model.NewInt(500)
|
||||
return cfg
|
||||
}(),
|
||||
diffs: ConfigDiffs{
|
||||
{
|
||||
Path: "BleveSettings.BatchSize",
|
||||
BaseVal: 10000,
|
||||
ActualVal: 500,
|
||||
},
|
||||
},
|
||||
tag: "access",
|
||||
value: "cloud_restrictable",
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "changed struct is not in scope",
|
||||
base: defaultConfigGen(),
|
||||
actual: func() *model.Config {
|
||||
cfg := defaultConfigGen()
|
||||
cfg.LocalizationSettings.DefaultServerLocale = model.NewString("Elvish")
|
||||
return cfg
|
||||
}(),
|
||||
diffs: nil,
|
||||
tag: "access",
|
||||
value: "cloud_restrictable",
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "changed field is not in scope due to its value but has the same tag",
|
||||
base: defaultConfigGen(),
|
||||
actual: func() *model.Config {
|
||||
cfg := defaultConfigGen()
|
||||
cfg.TeamSettings.SiteName = model.NewString("Mordor")
|
||||
return cfg
|
||||
}(),
|
||||
diffs: nil,
|
||||
tag: "access",
|
||||
value: "cloud_restrictable",
|
||||
err: "",
|
||||
},
|
||||
}
|
||||
for _, tc := range tcs {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
diffs, err := DiffTags(tc.base, tc.actual, tc.tag, tc.value)
|
||||
if tc.err != "" {
|
||||
require.EqualError(t, err, tc.err)
|
||||
require.Nil(t, diffs)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
require.Equal(t, tc.diffs, diffs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user