initial commit

This commit is contained in:
2024-08-17 10:26:02 -05:00
commit 7711fc14ed
17 changed files with 975 additions and 0 deletions

1
customer/customer.go Normal file
View File

@@ -0,0 +1 @@
package customer

View File

@@ -0,0 +1,70 @@
package customer
import (
"errors"
"fmt"
"go-nkode/models"
"go-nkode/util"
)
// TODO: make generic
type CustomerAttributes struct {
AttrVals []uint64
SetVals []uint64
keypadSize models.KeypadSize
}
func NewCustomerAttributes(keypadSize models.KeypadSize) (*CustomerAttributes, error) {
if keypadSize.IsDispersable() {
return nil, errors.New("number of keys must be less than the number of attributes per key to be dispersion resistant")
}
attrVals, errAttr := util.GenerateRandomNonRepeatingUint64(keypadSize.TotalAttrs())
if errAttr != nil {
return nil, errAttr
}
setVals, errSet := util.GenerateRandomNonRepeatingUint64(keypadSize.AttrsPerKey)
if errSet != nil {
return nil, errSet
}
customerAttrs := CustomerAttributes{
AttrVals: attrVals,
SetVals: setVals,
keypadSize: keypadSize,
}
return &customerAttrs, nil
}
func (c *CustomerAttributes) Renew() error {
attrVals, errAttr := util.GenerateRandomNonRepeatingUint64(c.keypadSize.TotalAttrs())
if errAttr != nil {
return errAttr
}
setVals, errSet := util.GenerateRandomNonRepeatingUint64(c.keypadSize.AttrsPerKey)
if errSet != nil {
return errSet
}
c.AttrVals = attrVals
c.SetVals = setVals
return nil
}
func (c *CustomerAttributes) IndexOfAttr(attrVal uint64) int {
// TODO: should this be mapped instead?
return util.IndexOf[uint64](c.AttrVals, attrVal)
}
func (c *CustomerAttributes) IndexOfSet(setVal uint64) int {
// TODO: should this be mapped instead?
return util.IndexOf[uint64](c.SetVals, setVal)
}
func (c *CustomerAttributes) GetAttrSetVal(attrVal uint64) (uint64, error) {
indexOfAttr := c.IndexOfAttr(attrVal)
if indexOfAttr == -1 {
return 0, errors.New(fmt.Sprintf("No attribute %d", attrVal))
}
setIdx := indexOfAttr % c.keypadSize.AttrsPerKey
return c.SetVals[setIdx], nil
}

View File

@@ -0,0 +1,13 @@
package customer
import (
"github.com/stretchr/testify/assert"
"go-nkode/models"
"testing"
)
func TestNewCustomerAttributes(t *testing.T) {
keypad := models.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 5}
_, nil := NewCustomerAttributes(keypad)
assert.NoError(t, nil)
}