feat(reflection): add ExtractTagValue and GetRelationshipInfo functions

* Implement ExtractTagValue to handle struct tag parsing.
* Introduce GetRelationshipInfo for extracting relationship metadata.
* Update tests to validate new functionality.
* Refactor related code for improved clarity and maintainability.
This commit is contained in:
Hein
2026-01-07 11:54:12 +02:00
parent e220ab3d34
commit bf7125efc3
8 changed files with 437 additions and 273 deletions

View File

@@ -2,6 +2,8 @@ package restheadspec
import (
"testing"
"github.com/bitechdev/ResolveSpec/pkg/common"
)
func TestParseModelName(t *testing.T) {
@@ -112,3 +114,88 @@ func TestNewStandardBunRouter(t *testing.T) {
t.Error("Expected router to be created, got nil")
}
}
func TestExtractTagValue(t *testing.T) {
tests := []struct {
name string
tag string
key string
expected string
}{
{
name: "Extract existing key",
tag: "json:name;validate:required",
key: "json",
expected: "name",
},
{
name: "Extract key with spaces",
tag: "json:name ; validate:required",
key: "validate",
expected: "required",
},
{
name: "Extract key at end",
tag: "json:name;validate:required;db:column_name",
key: "db",
expected: "column_name",
},
{
name: "Extract key at beginning",
tag: "primary:true;json:id;db:user_id",
key: "primary",
expected: "true",
},
{
name: "Key not found",
tag: "json:name;validate:required",
key: "db",
expected: "",
},
{
name: "Empty tag",
tag: "",
key: "json",
expected: "",
},
{
name: "Single key-value pair",
tag: "json:name",
key: "json",
expected: "name",
},
{
name: "Key with empty value",
tag: "json:;validate:required",
key: "json",
expected: "",
},
{
name: "Key with complex value",
tag: "json:user_name,omitempty;validate:required,min=3",
key: "json",
expected: "user_name,omitempty",
},
{
name: "Multiple semicolons",
tag: "json:name;;validate:required",
key: "validate",
expected: "required",
},
{
name: "BUN Tag",
tag: "rel:has-many,join:rid_hub=rid_hub_child",
key: "join",
expected: "rid_hub=rid_hub_child",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := common.ExtractTagValue(tt.tag, tt.key)
if result != tt.expected {
t.Errorf("ExtractTagValue(%q, %q) = %q; want %q", tt.tag, tt.key, result, tt.expected)
}
})
}
}