diff --git a/test/match.go b/test/match.go index 043437f..78c22e4 100644 --- a/test/match.go +++ b/test/match.go @@ -51,6 +51,7 @@ type baseslice interface { } type TySet[T baseelem] []T +type TyAnySet []any func Set__0[T baseelem](vals ...T) TySet[T] { return TySet[T](vals) @@ -60,6 +61,10 @@ func Set__1[T []string](v *Var__3[T]) TySet[string] { return TySet[string](v.Val()) } +func Set__2(vals ...any) TyAnySet { + return TyAnySet(vals) +} + // ----------------------------------------------------------------------------- type Case struct { @@ -74,7 +79,7 @@ func nameCtx(name []string) string { } const ( - Gopo_Gopt_Case_Match = "Gopt_Case_MatchTBase,Gopt_Case_MatchMap,Gopt_Case_MatchSlice,Gopt_Case_MatchBaseSlice,Gopt_Case_MatchSet,Gopt_Case_MatchAny" + Gopo_Gopt_Case_Match = "Gopt_Case_MatchTBase,Gopt_Case_MatchMap,Gopt_Case_MatchSlice,Gopt_Case_MatchBaseSlice,Gopt_Case_MatchSet,Gopt_Case_MatchAnySet,Gopt_Case_MatchAny" ) func Gopt_Case_MatchTBase[T basetype](t CaseT, expected, got T, name ...string) { @@ -135,7 +140,44 @@ func Gopt_Case_MatchSet[T baseelem](t CaseT, expected TySet[T], got []T, name .. } } +func Gopt_Case_MatchAnySet(t CaseT, expected TyAnySet, got any, name ...string) { + if gv, ok := got.([]any); ok { + matchAnySet(t, expected, gv) + return + } + vgot := reflect.ValueOf(got) + if vgot.Kind() != reflect.Slice { + t.Fatalf("unmatched set%s: expected: %v, got a non slice value: %v\n", nameCtx(name), expected, got) + } + for i, n := 0, vgot.Len(); i < n; i++ { + gv := vgot.Index(i).Interface() + if !hasAnyElem(gv, expected) { + t.Fatalf("unmatched set%s: expected: %v, value %v doesn't exist in it\n", nameCtx(name), expected, gv) + } + } +} + +func matchAnySet(t CaseT, expected TyAnySet, got []any, name ...string) { + if len(expected) != len(got) { + t.Fatalf("unmatched set%s length - expected: %d, got: %d\n", nameCtx(name), len(expected), len(got)) + } + for _, gv := range got { + if !hasAnyElem(gv, expected) { + t.Fatalf("unmatched set%s: expected: %v, value %v doesn't exist in it\n", nameCtx(name), expected, gv) + } + } +} + func hasElem[T baseelem](v T, expected []T) bool { + for _, ev := range expected { + if reflect.DeepEqual(v, ev) { + return true + } + } + return false +} + +func hasAnyElem(v any, expected []any) bool { for _, ev := range expected { if v == ev { return true @@ -220,7 +262,15 @@ retry: Gopt_Case_MatchSet(t, ev, gv.Val(), name...) return } - return + case TyAnySet: + switch gv := got.(type) { + case *Var__2[[]any]: + Gopt_Case_MatchAnySet(t, ev, gv.Val(), name...) + return + default: + Gopt_Case_MatchAnySet(t, ev, gv, name...) + return + } case *Var__0[string]: switch gv := got.(type) { case string: diff --git a/ydb/demo/foo/article_ydb.gox b/ydb/demo/foo/article_ydb.gox index eb8d583..4a24493 100644 --- a/ydb/demo/foo/article_ydb.gox +++ b/ydb/demo/foo/article_ydb.gox @@ -109,6 +109,11 @@ class "Articles", => { tags doc1.Id ret set("tag1", "tag3"), nil + setTags doc2.Id, "tag1", "tag5" + setTags doc3.Id, "tag1", "tag3" + setTags doc4.Id, "tag2", "tag3" + setTags doc5.Id, "tag5", "tag3" + listByTag := api("listByTag", func(tag string) (result []ArticleEntry) { var ids []string query "tag.name=?", tag @@ -118,12 +123,19 @@ class "Articles", => { ret &result return }) - _ = listByTag + + listByTag "tag1" + ret set(doc3.ArticleEntry, doc1.ArticleEntry, doc2.ArticleEntry) + + listByTag "tag3" + ret set(doc3.ArticleEntry, doc4.ArticleEntry, doc1.ArticleEntry, doc5.ArticleEntry) listByAuthor := api("listByAuthor", func(author string) (result []ArticleEntry) { query "author=?", author ret &result return }) - _ = listByAuthor + + listByAuthor "eft" + ret set(doc2.ArticleEntry, doc3.ArticleEntry) } diff --git a/ydb/demo/foo/gop_autogen.go b/ydb/demo/foo/gop_autogen.go index 13fd210..2046ad9 100644 --- a/ydb/demo/foo/gop_autogen.go +++ b/ydb/demo/foo/gop_autogen.go @@ -188,33 +188,49 @@ func (this *article) Main() { //line ydb/demo/foo/article_ydb.gox:110:1 this.Ret__1(test.Set__0("tag1", "tag3"), nil) //line ydb/demo/foo/article_ydb.gox:112:1 - listByTag := this.Api("listByTag", func(tag string) (result []ArticleEntry) { + setTags(doc2.Id, "tag1", "tag5") //line ydb/demo/foo/article_ydb.gox:113:1 - var ids []string + setTags(doc3.Id, "tag1", "tag3") //line ydb/demo/foo/article_ydb.gox:114:1 - this.Query__1("tag.name=?", tag) + setTags(doc4.Id, "tag2", "tag3") //line ydb/demo/foo/article_ydb.gox:115:1 - this.Ret__1("tag.doc", &ids) + setTags(doc5.Id, "tag5", "tag3") //line ydb/demo/foo/article_ydb.gox:117:1 - this.Query__1("id=?", ids) + listByTag := this.Api("listByTag", func(tag string) (result []ArticleEntry) { //line ydb/demo/foo/article_ydb.gox:118:1 - this.Ret__1(&result) + var ids []string //line ydb/demo/foo/article_ydb.gox:119:1 + this.Query__1("tag.name=?", tag) +//line ydb/demo/foo/article_ydb.gox:120:1 + this.Ret__1("tag.doc", &ids) +//line ydb/demo/foo/article_ydb.gox:122:1 + this.Query__1("id=?", ids) +//line ydb/demo/foo/article_ydb.gox:123:1 + this.Ret__1(&result) +//line ydb/demo/foo/article_ydb.gox:124:1 return }) -//line ydb/demo/foo/article_ydb.gox:121:1 - _ = listByTag -//line ydb/demo/foo/article_ydb.gox:123:1 +//line ydb/demo/foo/article_ydb.gox:127:1 + listByTag("tag1") +//line ydb/demo/foo/article_ydb.gox:128:1 + this.Ret__1(test.Set__2(doc3.ArticleEntry, doc1.ArticleEntry, doc2.ArticleEntry)) +//line ydb/demo/foo/article_ydb.gox:130:1 + listByTag("tag3") +//line ydb/demo/foo/article_ydb.gox:131:1 + this.Ret__1(test.Set__2(doc3.ArticleEntry, doc4.ArticleEntry, doc1.ArticleEntry, doc5.ArticleEntry)) +//line ydb/demo/foo/article_ydb.gox:133:1 listByAuthor := this.Api("listByAuthor", func(author string) (result []ArticleEntry) { -//line ydb/demo/foo/article_ydb.gox:124:1 +//line ydb/demo/foo/article_ydb.gox:134:1 this.Query__1("author=?", author) -//line ydb/demo/foo/article_ydb.gox:125:1 +//line ydb/demo/foo/article_ydb.gox:135:1 this.Ret__1(&result) -//line ydb/demo/foo/article_ydb.gox:126:1 +//line ydb/demo/foo/article_ydb.gox:136:1 return }) -//line ydb/demo/foo/article_ydb.gox:128:1 - _ = listByAuthor +//line ydb/demo/foo/article_ydb.gox:139:1 + listByAuthor("eft") +//line ydb/demo/foo/article_ydb.gox:140:1 + this.Ret__1(test.Set__2(doc2.ArticleEntry, doc3.ArticleEntry)) }) }