Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual branch #23

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ^1.13
go-version: '^1.13'

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Build
run: go build -v .
Expand All @@ -27,4 +27,4 @@ jobs:
run: go test -v .

- name: Lint
uses: golangci/golangci-lint-action@v3.2.0
uses: golangci/golangci-lint-action@v6.1.0
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
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/oleiade/reflections

go 1.15
go 1.22.6

require github.com/stretchr/testify v1.6.1
require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 6 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
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
Loading