myxce/src/frontend/xcecli/models/resource.go

202 lines
5.7 KiB
Go

package models
import (
"encoding/json"
"fmt"
"strings"
)
const (
HEADER_RESOURCE_NAME = "RESOURCE"
HEADER_RESOURCE_VALUE = "VALUE"
HEADER_RESOURCE_LOC = "LOCATION"
HEADER_RESOURCE_DEF_TYPE = "TYPE"
HEADER_RESOURCE_DEF_ORDER = "ORDER"
HEADER_RESOURCE_DEF_BUILTIN = "BUILTIN"
HEADER_RESOURCE_DEF_CONSUMABLE = "CONSUMABLE"
HEADER_RESOURCE_DEF_DYNAMIC = "DYNAMIC"
HEADER_RESOURCE_DEF_RELEASE = "RELEASE"
HEADER_RESOURCE_DEF_INTERVAL = "INTERVAL"
HEADER_RESOURCE_DEF_DESC = "DESCRIPTION"
)
var ResourceDistFieldsMap = map[string]string{
HEADER_RESOURCE_NAME: "res_name",
HEADER_RESOURCE_VALUE: "value",
HEADER_RESOURCE_LOC: "location",
}
// ResourceDefFieldsMap maps resource definition field headers to API field names
var ResourceDefFieldsMap = map[string]string{
HEADER_RESOURCE_NAME: "res_name",
HEADER_RESOURCE_DEF_DESC: "description",
HEADER_RESOURCE_DEF_TYPE: "type",
HEADER_RESOURCE_DEF_ORDER: "order",
HEADER_RESOURCE_DEF_BUILTIN: "builtin",
HEADER_RESOURCE_DEF_CONSUMABLE: "consumable",
HEADER_RESOURCE_DEF_DYNAMIC: "dynamic",
HEADER_RESOURCE_DEF_RELEASE: "release",
HEADER_RESOURCE_DEF_INTERVAL: "interval",
}
var ResourceDefDetailHeaders = []string{
HEADER_RESOURCE_DEF_TYPE,
HEADER_RESOURCE_DEF_ORDER,
HEADER_RESOURCE_DEF_INTERVAL,
HEADER_RESOURCE_DEF_BUILTIN,
HEADER_RESOURCE_DEF_DYNAMIC,
HEADER_RESOURCE_DEF_RELEASE,
HEADER_RESOURCE_DEF_CONSUMABLE,
}
// ResourceValue can handle both string and boolean values from the API
type ResourceValue struct {
Value string
}
// ResourceLocation can handle both string and array values from the API
type ResourceLocation struct {
Value string
}
// UnmarshalJSON customizes the JSON parsing method to handle string, boolean, and numeric values
func (rv *ResourceValue) UnmarshalJSON(data []byte) error {
// First try to parse as a string
var str string
if err := json.Unmarshal(data, &str); err == nil {
rv.Value = str
return nil
}
// If it's not a string, try to parse as a boolean
var boolVal bool
if err := json.Unmarshal(data, &boolVal); err == nil {
if boolVal {
rv.Value = "true"
} else {
rv.Value = "false"
}
return nil
}
// If it's not a boolean, try to parse as a number (int or float)
var num json.Number
if err := json.Unmarshal(data, &num); err == nil {
rv.Value = num.String()
return nil
}
return fmt.Errorf("cannot unmarshal ResourceValue %s", string(data))
}
// String returns the string representation of ResourceValue
func (rv ResourceValue) String() string {
return rv.Value
}
// UnmarshalJSON customizes the JSON parsing method to handle string and array values
func (rl *ResourceLocation) UnmarshalJSON(data []byte) error {
// First try to parse as a string
var str string
if err := json.Unmarshal(data, &str); err == nil {
rl.Value = str
return nil
}
// If it's not a string, try to parse as an array
var arr []interface{}
if err := json.Unmarshal(data, &arr); err == nil {
// Convert array to comma-separated string
var strValues []string
for _, item := range arr {
if strItem, ok := item.(string); ok {
strValues = append(strValues, strItem)
} else {
// Convert non-string items to string
jsonBytes, err := json.Marshal(item)
if err != nil {
return fmt.Errorf("cannot marshal location array item: %v", err)
}
strValues = append(strValues, string(jsonBytes))
}
}
rl.Value = strings.Join(strValues, ", ")
return nil
}
return fmt.Errorf("cannot unmarshal ResourceLocation %s", string(data))
}
// String returns the string representation of ResourceLocation
func (rl ResourceLocation) String() string {
return rl.Value
}
type ResourceDistResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data struct {
Count int `json:"count"`
Rows []ResourceDist `json:"rows"`
Page_token string `json:"page_token"`
} `json:"data"`
}
type ResourceDist struct {
ResourceName string `json:"res_name"`
ResourceValue ResourceValue `json:"value"`
ResourceLoc ResourceLocation `json:"location"`
}
func (r *ResourceDist) UnmarshalJSON(data []byte) error {
type Alias ResourceDist
var a Alias
if err := UnmarshalOrderMapRow(data, &a); err != nil {
return err
}
*r = ResourceDist(a)
return nil
}
// ResourceDefResponse represents the API response for resource definition queries
type ResourceDefResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data struct {
Count int `json:"count"`
Rows []ResourceDef `json:"rows"`
Page_token string `json:"page_token"`
} `json:"data"`
}
// ResourceDef represents a resource definition
type ResourceDef struct {
ResourceName string `json:"res_name"`
Description string `json:"description"`
ResourceType string `json:"-"` // populated from value_type int
OrderType string `json:"-"` // populated from order_type int
Flag map[string]interface{} `json:"flag"`
Interval int `json:"interval"`
}
func (r *ResourceDef) UnmarshalJSON(data []byte) error {
type Alias struct {
ResourceName string `json:"res_name"`
Description string `json:"description"`
ResourceType string `json:"value_type"`
OrderType string `json:"order_type"`
Flag map[string]interface{} `json:"flag"`
Interval int `json:"interval"`
}
var a Alias
if err := UnmarshalOrderMapRow(data, &a); err != nil {
return err
}
r.ResourceName = a.ResourceName
r.Description = a.Description
r.ResourceType = a.ResourceType
r.OrderType = a.OrderType
r.Flag = a.Flag
r.Interval = a.Interval
return nil
}