Skip to content

Commit

Permalink
Add IsStruct() to check if a value is a struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Sep 24, 2023
1 parent 69c62cd commit 7c55a9f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func SetStructFieldValue(ctx context.Context, st any, name string, value any) (e
return wrapError(ErrStructFieldNameUndefined, name)
}

// IsStruct returns true if the value is a struct
func IsStruct(v any) bool {
return v != nil && reflect.TypeOf(v).Kind() == reflect.Struct
}

///////////////////////////////////////////////////////////////////////////////
/// MARK: Helpers
///////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ func TestStructFieldTags(t *testing.T) {
"ignore": "ignore"}, fields)
}

func TestIsStruct(t *testing.T) {
assert.True(t, IsStruct(testStruct{}))
assert.False(t, IsStruct(1))
}

func BenchmarkGetSetFieldValue(b *testing.B) {
st := &struct{ Name string }{}
ctx := context.TODO()
Expand Down

0 comments on commit 7c55a9f

Please sign in to comment.