Skip to content
Merged
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 @@ -43,14 +43,15 @@ func Walk(s interface{}, callback CallbackFunc) {
// - input: the original struct.
// - includeFields: list of fields to include (if empty, includes all).
// - excludeFields: list of fields to exclude (processed after include).
func FilterStruct(input interface{}, includeFields, excludeFields []string) (interface{}, error) {
func FilterStruct[T any](input T, includeFields, excludeFields []string) (T, error) {
var zeroValue T
val := reflect.ValueOf(input)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}

if val.Kind() != reflect.Struct {
return nil, errors.New("input must be a struct")
return zeroValue, errors.New("input must be a struct")
}

includeMap := make(map[string]bool)
Expand All @@ -76,13 +77,13 @@ func FilterStruct(input interface{}, includeFields, excludeFields []string) (int
}
}

return filteredStruct.Interface(), nil
return filteredStruct.Interface().(T), nil
}

// GetStructFields returns all the top-level field names from the given struct.
// - input: the original struct.
// Returns a slice of field names or an error if the input is not a struct.
func GetStructFields(input interface{}) ([]string, error) {
func GetStructFields[T any](input T) ([]string, error) {
val := reflect.ValueOf(input)
if val.Kind() == reflect.Ptr {
val = val.Elem()
Expand Down
Loading