functional simple api
This commit is contained in:
@@ -4,61 +4,61 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"go-nkode/hashset"
|
||||
py_builtin "go-nkode/py-builtin"
|
||||
py "go-nkode/py-builtin"
|
||||
"go-nkode/util"
|
||||
)
|
||||
|
||||
type UserSignSession struct {
|
||||
SessionId uuid.UUID
|
||||
CustomerId uuid.UUID
|
||||
LoginInterface UserInterface
|
||||
KeypadSize model.KeypadSize
|
||||
SetInterface []int
|
||||
ConfirmInterface []int
|
||||
SetKeyEntry []int
|
||||
Username string
|
||||
SessionId m.SessionId
|
||||
CustomerId m.CustomerId
|
||||
LoginUserInterface UserInterface
|
||||
Keypad m.KeypadSize
|
||||
SetIdxInterface m.IdxInterface
|
||||
ConfirmIdxInterface m.IdxInterface
|
||||
SetKeySelection m.KeySelection
|
||||
Username m.Username
|
||||
}
|
||||
|
||||
func NewSignupSession(keypadSize model.KeypadSize, customerId uuid.UUID) (*UserSignSession, error) {
|
||||
func NewSignupSession(keypadSize m.KeypadSize, customerId m.CustomerId) (*UserSignSession, error) {
|
||||
loginInterface, err := NewUserInterface(keypadSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
signupInter, err := signupInterface(*loginInterface)
|
||||
session := UserSignSession{
|
||||
SessionId: uuid.New(),
|
||||
CustomerId: customerId,
|
||||
LoginInterface: *loginInterface,
|
||||
SetInterface: signupInter.IdxInterface,
|
||||
ConfirmInterface: nil,
|
||||
SetKeyEntry: nil,
|
||||
Username: "",
|
||||
KeypadSize: signupInter.KeypadSize,
|
||||
SessionId: m.SessionId(uuid.New()),
|
||||
CustomerId: customerId,
|
||||
LoginUserInterface: *loginInterface,
|
||||
SetIdxInterface: signupInter.IdxInterface,
|
||||
ConfirmIdxInterface: nil,
|
||||
SetKeySelection: nil,
|
||||
Username: "",
|
||||
Keypad: signupInter.KeypadSize,
|
||||
}
|
||||
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
func (s *UserSignSession) DeducePasscode(confirmKeyEntry []int) ([]int, error) {
|
||||
validEntry := py_builtin.All[int](confirmKeyEntry, func(i int) bool {
|
||||
return 0 <= i && i < s.LoginInterface.KeypadSize.NumbOfKeys
|
||||
func (s *UserSignSession) DeducePasscode(confirmKeyEntry m.KeySelection) ([]int, error) {
|
||||
validEntry := py.All[int](confirmKeyEntry, func(i int) bool {
|
||||
return 0 <= i && i < s.LoginUserInterface.KeypadSize.NumbOfKeys
|
||||
})
|
||||
|
||||
if !validEntry {
|
||||
return nil, errors.New(fmt.Sprintf("Invalid Key entry. One or more key index: %#v, not in range 0-%d", confirmKeyEntry, s.LoginInterface.KeypadSize.NumbOfKeys))
|
||||
return nil, errors.New(fmt.Sprintf("Invalid Key entry. One or more key index: %#v, not in range 0-%d", confirmKeyEntry, s.LoginUserInterface.KeypadSize.NumbOfKeys))
|
||||
}
|
||||
|
||||
if s.SetInterface == nil {
|
||||
if s.SetIdxInterface == nil {
|
||||
return nil, errors.New("signup session set interface is nil")
|
||||
}
|
||||
|
||||
if s.ConfirmInterface == nil {
|
||||
if s.ConfirmIdxInterface == nil {
|
||||
return nil, errors.New("signup session confirm interface is nil")
|
||||
}
|
||||
|
||||
if s.SetKeyEntry == nil {
|
||||
if s.SetKeySelection == nil {
|
||||
return nil, errors.New("signup session set key entry is nil")
|
||||
}
|
||||
|
||||
@@ -66,16 +66,16 @@ func (s *UserSignSession) DeducePasscode(confirmKeyEntry []int) ([]int, error) {
|
||||
return nil, errors.New("signup session username is nil")
|
||||
}
|
||||
|
||||
if len(confirmKeyEntry) != len(s.SetKeyEntry) {
|
||||
return nil, errors.New(fmt.Sprintf("confirm and set key entry lenght mismatch %d != %d", len(confirmKeyEntry), len(s.SetKeyEntry)))
|
||||
if len(confirmKeyEntry) != len(s.SetKeySelection) {
|
||||
return nil, errors.New(fmt.Sprintf("confirm and set key entry lenght mismatch %d != %d", len(confirmKeyEntry), len(s.SetKeySelection)))
|
||||
}
|
||||
|
||||
passcodeLen := len(confirmKeyEntry)
|
||||
setKeyVals, err := s.getSelectedKeyVals(s.SetKeyEntry, s.SetInterface)
|
||||
setKeyVals, err := s.getSelectedKeyVals(s.SetKeySelection, s.SetIdxInterface)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
confirmKeyVals, err := s.getSelectedKeyVals(confirmKeyEntry, s.ConfirmInterface)
|
||||
confirmKeyVals, err := s.getSelectedKeyVals(confirmKeyEntry, s.ConfirmIdxInterface)
|
||||
passcode := make([]int, passcodeLen)
|
||||
|
||||
for idx := 0; idx < passcodeLen; idx++ {
|
||||
@@ -94,28 +94,28 @@ func (s *UserSignSession) DeducePasscode(confirmKeyEntry []int) ([]int, error) {
|
||||
return passcode, nil
|
||||
}
|
||||
|
||||
func (s *UserSignSession) SetUserNKode(username string, keySelection []int) ([]int, error) {
|
||||
validKeySelection := py_builtin.All[int](keySelection, func(i int) bool {
|
||||
return 0 <= i && i < s.KeypadSize.NumbOfKeys
|
||||
func (s *UserSignSession) SetUserNKode(username m.Username, keySelection m.KeySelection) (m.IdxInterface, error) {
|
||||
validKeySelection := py.All[int](keySelection, func(i int) bool {
|
||||
return 0 <= i && i < s.Keypad.NumbOfKeys
|
||||
})
|
||||
if !validKeySelection {
|
||||
return nil, errors.New(fmt.Sprintf("one or key selection is out of range 0-%d", s.KeypadSize.NumbOfKeys-1))
|
||||
return nil, errors.New(fmt.Sprintf("one or key selection is out of range 0-%d", s.Keypad.NumbOfKeys-1))
|
||||
}
|
||||
|
||||
s.SetKeyEntry = keySelection
|
||||
s.SetKeySelection = keySelection
|
||||
s.Username = username
|
||||
|
||||
setInterface := UserInterface{IdxInterface: s.SetInterface, KeypadSize: s.KeypadSize}
|
||||
setInterface := UserInterface{IdxInterface: s.SetIdxInterface, KeypadSize: s.Keypad}
|
||||
err := setInterface.DisperseInterface()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.ConfirmInterface = setInterface.IdxInterface
|
||||
return s.ConfirmInterface, nil
|
||||
s.ConfirmIdxInterface = setInterface.IdxInterface
|
||||
return s.ConfirmIdxInterface, nil
|
||||
}
|
||||
|
||||
func (s *UserSignSession) getSelectedKeyVals(keySelections []int, userInterface []int) ([][]int, error) {
|
||||
keypadInterface, err := util.ListToMatrix(userInterface, s.KeypadSize.AttrsPerKey)
|
||||
func (s *UserSignSession) getSelectedKeyVals(keySelections m.KeySelection, userInterface []int) ([][]int, error) {
|
||||
keypadInterface, err := util.ListToMatrix(userInterface, s.Keypad.AttrsPerKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -155,7 +155,7 @@ func signupInterface(baseUserInterface UserInterface) (*UserInterface, error) {
|
||||
}
|
||||
signupUserInterface := UserInterface{
|
||||
IdxInterface: util.MatrixToList(attrSetView),
|
||||
KeypadSize: model.KeypadSize{
|
||||
KeypadSize: m.KeypadSize{
|
||||
AttrsPerKey: numbOfKeys,
|
||||
NumbOfKeys: numbOfKeys,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user