mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package mixin
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/log"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
"github.com/hanzoai/commerce/util/reflect"
|
|
)
|
|
|
|
// Create a new zero'd entity of this type
|
|
func (m *BaseModel) Zero() Entity {
|
|
// Get type of entity
|
|
entity := reflect.ValueOf(m.Entity)
|
|
|
|
// De-pointer if necessary
|
|
for entity.Kind() == reflect.Ptr {
|
|
entity = reflect.Indirect(entity)
|
|
}
|
|
|
|
// Actual type
|
|
typ := entity.Type()
|
|
|
|
// Create new entity
|
|
entity = reflect.New(typ)
|
|
return entity.Interface().(Entity)
|
|
}
|
|
|
|
// Create a clone of current entity
|
|
func (m *BaseModel) Clone() Entity {
|
|
entity := m.Zero()
|
|
if err := reflect.Copy(m.Entity, entity); err != nil {
|
|
log.Warn("Unable to copy of model: %v", err, m.Db.Context)
|
|
}
|
|
return entity
|
|
}
|
|
|
|
// Create a clone of currenty entity using only JSON-serializable data
|
|
func (m *BaseModel) CloneFromJSON() Entity {
|
|
buf := json.EncodeBuffer(m.Entity)
|
|
entity := m.Zero()
|
|
json.DecodeBuffer(buf, entity)
|
|
return entity
|
|
}
|
|
|
|
// Create a slice of entity type suitable for use with datastore.GetAll, etc.
|
|
func (m *BaseModel) Slice() interface{} {
|
|
typ := reflect.TypeOf(m.Entity)
|
|
slice := reflect.MakeSlice(reflect.SliceOf(typ), 0, 0)
|
|
ptr := reflect.New(slice.Type())
|
|
ptr.Elem().Set(slice)
|
|
return ptr.Interface()
|
|
}
|
|
|
|
// Serialize entity to JSON
|
|
func (m *BaseModel) JSON() []byte {
|
|
return json.EncodeBytes(m.Entity)
|
|
}
|
|
|
|
func (m *BaseModel) JSONString() string {
|
|
return string(m.JSON())
|
|
}
|