checker API

checker

package

API reference for the checker package.

F
function

IsMatch

IsMatch checks if the value of a field matches the user ID.

Parameters

userID
string

Returns

bool
core/guard/checker/matcher.go:9-42
func IsMatch(val reflect.Value, userID string) bool

{
	switch val.Kind() {
	case reflect.Slice, reflect.Array:
		for i := 0; i < val.Len(); i++ {
			if IsMatch(val.Index(i), userID) {
				return true
			}
		}
		return false
	case reflect.Map:
		for _, key := range val.MapKeys() {
			if IsMatch(key, userID) {
				return true
			}
		}
		return false
	case reflect.String:
		return val.String() == userID
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return fmt.Sprintf("%d", val.Int()) == userID
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return fmt.Sprintf("%d", val.Uint()) == userID
	case reflect.Ptr, reflect.Interface:
		if val.IsNil() {
			return false
		}
		return IsMatch(val.Elem(), userID)
	default:
		if s, ok := val.Interface().(fmt.Stringer); ok {
			return s.String() == userID
		}
		return fmt.Sprintf("%v", val.Interface()) == userID
	}
}
T
type

stringID

core/guard/checker/matcher_test.go:9-9
type stringID string
F
function

TestIsMatchScalarValues

Parameters

core/guard/checker/matcher_test.go:15-36
func TestIsMatchScalarValues(t *testing.T)

{
	tests := []struct {
		name string
		val  any
		id   string
		want bool
	}{
		{"string", "42", "42", true},
		{"int", int64(42), "42", true},
		{"uint", uint(42), "42", true},
		{"stringer", stringID("42"), "42", true},
		{"fallback", fmt.Errorf("42"), "42", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := IsMatch(reflect.ValueOf(tt.val), tt.id); got != tt.want {
				t.Fatalf("IsMatch() = %v, want %v", got, tt.want)
			}
		})
	}
}
F
function

TestIsMatchCollectionsAndPointers

Parameters

core/guard/checker/matcher_test.go:38-59
func TestIsMatchCollectionsAndPointers(t *testing.T)

{
	value := "42"
	tests := []struct {
		name string
		val  any
		want bool
	}{
		{"slice", []string{"1", "42"}, true},
		{"array", [2]int{1, 42}, true},
		{"map key", map[string]string{"42": "owner"}, true},
		{"pointer", &value, true},
		{"missing", []string{"1", "2"}, false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := IsMatch(reflect.ValueOf(tt.val), "42"); got != tt.want {
				t.Fatalf("IsMatch() = %v, want %v", got, tt.want)
			}
		})
	}
}
F
function

TestIsMatchNilPointer

Parameters

core/guard/checker/matcher_test.go:61-66
func TestIsMatchNilPointer(t *testing.T)

{
	var value *string
	if IsMatch(reflect.ValueOf(value), "42") {
		t.Fatal("IsMatch() = true, want false")
	}
}