Merge pull request #1904 from artmello/enable_gocritic

kyaml: Enable gocritic Go linter
This commit is contained in:
Kubernetes Prow Robot
2019-12-12 06:56:32 -08:00
committed by GitHub
3 changed files with 14 additions and 10 deletions

View File

@@ -18,7 +18,7 @@ linters:
# - funlen
- gochecknoinits
- goconst
# - gocritic
- gocritic
- gocyclo
- gofmt
- goimports

View File

@@ -103,15 +103,16 @@ func (s ValueReplacer) Filter(object *RNode) (*RNode, error) {
if s.Count == 0 {
s.Count = -1
}
if s.StringMatch != "" {
switch {
case s.StringMatch != "":
object.value.Value = strings.Replace(object.value.Value, s.StringMatch, s.Replace, s.Count)
} else if s.RegexMatch != "" {
case s.RegexMatch != "":
r, err := regexp.Compile(s.RegexMatch)
if err != nil {
return nil, fmt.Errorf("ValueReplacer RegexMatch does not compile: %v", err)
}
object.value.Value = r.ReplaceAllString(object.value.Value, s.Replace)
} else {
default:
return nil, fmt.Errorf("ValueReplacer missing StringMatch and RegexMatch")
}
return object, nil
@@ -138,7 +139,7 @@ type SuffixSetter struct {
func (s SuffixSetter) Filter(object *RNode) (*RNode, error) {
if !strings.HasSuffix(object.value.Value, s.Value) {
object.value.Value = object.value.Value + s.Value
object.value.Value += s.Value
}
return object, nil
}

View File

@@ -141,10 +141,12 @@ func (c FieldClearer) Filter(rn *RNode) (*RNode, error) {
// save the item we are about to remove
removed := NewRNode(rn.Content()[i+1])
if len(rn.YNode().Content) > i+2 {
l := len(rn.YNode().Content)
// remove from the middle of the list
rn.YNode().Content = rn.Content()[:i]
rn.YNode().Content = append(
rn.Content()[:i],
rn.Content()[i+2:len(rn.YNode().Content)]...)
rn.YNode().Content,
rn.Content()[i+2:l]...)
} else {
// remove from the end of the list
rn.YNode().Content = rn.Content()[:i]
@@ -264,7 +266,8 @@ func (f FieldMatcher) Filter(rn *RNode) (*RNode, error) {
if err := ErrorIfInvalid(rn, yaml.ScalarNode); err != nil {
return nil, err
}
if f.StringRegexValue != "" {
switch {
case f.StringRegexValue != "":
// TODO(pwittrock): pre-compile this when unmarshalling and cache to a field
rg, err := regexp.Compile(f.StringRegexValue)
if err != nil {
@@ -274,9 +277,9 @@ func (f FieldMatcher) Filter(rn *RNode) (*RNode, error) {
return rn, nil
}
return nil, nil
} else if rn.value.Value == f.Value.YNode().Value {
case rn.value.Value == f.Value.YNode().Value:
return rn, nil
} else {
default:
return nil, nil
}
}