Skip to content

Commit

Permalink
Merge pull request #82 from xushiwei/ydb
Browse files Browse the repository at this point in the history
matchAnySet
  • Loading branch information
xushiwei authored Feb 13, 2024
2 parents e74e7f6 + b3e69dc commit dca35d9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 18 deletions.
54 changes: 52 additions & 2 deletions test/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 14 additions & 2 deletions ydb/demo/foo/article_ydb.gox
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
44 changes: 30 additions & 14 deletions ydb/demo/foo/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dca35d9

Please sign in to comment.