Refactor models to use single struct per entity
This commit is contained in:
35
scnserver/models/duration.go
Normal file
35
scnserver/models/duration.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SCNDuration time.Duration
|
||||
|
||||
func (t SCNDuration) MarshalToDB(v SCNDuration) (int64, error) {
|
||||
return v.Duration().Milliseconds(), nil
|
||||
}
|
||||
|
||||
func (t SCNDuration) UnmarshalToModel(v int64) (SCNDuration, error) {
|
||||
return SCNDuration(timeext.FromMilliseconds(v)), nil
|
||||
}
|
||||
|
||||
func (t SCNDuration) Duration() time.Duration {
|
||||
return time.Duration(t)
|
||||
}
|
||||
|
||||
func (t *SCNDuration) UnmarshalJSON(data []byte) error {
|
||||
flt := float64(0)
|
||||
if err := json.Unmarshal(data, &flt); err != nil {
|
||||
return err
|
||||
}
|
||||
d0 := timeext.FromSeconds(flt)
|
||||
*t = SCNDuration(d0)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t SCNDuration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.Duration().Seconds())
|
||||
}
|
||||
Reference in New Issue
Block a user