54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package model
|
|
|
|
import "encoding/json"
|
|
|
|
// APIResponse is the standard JSON envelope.
|
|
type APIResponse struct {
|
|
Status string `json:"status"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
Error *APIError `json:"error,omitempty"`
|
|
}
|
|
|
|
type APIError struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func SuccessResponse(data interface{}) APIResponse {
|
|
return APIResponse{Status: "success", Data: data}
|
|
}
|
|
|
|
func FailedResponse(message string, code int) APIResponse {
|
|
return APIResponse{Status: "failed", Error: &APIError{Code: code, Message: message}}
|
|
}
|
|
|
|
// UnmarshalFilterRules parses filters from JSON, tolerating nil.
|
|
func UnmarshalFilterRules(data string) []FilterRule {
|
|
if data == "" || data == "[]" {
|
|
return []FilterRule{}
|
|
}
|
|
var rules []FilterRule
|
|
if err := json.Unmarshal([]byte(data), &rules); err != nil {
|
|
return []FilterRule{}
|
|
}
|
|
if rules == nil {
|
|
return []FilterRule{}
|
|
}
|
|
return rules
|
|
}
|
|
|
|
// UnmarshalMeta parses meta from JSON, tolerating nil.
|
|
func UnmarshalMeta(data string) map[string]any {
|
|
if data == "" || data == "{}" {
|
|
return map[string]any{}
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(data), &m); err != nil {
|
|
return map[string]any{}
|
|
}
|
|
if m == nil {
|
|
return map[string]any{}
|
|
}
|
|
return m
|
|
}
|