Skip to content

Commit

Permalink
Update golangci-lint configuration and fix code accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Aug 24, 2024
1 parent ed6db0c commit da2d723
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 106 deletions.
18 changes: 5 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ issues:
text: "does not use range value in test Run"

linters-settings:
nolintlint:
# Disable to ensure that nolint directives don't have a leading space. Default is true.
allow-leading-space: false
exhaustive:
default-signifies-exhaustive: true
govet:
check-shadowing: true
enable-all: true
disable:
- fieldalignment
cyclop:
max-complexity: 25
maligned:
suggest-new: true
dupl:
threshold: 150
goconst:
Expand All @@ -52,6 +49,7 @@ linters-settings:
linters:
enable-all: true
disable:
- depguard
- nlreturn
- gci
- gochecknoinits
Expand All @@ -61,16 +59,11 @@ linters:
- testpackage
- wsl
- gomnd
- goerr113 # most of the errors here are meant for humans
- err113
- goheader
- exhaustivestruct
- thelper
- gocyclo # replaced by cyclop since it also calculates the package complexity
- maligned # replaced by govet 'fieldalignment'
- interfacer # deprecated
- scopelint # deprecated, replaced by exportloopref
- wrapcheck # a little bit too much for k6, maybe after https://github.com/tomarrell/wrapcheck/issues/2 is fixed
- golint # this linter is deprecated
- varnamelen
- ireturn
- tagliatelle
Expand All @@ -80,5 +73,4 @@ linters:
- grouper
- decorder
- nonamedreturns
- nosnakecase
fast: false
42 changes: 37 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ func ExampleGetField() {
if err != nil {
log.Fatal(err)
}

fmt.Println(value)

// output:
// first value
// third value
}
}

Expand Down Expand Up @@ -62,6 +67,10 @@ func ExampleGetFieldKind() {
log.Fatal(err)
}
fmt.Println(secondFieldKind)

// output:
// string
// int
}

func ExampleGetFieldType() {
Expand All @@ -88,6 +97,10 @@ func ExampleGetFieldType() {
log.Fatal(err)
}
fmt.Println(secondFieldType)

// output:
// string
// int
}

func ExampleGetFieldTag() {
Expand All @@ -104,6 +117,10 @@ func ExampleGetFieldTag() {
log.Fatal(err)
}
fmt.Println(tag)

// output:
// first tag
// third tag
}

func ExampleHasField() {
Expand All @@ -120,6 +137,10 @@ func ExampleHasField() {
// has == false
has, _ = reflections.HasField(s, "FourthField")
fmt.Println(has)

// output:
// true
// false
}

func ExampleFields() {
Expand All @@ -136,6 +157,9 @@ func ExampleFields() {
// []string{"FirstField", "SecondField", "ThirdField"}
fields, _ = reflections.Fields(s)
fmt.Println(fields)

// output:
// [MyEmbeddedStruct FirstField SecondField ThirdField]
}

func ExampleItems() {
Expand All @@ -151,6 +175,9 @@ func ExampleItems() {
// field value map
structItems, _ = reflections.Items(s)
fmt.Println(structItems)

// output:
// map[FirstField:first value MyEmbeddedStruct:{} SecondField:2 ThirdField:third value]
}

func ExampleItemsDeep() {
Expand All @@ -170,6 +197,9 @@ func ExampleItemsDeep() {
// anonymous embedded structs
structItems, _ = reflections.ItemsDeep(s)
fmt.Println(structItems)

// output:
// map[EmbeddedField:embedded value FirstField:first value SecondField:2 ThirdField:third value]
}

func ExampleTags() {
Expand All @@ -191,6 +221,9 @@ func ExampleTags() {
// }
structTags, _ = reflections.Tags(s, "matched")
fmt.Println(structTags)

// output:
// map[FirstField:first tag MyEmbeddedStruct: SecondField:second tag ThirdField:]
}

func ExampleSetField() {
Expand All @@ -207,12 +240,11 @@ func ExampleSetField() {
log.Fatal(err)
}

// If you try to set a field's value using the wrong type,
// Note that if you try to set a field's value using the wrong type,
// an error will be returned
err = reflections.SetField(&s, "FirstField", 123) // err != nil
if err != nil {
log.Fatal(err)
}
_ = reflections.SetField(&s, "FirstField", 123) // err != nil

// output:
}

func ExampleGetFieldNameByTagValue() {
Expand Down
12 changes: 6 additions & 6 deletions reflections.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func GetFieldNameByTagValue(obj interface{}, tagKey, tagValue string) (string, e
objType := objValue.Type()
fieldsCount := objType.NumField()

for i := 0; i < fieldsCount; i++ {
for i := range fieldsCount {
structField := objType.Field(i)
if structField.Tag.Get(tagKey) == tagValue {
return structField.Name, nil
Expand Down Expand Up @@ -142,7 +142,7 @@ func SetField(obj interface{}, name string, value interface{}) error {
structFieldType := structFieldValue.Type()
val := reflect.ValueOf(value)
if !val.Type().AssignableTo(structFieldType) {
invalidTypeError := fmt.Errorf("provided value type not assignable to obj field type")
invalidTypeError := errors.New("provided value type not assignable to obj field type")
return invalidTypeError
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func Fields(obj interface{}) ([]string, error) {

// FieldsDeep returns "flattened" fields.
//
// Note that FieldsDeept treats fields from anonymous inner structs as normal fields.
// Note that FieldsDeep treats fields from anonymous inner structs as normal fields.
func FieldsDeep(obj interface{}) ([]string, error) {
return fields(obj, true)
}
Expand All @@ -190,7 +190,7 @@ func fields(obj interface{}, deep bool) ([]string, error) {
fieldsCount := objType.NumField()

var allFields []string
for i := 0; i < fieldsCount; i++ {
for i := range fieldsCount {
field := objType.Field(i)
if isExportableField(field) {
if !deep || !field.Anonymous {
Expand Down Expand Up @@ -233,7 +233,7 @@ func items(obj interface{}, deep bool) (map[string]interface{}, error) {

allItems := make(map[string]interface{})

for i := 0; i < fieldsCount; i++ {
for i := range fieldsCount {
field := objType.Field(i)
fieldValue := objValue.Field(i)

Expand Down Expand Up @@ -281,7 +281,7 @@ func tags(obj interface{}, key string, deep bool) (map[string]string, error) {

allTags := make(map[string]string)

for i := 0; i < fieldsCount; i++ {
for i := range fieldsCount {
structField := objType.Field(i)
if isExportableField(structField) {
if !deep || !structField.Anonymous {
Expand Down
Loading

0 comments on commit da2d723

Please sign in to comment.