mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
* Move models and other shareable code to pkg/domain and add own go.mod * Update all references to use pkg/domain paths * Fix generated file * Ensure pkg/domain tests are run by just tests * Use new models path in connector templates * Copy local pkg/domain in dockerfile * Ensure pkg/domain is subject to go generate
105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
package models
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/gibson042/canonicaljson-go"
|
|
)
|
|
|
|
type AccountID struct {
|
|
Reference string
|
|
ConnectorID ConnectorID
|
|
}
|
|
|
|
// Ref returns the reference string pointer, or nil if the AccountID is nil.
|
|
func (aid *AccountID) Ref() *string {
|
|
if aid == nil {
|
|
return nil
|
|
}
|
|
return &aid.Reference
|
|
}
|
|
|
|
// StringPtr returns the base64-encoded AccountID as a *string, or nil if the AccountID is nil.
|
|
func (aid *AccountID) StringPtr() *string {
|
|
if aid == nil || aid.Reference == "" {
|
|
return nil
|
|
}
|
|
s := aid.String()
|
|
return &s
|
|
}
|
|
|
|
func (aid *AccountID) String() string {
|
|
if aid == nil || aid.Reference == "" {
|
|
return ""
|
|
}
|
|
|
|
data, err := canonicaljson.Marshal(aid)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(data)
|
|
}
|
|
|
|
func AccountIDFromString(value string) (AccountID, error) {
|
|
ret := AccountID{}
|
|
|
|
data, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(value)
|
|
if err != nil {
|
|
return ret, err
|
|
}
|
|
|
|
err = canonicaljson.Unmarshal(data, &ret)
|
|
if err != nil {
|
|
return ret, err
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
// NewAccountID builds an AccountID pointer from an optional reference and a
|
|
// ConnectorID. Returns nil when ref is nil or empty.
|
|
func NewAccountID(ref *string, connectorID ConnectorID) *AccountID {
|
|
if ref == nil || *ref == "" {
|
|
return nil
|
|
}
|
|
return &AccountID{Reference: *ref, ConnectorID: connectorID}
|
|
}
|
|
|
|
func MustAccountIDFromString(value string) AccountID {
|
|
id, err := AccountIDFromString(value)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func (aid AccountID) Value() (driver.Value, error) {
|
|
return aid.String(), nil
|
|
}
|
|
|
|
func (aid *AccountID) Scan(value interface{}) error {
|
|
if value == nil {
|
|
return errors.New("account id is nil")
|
|
}
|
|
|
|
if s, err := driver.String.ConvertValue(value); err == nil {
|
|
|
|
if v, ok := s.(string); ok {
|
|
|
|
id, err := AccountIDFromString(v)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse account id %s: %v", v, err)
|
|
}
|
|
|
|
*aid = id
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf("failed to scan account id: %v", value)
|
|
}
|