Refactored methods into pure functions.

This commit is contained in:
Jay
2025-10-31 19:48:56 -04:00
parent 67db088981
commit 4df91938ef
9 changed files with 42 additions and 42 deletions

View File

@@ -29,7 +29,7 @@ type Filter struct {
// MarshalJSON converts the filter to JSON with standard fields, tag filters
// (prefixed with "#"), and extensions merged into a single object.
func (f *Filter) MarshalJSON() ([]byte, error) {
func MarshalJSON(f Filter) ([]byte, error) {
outputMap := make(map[string]interface{})
// Add standard fields
@@ -86,7 +86,7 @@ func (f *Filter) MarshalJSON() ([]byte, error) {
// UnmarshalJSON parses JSON into the filter, separating standard fields,
// tag filters (keys starting with "#"), and extensions.
func (f *Filter) UnmarshalJSON(data []byte) error {
func UnmarshalJSON(data []byte, f *Filter) error {
// Decode into raw map
raw := make(FilterExtensions)
if err := json.Unmarshal(data, &raw); err != nil {
@@ -182,7 +182,7 @@ func (f *Filter) UnmarshalJSON(data []byte) error {
// Matches returns true if the event satisfies all filter conditions.
// Supports prefix matching for IDs and authors, and tag filtering.
// Does not account for custom extensions.
func (f *Filter) Matches(event *events.Event) bool {
func Matches(f Filter, event events.Event) bool {
// Check ID
if len(f.IDs) > 0 {
if !matchesPrefix(event.ID, f.IDs) {

View File

@@ -584,7 +584,7 @@ var roundTripTestCases = []FilterRoundTripTestCase{
func TestFilterMarshalJSON(t *testing.T) {
for _, tc := range marshalTestCases {
t.Run(tc.name, func(t *testing.T) {
result, err := tc.filter.MarshalJSON()
result, err := MarshalJSON(tc.filter)
assert.NoError(t, err)
var expectedMap, actualMap map[string]interface{}
@@ -602,7 +602,7 @@ func TestFilterUnmarshalJSON(t *testing.T) {
for _, tc := range unmarshalTestCases {
t.Run(tc.name, func(t *testing.T) {
var result Filter
err := result.UnmarshalJSON([]byte(tc.input))
err := UnmarshalJSON([]byte(tc.input), &result)
assert.NoError(t, err)
expectEqualFilters(t, result, tc.expected)
@@ -613,11 +613,11 @@ func TestFilterUnmarshalJSON(t *testing.T) {
func TestFilterRoundTrip(t *testing.T) {
for _, tc := range roundTripTestCases {
t.Run(tc.name, func(t *testing.T) {
jsonBytes, err := tc.filter.MarshalJSON()
jsonBytes, err := MarshalJSON(tc.filter)
assert.NoError(t, err)
var result Filter
err = result.UnmarshalJSON(jsonBytes)
err = UnmarshalJSON(jsonBytes, &result)
assert.NoError(t, err)
expectEqualFilters(t, result, tc.filter)

View File

@@ -391,7 +391,7 @@ func TestEventFilterMatching(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
actualIDs := []string{}
for _, event := range testEvents {
if tc.filter.Matches(&event) {
if Matches(tc.filter, event) {
actualIDs = append(actualIDs, event.ID[:8])
}
}
@@ -416,5 +416,5 @@ func TestEventFilterMatchingSkipMalformedTags(t *testing.T) {
},
}
assert.True(t, filter.Matches(&event))
assert.True(t, Matches(filter, event))
}