Nexus/pkg/nex/command_context.go

91 lines
1.8 KiB
Go
Raw Normal View History

2024-10-20 09:23:53 +00:00
package nex
import (
2024-10-20 13:22:53 +00:00
"fmt"
2024-10-20 09:23:53 +00:00
"github.com/goccy/go-json"
2024-10-20 13:22:53 +00:00
"net/http"
2024-10-20 09:23:53 +00:00
"sync"
)
type CommandCtx struct {
requestBody []byte
responseBody []byte
2024-10-20 10:13:07 +00:00
contentType string
statusCode int
2024-10-20 09:23:53 +00:00
values sync.Map
}
2024-10-20 13:22:53 +00:00
func CtxValueMustBe[T any](c *CommandCtx, key string) (T, error) {
if val, ok := c.values.Load(key); ok {
if v, ok := val.(T); ok {
return v, nil
}
}
var out T
if err := c.Write([]byte(fmt.Sprintf("value %s not found in type %T", key, out)), "text/plain+error", http.StatusBadRequest); err != nil {
return out, err
}
return out, fmt.Errorf("value %s not found", key)
}
func CtxValueShouldBe[T any](c *CommandCtx, key string, defaultValue T) T {
if val, ok := c.values.Load(key); ok {
if v, ok := val.(T); ok {
return v
}
}
return defaultValue
}
2024-10-20 10:13:07 +00:00
func (c *CommandCtx) Values() map[string]any {
duplicate := make(map[string]any)
c.values.Range(func(key, value any) bool {
duplicate[key.(string)] = value
return true
})
return duplicate
}
func (c *CommandCtx) ValueOrElse(key string, defaultValue any) any {
val, _ := c.values.Load(key)
if val == nil {
return defaultValue
}
return val
}
2024-10-20 09:23:53 +00:00
func (c *CommandCtx) Value(key string, newValue ...any) any {
if len(newValue) > 0 {
c.values.Store(key, newValue[0])
}
val, _ := c.values.Load(key)
return val
}
func (c *CommandCtx) Read() []byte {
return c.requestBody
}
func (c *CommandCtx) ReadJSON(out any) error {
return json.Unmarshal(c.requestBody, out)
}
2024-10-20 10:13:07 +00:00
func (c *CommandCtx) Write(data []byte, contentType string, statusCode ...int) error {
2024-10-20 09:23:53 +00:00
c.responseBody = data
2024-10-20 10:13:07 +00:00
c.contentType = contentType
2024-10-20 09:23:53 +00:00
if len(statusCode) > 0 {
c.statusCode = statusCode[0]
}
return nil
}
func (c *CommandCtx) JSON(data any, statusCode ...int) error {
raw, err := json.Marshal(data)
if err != nil {
return err
}
2024-10-20 10:13:07 +00:00
return c.Write(raw, "application/json", statusCode...)
2024-10-20 09:23:53 +00:00
}