package main
import (
"github.com/navidrome/navidrome/plugins/pdk/go/host"
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)
type TestCacheInput struct {
Operation string `json:"operation"`
Key string `json:"key"`
StringVal string `json:"string_val"`
IntVal int64 `json:"int_val"`
FloatVal float64 `json:"float_val"`
BytesVal []byte `json:"bytes_val"`
TTLSeconds int64 `json:"ttl_seconds"`
}
type TestCacheOutput struct {
StringVal string `json:"string_val,omitempty"`
IntVal int64 `json:"int_val,omitempty"`
FloatVal float64 `json:"float_val,omitempty"`
BytesVal []byte `json:"bytes_val,omitempty"`
Exists bool `json:"exists,omitempty"`
Error *string `json:"error,omitempty"`
}
func ndTestCache() int32 {
var input TestCacheInput
if err := pdk.InputJSON(&input); err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
switch input.Operation {
case "set_string":
err := host.CacheSetString(input.Key, input.StringVal, input.TTLSeconds)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{})
return 0
case "get_string":
value, exists, err := host.CacheGetString(input.Key)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{StringVal: value, Exists: exists})
return 0
case "set_int":
err := host.CacheSetInt(input.Key, input.IntVal, input.TTLSeconds)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{})
return 0
case "get_int":
value, exists, err := host.CacheGetInt(input.Key)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{IntVal: value, Exists: exists})
return 0
case "set_float":
err := host.CacheSetFloat(input.Key, input.FloatVal, input.TTLSeconds)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{})
return 0
case "get_float":
value, exists, err := host.CacheGetFloat(input.Key)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{FloatVal: value, Exists: exists})
return 0
case "set_bytes":
err := host.CacheSetBytes(input.Key, input.BytesVal, input.TTLSeconds)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{})
return 0
case "get_bytes":
value, exists, err := host.CacheGetBytes(input.Key)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{BytesVal: value, Exists: exists})
return 0
case "has":
exists, err := host.CacheHas(input.Key)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{Exists: exists})
return 0
case "remove":
err := host.CacheRemove(input.Key)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestCacheOutput{})
return 0
default:
errStr := "unknown operation: " + input.Operation
pdk.OutputJSON(TestCacheOutput{Error: &errStr})
return 0
}
}
func main() {}