package model import "encoding/json" type Inbound struct { Id uint `json:"id" form:"id" gorm:"primaryKey;autoIncrement"` Type string `json:"type" form:"type"` Tag string `json:"tag" form:"tag"` // Foreign key to tls table TlsId uint `json:"tls_id" form:"tls_id"` Tls *Tls `json:"tls" form:"tls" gorm:"foreignKey:TlsId;references:Id"` Addrs json.RawMessage `json:"addrs" form:"addrs"` OutJson json.RawMessage `json:"outJson" form:"outJson"` Options json.RawMessage `json:"-" form:"-"` } func (i *Inbound) UnmarshalJSON(data []byte) error { var err error var raw map[string]interface{} if err = json.Unmarshal(data, &raw); err != nil { return err } // Extract fixed fields and store the rest in Options if val, exists := raw["id"].(uint); exists { i.Id = val delete(raw, "id") } i.Type, _ = raw["type"].(string) delete(raw, "type") i.Tag, _ = raw["tag"].(string) delete(raw, "tag") // TlsId if val, exists := raw["tls_id"].(float64); exists { i.TlsId = uint(val) } delete(raw, "tls_id") delete(raw, "tls") delete(raw, "users") // Addrs i.Addrs, _ = json.MarshalIndent(raw["addrs"], "", " ") delete(raw, "addrs") // OutJson i.OutJson, _ = json.MarshalIndent(raw["outJson"], "", " ") delete(raw, "outJson") // Remaining fields i.Options, err = json.MarshalIndent(raw, "", " ") return err } // MarshalJSON customizes marshalling func (i Inbound) MarshalJSON() ([]byte, error) { // Combine fixed fields and dynamic fields into one map combined := make(map[string]interface{}) combined["id"] = i.Id combined["type"] = i.Type combined["tag"] = i.Tag if i.Tls != nil { combined["tls"] = i.Tls.Server } if i.Options != nil { var restFields map[string]json.RawMessage if err := json.Unmarshal(i.Options, &restFields); err != nil { return nil, err } for k, v := range restFields { combined[k] = v } } return json.Marshal(combined) }