package grpcapi import ( "encoding/json" "fmt" "git.coopgo.io/coopgo-platform/mobility-accounts/storage" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/types/known/structpb" ) func (a Account) ToStorageType() storage.Account { var localauth storage.LocalAuth if a.Authentication != nil && a.Authentication.Local != nil { localauth = a.Authentication.Local.ToStorageType() } account := storage.Account{ ID: a.Id, Namespace: a.Namespace, Data: map[string]any{}, Authentication: storage.AccountAuth{ Local: localauth, }, } for k, d := range a.Data.GetFields() { jsondata, err := protojson.Marshal(d) if err != nil { fmt.Println(err) break } var data any json.Unmarshal(jsondata, &data) account.Data[k] = data } return account } func (lc LocalAuth) ToStorageType() storage.LocalAuth { return storage.LocalAuth{ Username: lc.Username, Password: lc.Password, Email: lc.Email, EmailValidation: storage.Validation{ Validated: lc.EmailValidation.Validated, ValidationCode: lc.EmailValidation.ValidationCode, }, PhoneNumber: lc.PhoneNumber, PhoneNumberValidation: storage.Validation{ Validated: lc.PhoneNumberValidation.Validated, ValidationCode: lc.PhoneNumberValidation.ValidationCode, }, } } func AccountFromStorageType(account *storage.Account) (*Account, error) { lc := LocalAuthFromStorageType(account.Authentication.Local) d, err := sanitizeData(account.Data) if err != nil { return nil, err } data, err := structpb.NewStruct(d) if err != nil { fmt.Println(err) return nil, err } return &Account{ Id: account.ID, Namespace: account.Namespace, Data: data, Authentication: &AccountAuth{ Local: lc, }, }, nil } func LocalAuthFromStorageType(lc storage.LocalAuth) *LocalAuth { return &LocalAuth{ Username: lc.Username, Password: lc.Password, Email: lc.Email, PhoneNumber: lc.PhoneNumber, EmailValidation: &Validation{ Validated: lc.EmailValidation.Validated, ValidationCode: lc.EmailValidation.ValidationCode, }, PhoneNumberValidation: &Validation{ Validated: lc.PhoneNumberValidation.Validated, ValidationCode: lc.PhoneNumberValidation.ValidationCode, }, } } func sanitizeData(data map[string]any) (d map[string]any, err error) { j, err := json.Marshal(data) if err != nil { return nil, err } if err = json.Unmarshal(j, &d); err != nil { return nil, err } return d, nil }