Merge pull request 'refactor with sugar-n-spice' (#9) from SugarNSpice into main

Reviewed-on: https://git.infra.nkode.tech/dkelly/go-nkode/pulls/9
This commit is contained in:
dkelly
2024-12-29 20:06:20 +00:00
11 changed files with 30 additions and 134 deletions

7
go.mod
View File

@@ -1,8 +1,6 @@
module go-nkode
go 1.22.0
toolchain go1.23.0
go 1.23.0
require (
github.com/aws/aws-sdk-go-v2 v1.31.0
@@ -12,7 +10,7 @@ require (
github.com/google/uuid v1.6.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.4
github.com/swaggo/swag/example/celler v0.0.0-20241025062444-99698582709d
@@ -20,6 +18,7 @@ require (
)
require (
github.com/DonovanKelly/sugar-n-spice v1.0.1 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.35 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14 // indirect

4
go.sum
View File

@@ -1,3 +1,5 @@
github.com/DonovanKelly/sugar-n-spice v1.0.1 h1:VsybiCHSziAqyPtbYF6GtkiJYYECWMHKN+EyEa6UVpA=
github.com/DonovanKelly/sugar-n-spice v1.0.1/go.mod h1:/HQWoablLFCwsa4gwfzVBu80cI5A3dyO1uCiB11sup0=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/aws/aws-sdk-go-v2 v1.31.0 h1:3V05LbxTSItI5kUqNwhJrrrY1BAXxXt0sN0l72QmG5U=
@@ -116,6 +118,8 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE=
github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg=
github.com/swaggo/http-swagger v1.3.4 h1:q7t/XLx0n15H1Q9/tk3Y9L4n210XzJF5WtnDX64a5ww=

View File

@@ -1,6 +1,7 @@
package entities
import (
"github.com/DonovanKelly/sugar-n-spice/set"
"github.com/google/uuid"
"go-nkode/config"
"go-nkode/internal/models"
@@ -38,8 +39,8 @@ func (c *Customer) IsValidNKode(kp KeypadDimension, passcodeAttrIdx []int) error
if validIdx := kp.ValidateAttributeIndices(passcodeAttrIdx); !validIdx {
return config.ErrInvalidNKodeIdx
}
passcodeSetVals := make(utils.Set[uint64])
passcodeAttrVals := make(utils.Set[uint64])
passcodeSetVals := make(set.Set[uint64])
passcodeAttrVals := make(set.Set[uint64])
attrVals, err := c.Attributes.AttrValsForKp(kp)
if err != nil {
return err

View File

@@ -1,8 +1,8 @@
package entities
import (
"github.com/DonovanKelly/sugar-n-spice/all"
"go-nkode/config"
py "go-nkode/internal/utils"
)
type KeypadDimension struct {
@@ -26,13 +26,13 @@ func (kp *KeypadDimension) IsValidKeypadDimension() error {
}
func (kp *KeypadDimension) ValidKeySelections(selectedKeys []int) bool {
return py.All[int](selectedKeys, func(idx int) bool {
return all.All[int](selectedKeys, func(idx int) bool {
return 0 <= idx && idx < kp.NumbOfKeys
})
}
func (kp *KeypadDimension) ValidateAttributeIndices(attrIndicies []int) bool {
return py.All[int](attrIndicies, func(i int) bool {
return all.All[int](attrIndicies, func(i int) bool {
return i >= 0 && i < kp.TotalAttrs()
})
}

View File

@@ -1,10 +1,10 @@
package entities
import (
"github.com/DonovanKelly/sugar-n-spice/set"
"go-nkode/config"
"go-nkode/internal/models"
"go-nkode/internal/security"
"go-nkode/internal/utils"
"log"
)
@@ -122,15 +122,15 @@ func (u *UserInterface) randomAttributeRotation() error {
return nil
}
func (u *UserInterface) AttributeAdjacencyGraph() (map[int]utils.Set[int], error) {
func (u *UserInterface) AttributeAdjacencyGraph() (map[int]set.Set[int], error) {
interfaceKeypad, err := u.InterfaceMatrix()
if err != nil {
return nil, err
}
graph := make(map[int]utils.Set[int])
graph := make(map[int]set.Set[int])
for _, key := range interfaceKeypad {
keySet := utils.NewSetFromSlice(key)
keySet := set.NewSetFromSlice(key)
for _, attr := range key {
attrAdjacency := keySet.Copy()
attrAdjacency.Remove(attr)
@@ -154,7 +154,7 @@ func (u *UserInterface) LoginShuffle() error {
if err != nil {
return err
}
selectedSets := utils.NewSetFromSlice[int](setIdxs[:numbOfSelectedSets])
selectedSets := set.NewSetFromSlice[int](setIdxs[:numbOfSelectedSets])
for keyIdx, key := range keypadSet1 {
for idx := range key {

View File

@@ -1,11 +1,12 @@
package entities
import (
"github.com/DonovanKelly/sugar-n-spice/all"
"github.com/DonovanKelly/sugar-n-spice/set"
"github.com/google/uuid"
"go-nkode/config"
"go-nkode/internal/models"
"go-nkode/internal/security"
py "go-nkode/internal/utils"
"log"
"sort"
)
@@ -50,7 +51,7 @@ func NewSignupResetSession(userEmail models.UserEmail, kp KeypadDimension, custo
}
func (s *UserSignSession) DeducePasscode(confirmKeyEntry models.KeySelection) ([]int, error) {
validEntry := py.All[int](confirmKeyEntry, func(i int) bool {
validEntry := all.All[int](confirmKeyEntry, func(i int) bool {
return 0 <= i && i < s.Kp.NumbOfKeys
})
@@ -93,8 +94,8 @@ func (s *UserSignSession) DeducePasscode(confirmKeyEntry models.KeySelection) ([
passcode := make([]int, passcodeLen)
for idx := 0; idx < passcodeLen; idx++ {
setKey := py.NewSetFromSlice[int](setKeyVals[idx])
confirmKey := py.NewSetFromSlice[int](confirmKeyVals[idx])
setKey := set.NewSetFromSlice[int](setKeyVals[idx])
confirmKey := set.NewSetFromSlice[int](confirmKeyVals[idx])
intersection := setKey.Intersect(confirmKey)
if intersection.Size() < 1 {
log.Printf("set and confirm do not intersect at index %d", idx)
@@ -111,7 +112,7 @@ func (s *UserSignSession) DeducePasscode(confirmKeyEntry models.KeySelection) ([
}
func (s *UserSignSession) SetUserNKode(keySelection models.KeySelection) (models.IdxInterface, error) {
validKeySelection := py.All[int](keySelection, func(i int) bool {
validKeySelection := all.All[int](keySelection, func(i int) bool {
return 0 <= i && i < s.Kp.NumbOfKeys
})
if !validKeySelection {

View File

@@ -1,9 +1,9 @@
package entities
import (
"github.com/DonovanKelly/sugar-n-spice/all"
"github.com/stretchr/testify/assert"
"go-nkode/internal/models"
py "go-nkode/internal/utils"
"testing"
)
@@ -120,12 +120,12 @@ func TestUserInterface_PartialInterfaceShuffle(t *testing.T) {
shuffleCompare[idx] = val == postShuffle[idx]
}
allTrue := py.All[bool](shuffleCompare, func(n bool) bool {
allTrue := all.All[bool](shuffleCompare, func(n bool) bool {
return n == true
})
assert.False(t, allTrue)
allFalse := py.All[bool](shuffleCompare, func(n bool) bool {
allFalse := all.All[bool](shuffleCompare, func(n bool) bool {
return n == false
})

View File

@@ -6,7 +6,7 @@ import (
"encoding/binary"
"encoding/hex"
"errors"
"go-nkode/internal/utils"
"github.com/DonovanKelly/sugar-n-spice/set"
"log"
"math/big"
r "math/rand"
@@ -84,7 +84,7 @@ func GenerateRandomNonRepeatingUint64(listLen int) ([]uint64, error) {
if listLen > int(1)<<32 {
return nil, ErrRandNonRepeatingUint64
}
listSet := make(utils.Set[uint64])
listSet := make(set.Set[uint64])
for {
if listSet.Size() == listLen {
break
@@ -104,7 +104,7 @@ func GenerateRandomNonRepeatingInt(listLen int) ([]int, error) {
if listLen > int(1)<<31 {
return nil, ErrRandNonRepeatingInt
}
listSet := make(utils.Set[int])
listSet := make(set.Set[int])
for {
if listSet.Size() == listLen {
break

View File

@@ -1,63 +0,0 @@
package utils
type Set[T comparable] map[T]struct{}
func (s *Set[T]) Add(element T) {
(*s)[element] = struct{}{}
}
func (s *Set[T]) Remove(element T) {
delete(*s, element)
}
func (s *Set[T]) Contains(element T) bool {
_, exists := (*s)[element]
return exists
}
func (s *Set[T]) Size() int {
return len(*s)
}
func (s *Set[T]) ToSlice() []T {
list := make([]T, 0, len(*s))
for key := range *s {
list = append(list, key)
}
return list
}
func NewSetFromSlice[T comparable](slice []T) Set[T] {
set := make(Set[T])
for _, val := range slice {
set.Add(val)
}
return set
}
func (s *Set[T]) Copy() Set[T] {
newSet := make(Set[T])
for key, val := range *s {
newSet[key] = val
}
return newSet
}
func (s *Set[T]) IsDisjoint(otherSet Set[T]) bool {
for attr := range *s {
if otherSet.Contains(attr) {
return false
}
}
return true
}
func (s *Set[T]) Intersect(otherSet Set[T]) Set[T] {
intersect := make(Set[T])
for val := range *s {
if otherSet.Contains(val) {
intersect.Add(val)
}
}
return intersect
}

View File

@@ -1,35 +0,0 @@
package utils
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestSet(t *testing.T) {
intSet := make(Set[int])
intSet.Add(1)
intSet.Add(2)
assert.EqualValues(t, intSet.Size(), 2)
intSet.Add(3)
intSet.Add(3)
assert.EqualValues(t, intSet.Size(), 3)
intSet.Remove(2)
assert.EqualValues(t, intSet.Size(), 2)
assert.False(t, intSet.Contains(2))
assert.True(t, intSet.Contains(1))
list := intSet.ToSlice()
assert.Contains(t, list, 1)
assert.Contains(t, list, 3)
}
func TestSet_Copy(t *testing.T) {
intSet := NewSetFromSlice[int]([]int{1, 2, 3})
copySet := intSet.Copy()
intSet.Remove(1)
assert.Equal(t, intSet.Size(), 2)
assert.Equal(t, copySet.Size(), 3)
}

View File

@@ -1,11 +0,0 @@
package utils
func All[T comparable](slice []T, condition func(T) bool) bool {
for _, v := range slice {
if !condition(v) {
return false
}
}
return true
}