fix: Enable interface field access in rule expressions #484
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Enable Interface Field Access in Rule Expressions
Problem Summary
The Grule Rule Engine cannot access fields on
interface{}
types in rule expressions, causing runtime errors when trying to navigate nested data structures.Previously, rules like this would fail if
Data.Payload
has typeany | interface{}
:Root Cause
In
/model/GoDataAccessLayer.go
, theIsObject()
method only recognizedreflect.Struct
andreflect.Ptr
types as objects, but notreflect.Interface
types that contain structs. This caused nested field access likeStruct.InterfaceField.NestedField
to fail whenInterfaceField
is aninterface{}
field.Changes Made
Core Implementation
IsObject()
method: Checks if interface contains struct and extracts concrete valueGetObjectValueByField()
method: Handles interface types by extracting concrete valuesSetObjectValueByField()
method: Supports setting values in interface fields for addressable typesTest Coverage
GoDataAccessLayer_test.go
for interface field accessexamples/interface_field_access_test.go
demonstrating real-world usageBenefits
✅ Enable direct field access on interface{} types in rules
✅ Maintain backward compatibility with existing functionality
✅ Simplify rule writing for complex data structures
✅ Natural rule syntax that's intuitive for developers
Example Usage
After this fix, users can write natural rules:
Testing
All existing tests pass, and new tests verify:
Backwards Compatibility
This change is fully backwards compatible. All existing code continues to work unchanged, and the enhancement only adds new capabilities for interface field access.
Files Modified:
model/GoDataAccessLayer.go
- Core implementationmodel/GoDataAccessLayer_test.go
- Unit testsexamples/interface_field_access_test.go
- Integration tests (new file)