bouncer/internal/store/redis/layer_item.go
William Petit e66938f1d3
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
feat: initial commit
2023-05-21 22:27:25 +02:00

63 lines
1.2 KiB
Go

package redis
import (
"time"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
)
type layerHeaderItem struct {
Proxy string `redis:"proxy"`
Name string `redis:"name"`
Type string `redis:"type"`
Weight int `redis:"weight"`
Enabled bool `redis:"enabled"`
}
func (i *layerHeaderItem) ToLayerHeader() (*store.LayerHeader, error) {
layerHeader := &store.LayerHeader{
Proxy: store.ProxyName(i.Proxy),
Name: store.LayerName(i.Name),
Type: store.LayerType(i.Type),
Weight: i.Weight,
Enabled: i.Enabled,
}
return layerHeader, nil
}
type layerItem struct {
layerHeaderItem
Options *jsonWrapper[store.LayerOptions] `redis:"options"`
CreatedAt *jsonWrapper[time.Time] `redis:"created_at"`
UpdatedAt *jsonWrapper[time.Time] `redis:"updated_at"`
}
func (i *layerItem) ToLayer() (*store.Layer, error) {
layerHeader, err := i.layerHeaderItem.ToLayerHeader()
if err != nil {
return nil, errors.WithStack(err)
}
layer := &store.Layer{
LayerHeader: *layerHeader,
}
if i.Options != nil {
layer.Options = i.Options.Value()
}
if i.CreatedAt != nil {
layer.CreatedAt = i.CreatedAt.Value()
}
if i.UpdatedAt != nil {
layer.UpdatedAt = i.UpdatedAt.Value()
}
return layer, nil
}