Skip to content

Commit

Permalink
lorm.Set
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfsdhgjkbmnmxc committed Feb 20, 2022
1 parent 036d8cd commit af96f20
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (

func UUID() string { return gouuidv6.New().String() }

func DoPks[PK, R interface{ Pk() PK }](l []R) []PK {
pks := make([]PK, len(l))
for i, r := range l {
pks[i] = r.Pk()
}
return pks
}

func DoCount(f Filter, table op.Table) int {
var res int
if !f.IsEmpty() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/tada-team/lorm

go 1.14
go 1.18

require (
github.com/bradleypeabody/gouuidv6 v0.0.0-20210725191436-99413e4b686d
Expand Down
20 changes: 20 additions & 0 deletions pk_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lorm

type iPK interface {
int | int64 | string
}

type Set[PK iPK] map[PK]struct{}

func (s Set[PK]) Add(pk PK) { s[pk] = struct{}{} }

func (s Set[PK]) Contains(pk PK) bool { _, ok := s[pk]; return ok }

func (s Set[PK]) AsList() []PK {
//pks := make([]PK, 0, len(s)) <-- len(s) doesn't work yet
pks := make([]PK, 0)
for pk := range s {
pks = append(pks, pk)
}
return pks
}

0 comments on commit af96f20

Please sign in to comment.