Skip to content
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
9 changes: 5 additions & 4 deletions structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package structs
import (
"errors"
"reflect"
"strings"
)

// CallbackFunc on the struct field
Expand Down Expand Up @@ -58,18 +59,18 @@ func FilterStruct[T any](input T, includeFields, excludeFields []string) (T, err
excludeMap := make(map[string]bool)

for _, field := range includeFields {
includeMap[field] = true
includeMap[strings.ToLower(field)] = true
}
for _, field := range excludeFields {
excludeMap[field] = true
excludeMap[strings.ToLower(field)] = true
}

typeOfStruct := val.Type()
filteredStruct := reflect.New(typeOfStruct).Elem()

for i := 0; i < val.NumField(); i++ {
field := typeOfStruct.Field(i)
fieldName := field.Name
fieldName := strings.ToLower(field.Name)
fieldValue := val.Field(i)

if (len(includeMap) == 0 || includeMap[fieldName]) && !excludeMap[fieldName] {
Expand All @@ -96,7 +97,7 @@ func GetStructFields[T any](input T) ([]string, error) {
fields := make([]string, 0, val.NumField())
typeOfStruct := val.Type()
for i := 0; i < val.NumField(); i++ {
fields = append(fields, typeOfStruct.Field(i).Name)
fields = append(fields, strings.ToLower(typeOfStruct.Field(i).Name))
}

return fields, nil
Expand Down
6 changes: 3 additions & 3 deletions structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestFilterStruct(t *testing.T) {
{
name: "include specific fields",
input: s,
includeFields: []string{"Name", "Age"},
includeFields: []string{"name", "Age"},
excludeFields: []string{},
want: TestStruct{
Name: "John",
Expand All @@ -46,7 +46,7 @@ func TestFilterStruct(t *testing.T) {
name: "exclude specific fields",
input: s,
includeFields: []string{},
excludeFields: []string{"Address"},
excludeFields: []string{"address"},
want: TestStruct{
Name: "John",
Age: 30,
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestGetStructFields(t *testing.T) {
{
name: "valid struct",
input: s,
want: []string{"Name", "Age", "Address"},
want: []string{"name", "age", "address"},
wantErr: false,
},
{
Expand Down
Loading