mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Timestamps on apps / routes (#614)
* route updated_at * add app created at, fix some route updated_at bugs * add app updated_at TODO need to add tests through front end TODO for validation we don't really want to use the validate wrapper since it's a programmer error and not a user error, hopefully tests block this. * add tests for timestamps to exist / change on apps&routes * route equals at done, fix tests wit dis * fix up the equals sugar * add swagger * fix rebase * precisely allocate maps in clone * vetted * meh * fix api tests
This commit is contained in:
@@ -1,8 +1,29 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-openapi/strfmt"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Name string `json:"name" db:"name"`
|
||||
Config Config `json:"config,omitempty" db:"config"`
|
||||
Name string `json:"name" db:"name"`
|
||||
Config Config `json:"config,omitempty" db:"config"`
|
||||
CreatedAt strfmt.DateTime `json:"created_at,omitempty" db:"created_at"`
|
||||
UpdatedAt strfmt.DateTime `json:"updated_at,omitempty" db:"updated_at"`
|
||||
}
|
||||
|
||||
func (a *App) SetDefaults() {
|
||||
if time.Time(a.CreatedAt).IsZero() {
|
||||
a.CreatedAt = strfmt.DateTime(time.Now())
|
||||
}
|
||||
if time.Time(a.UpdatedAt).IsZero() {
|
||||
a.UpdatedAt = strfmt.DateTime(time.Now())
|
||||
}
|
||||
if a.Config == nil {
|
||||
// keeps the json from being nil
|
||||
a.Config = map[string]string{}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) Validate() error {
|
||||
@@ -21,24 +42,42 @@ func (a *App) Validate() error {
|
||||
}
|
||||
|
||||
func (a *App) Clone() *App {
|
||||
var c App
|
||||
c.Name = a.Name
|
||||
clone := new(App)
|
||||
*clone = *a // shallow copy
|
||||
|
||||
// now deep copy the map
|
||||
if a.Config != nil {
|
||||
c.Config = make(Config)
|
||||
clone.Config = make(Config, len(a.Config))
|
||||
for k, v := range a.Config {
|
||||
c.Config[k] = v
|
||||
clone.Config[k] = v
|
||||
}
|
||||
}
|
||||
return &c
|
||||
return clone
|
||||
}
|
||||
|
||||
// UpdateConfig adds entries from patch to a.Config, and removes entries with empty values.
|
||||
func (a *App) UpdateConfig(patch Config) {
|
||||
if patch != nil {
|
||||
func (a1 *App) Equals(a2 *App) bool {
|
||||
// start off equal, check equivalence of each field.
|
||||
// the RHS of && won't eval if eq==false so config checking is lazy
|
||||
|
||||
eq := true
|
||||
eq = eq && a1.Name == a2.Name
|
||||
eq = eq && a1.Config.Equals(a2.Config)
|
||||
// NOTE: datastore tests are not very fun to write with timestamp checks,
|
||||
// and these are not values the user may set so we kind of don't care.
|
||||
//eq = eq && time.Time(a1.CreatedAt).Equal(time.Time(a2.CreatedAt))
|
||||
//eq = eq && time.Time(a1.UpdatedAt).Equal(time.Time(a2.UpdatedAt))
|
||||
return eq
|
||||
}
|
||||
|
||||
// Update adds entries from patch to a.Config, and removes entries with empty values.
|
||||
func (a *App) Update(src *App) {
|
||||
original := a.Clone()
|
||||
|
||||
if src.Config != nil {
|
||||
if a.Config == nil {
|
||||
a.Config = make(Config)
|
||||
}
|
||||
for k, v := range patch {
|
||||
for k, v := range src.Config {
|
||||
if v == "" {
|
||||
delete(a.Config, k)
|
||||
} else {
|
||||
@@ -46,6 +85,10 @@ func (a *App) UpdateConfig(patch Config) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !a.Equals(original) {
|
||||
a.UpdatedAt = strfmt.DateTime(time.Now())
|
||||
}
|
||||
}
|
||||
|
||||
// AppFilter is the filter used for querying apps
|
||||
|
||||
Reference in New Issue
Block a user