You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
spiderman/pkg/models/asset.go

82 lines
2.3 KiB

// Package models defines the data models used throughout the application
package models
import (
"encoding/json"
"strconv"
)
// Asset represents an asset in the ledger
type Asset struct {
ID string `json:"ID" example:"asset123"`
Color string `json:"color" example:"red"`
Size string `json:"size" example:"10"`
Owner string `json:"owner" example:"Alice"`
AppraisedValue string `json:"appraisedValue" example:"2000"`
}
// UnmarshalJSON custom unmarshaling for Asset to handle appraisedValue and size as both number and string
func (a *Asset) UnmarshalJSON(data []byte) error {
// Define a temporary struct with the same fields but problematic fields as interface{}
type TempAsset struct {
ID string `json:"ID"`
Color string `json:"color"`
Size interface{} `json:"size"`
Owner string `json:"owner"`
AppraisedValue interface{} `json:"appraisedValue"`
}
var temp TempAsset
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
// Copy simple fields
a.ID = temp.ID
a.Color = temp.Color
a.Owner = temp.Owner
// Handle Size conversion
switch v := temp.Size.(type) {
case string:
a.Size = v
case float64:
a.Size = strconv.FormatFloat(v, 'f', -1, 64)
case int:
a.Size = strconv.Itoa(v)
case int64:
a.Size = strconv.FormatInt(v, 10)
default:
a.Size = "0"
}
// Handle AppraisedValue conversion
switch v := temp.AppraisedValue.(type) {
case string:
a.AppraisedValue = v
case float64:
a.AppraisedValue = strconv.FormatFloat(v, 'f', -1, 64)
case int:
a.AppraisedValue = strconv.Itoa(v)
case int64:
a.AppraisedValue = strconv.FormatInt(v, 10)
default:
a.AppraisedValue = "0"
}
return nil
}
// CreateAssetRequest represents the request payload for creating an asset
type CreateAssetRequest struct {
ID string `json:"id,omitempty" example:"asset123"` // Optional - will be auto-generated if not provided
Color string `json:"color" example:"red"`
Size string `json:"size" example:"10"`
Owner string `json:"owner" example:"Alice"`
AppraisedValue string `json:"appraisedValue" example:"2000"`
}
// TransferAssetRequest represents the request payload for transferring an asset
type TransferAssetRequest struct {
NewOwner string `json:"newOwner" example:"Bob"`
}