Files

96 lines
2.2 KiB
Go

// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
package shared
import (
"encoding/json"
"errors"
"fmt"
"github.com/formancehq/formance-sdk-go/v2/pkg/utils"
)
type V2SubjectType string
const (
V2SubjectTypeAccount V2SubjectType = "ACCOUNT"
V2SubjectTypeWallet V2SubjectType = "WALLET"
)
type V2Subject struct {
V2LedgerAccountSubject *V2LedgerAccountSubject
V2WalletSubject *V2WalletSubject
Type V2SubjectType
}
func CreateV2SubjectAccount(account V2LedgerAccountSubject) V2Subject {
typ := V2SubjectTypeAccount
typStr := string(typ)
account.Type = typStr
return V2Subject{
V2LedgerAccountSubject: &account,
Type: typ,
}
}
func CreateV2SubjectWallet(wallet V2WalletSubject) V2Subject {
typ := V2SubjectTypeWallet
typStr := string(typ)
wallet.Type = typStr
return V2Subject{
V2WalletSubject: &wallet,
Type: typ,
}
}
func (u *V2Subject) UnmarshalJSON(data []byte) error {
type discriminator struct {
Type string `json:"type"`
}
dis := new(discriminator)
if err := json.Unmarshal(data, &dis); err != nil {
return fmt.Errorf("could not unmarshal discriminator: %w", err)
}
switch dis.Type {
case "ACCOUNT":
v2LedgerAccountSubject := new(V2LedgerAccountSubject)
if err := utils.UnmarshalJSON(data, &v2LedgerAccountSubject, "", true, true); err != nil {
return fmt.Errorf("could not unmarshal expected type: %w", err)
}
u.V2LedgerAccountSubject = v2LedgerAccountSubject
u.Type = V2SubjectTypeAccount
return nil
case "WALLET":
v2WalletSubject := new(V2WalletSubject)
if err := utils.UnmarshalJSON(data, &v2WalletSubject, "", true, true); err != nil {
return fmt.Errorf("could not unmarshal expected type: %w", err)
}
u.V2WalletSubject = v2WalletSubject
u.Type = V2SubjectTypeWallet
return nil
}
return errors.New("could not unmarshal into supported union types")
}
func (u V2Subject) MarshalJSON() ([]byte, error) {
if u.V2LedgerAccountSubject != nil {
return utils.MarshalJSON(u.V2LedgerAccountSubject, "", true)
}
if u.V2WalletSubject != nil {
return utils.MarshalJSON(u.V2WalletSubject, "", true)
}
return nil, errors.New("could not marshal union type: all fields are null")
}