fix: Enable interface field access in rule expressions#484
Merged
newm4n merged 1 commit intohyperjumptech:masterfrom Jul 1, 2025
Merged
Conversation
- Enhanced IsObject() method to recognize interfaces containing structs
- Updated GetObjectValueByField() to handle interface types
- Updated SetObjectValueByField() to support interface field modification
- Added comprehensive tests for interface field access
- Enables direct field access like Data.Payload.Status in rules
- Maintains backward compatibility with existing functionality
Fixes issue where interface{} fields could not be accessed directly
in rule expressions, requiring workaround accessor methods.
newm4n
approved these changes
Jul 1, 2025
Collaborator
newm4n
left a comment
There was a problem hiding this comment.
Thanks a lot for the contribution. LGTM
Collaborator
|
IMHO, this is part of the core improvement. I will make a new release because of this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.Payloadhas typeany | interface{}:Root Cause
In
/model/GoDataAccessLayer.go, theIsObject()method only recognizedreflect.Structandreflect.Ptrtypes as objects, but notreflect.Interfacetypes that contain structs. This caused nested field access likeStruct.InterfaceField.NestedFieldto fail whenInterfaceFieldis 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.gofor interface field accessexamples/interface_field_access_test.godemonstrating 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)