🏡 index : ironmagma/quintodrome.git

// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the Cache host service.
// It is intended for use in Navidrome plugins built with TinyGo.
//
//go:build wasip1

package host

import (
	"encoding/json"
	"errors"

	"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)

// cache_setstring is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_setstring
func cache_setstring(uint64) uint64

// cache_getstring is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_getstring
func cache_getstring(uint64) uint64

// cache_setint is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_setint
func cache_setint(uint64) uint64

// cache_getint is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_getint
func cache_getint(uint64) uint64

// cache_setfloat is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_setfloat
func cache_setfloat(uint64) uint64

// cache_getfloat is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_getfloat
func cache_getfloat(uint64) uint64

// cache_setbytes is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_setbytes
func cache_setbytes(uint64) uint64

// cache_getbytes is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_getbytes
func cache_getbytes(uint64) uint64

// cache_has is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_has
func cache_has(uint64) uint64

// cache_remove is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user cache_remove
func cache_remove(uint64) uint64

type cacheSetStringRequest struct {
	Key        string `json:"key"`
	Value      string `json:"value"`
	TtlSeconds int64  `json:"ttlSeconds"`
}

type cacheGetStringRequest struct {
	Key string `json:"key"`
}

type cacheGetStringResponse struct {
	Value  string `json:"value,omitempty"`
	Exists bool   `json:"exists,omitempty"`
	Error  string `json:"error,omitempty"`
}

type cacheSetIntRequest struct {
	Key        string `json:"key"`
	Value      int64  `json:"value"`
	TtlSeconds int64  `json:"ttlSeconds"`
}

type cacheGetIntRequest struct {
	Key string `json:"key"`
}

type cacheGetIntResponse struct {
	Value  int64  `json:"value,omitempty"`
	Exists bool   `json:"exists,omitempty"`
	Error  string `json:"error,omitempty"`
}

type cacheSetFloatRequest struct {
	Key        string  `json:"key"`
	Value      float64 `json:"value"`
	TtlSeconds int64   `json:"ttlSeconds"`
}

type cacheGetFloatRequest struct {
	Key string `json:"key"`
}

type cacheGetFloatResponse struct {
	Value  float64 `json:"value,omitempty"`
	Exists bool    `json:"exists,omitempty"`
	Error  string  `json:"error,omitempty"`
}

type cacheSetBytesRequest struct {
	Key        string `json:"key"`
	Value      []byte `json:"value"`
	TtlSeconds int64  `json:"ttlSeconds"`
}

type cacheGetBytesRequest struct {
	Key string `json:"key"`
}

type cacheGetBytesResponse struct {
	Value  []byte `json:"value,omitempty"`
	Exists bool   `json:"exists,omitempty"`
	Error  string `json:"error,omitempty"`
}

type cacheHasRequest struct {
	Key string `json:"key"`
}

type cacheHasResponse struct {
	Exists bool   `json:"exists,omitempty"`
	Error  string `json:"error,omitempty"`
}

type cacheRemoveRequest struct {
	Key string `json:"key"`
}

// CacheSetString calls the cache_setstring host function.
// SetString stores a string value in the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//   - value: The string value to store
//   - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours)
//
// Returns an error if the operation fails.
func CacheSetString(key string, value string, ttlSeconds int64) error {
	// Marshal request to JSON
	req := cacheSetStringRequest{
		Key:        key,
		Value:      value,
		TtlSeconds: ttlSeconds,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_setstring(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse error-only response
	var response struct {
		Error string `json:"error,omitempty"`
	}
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return err
	}
	if response.Error != "" {
		return errors.New(response.Error)
	}
	return nil
}

// CacheGetString calls the cache_getstring host function.
// GetString retrieves a string value from the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//
// Returns the value and whether the key exists. If the key doesn't exist
// or the stored value is not a string, exists will be false.
func CacheGetString(key string) (string, bool, error) {
	// Marshal request to JSON
	req := cacheGetStringRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return "", false, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_getstring(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response cacheGetStringResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return "", false, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return "", false, errors.New(response.Error)
	}

	return response.Value, response.Exists, nil
}

// CacheSetInt calls the cache_setint host function.
// SetInt stores an integer value in the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//   - value: The integer value to store
//   - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours)
//
// Returns an error if the operation fails.
func CacheSetInt(key string, value int64, ttlSeconds int64) error {
	// Marshal request to JSON
	req := cacheSetIntRequest{
		Key:        key,
		Value:      value,
		TtlSeconds: ttlSeconds,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_setint(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse error-only response
	var response struct {
		Error string `json:"error,omitempty"`
	}
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return err
	}
	if response.Error != "" {
		return errors.New(response.Error)
	}
	return nil
}

// CacheGetInt calls the cache_getint host function.
// GetInt retrieves an integer value from the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//
// Returns the value and whether the key exists. If the key doesn't exist
// or the stored value is not an integer, exists will be false.
func CacheGetInt(key string) (int64, bool, error) {
	// Marshal request to JSON
	req := cacheGetIntRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return 0, false, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_getint(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response cacheGetIntResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return 0, false, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return 0, false, errors.New(response.Error)
	}

	return response.Value, response.Exists, nil
}

// CacheSetFloat calls the cache_setfloat host function.
// SetFloat stores a float value in the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//   - value: The float value to store
//   - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours)
//
// Returns an error if the operation fails.
func CacheSetFloat(key string, value float64, ttlSeconds int64) error {
	// Marshal request to JSON
	req := cacheSetFloatRequest{
		Key:        key,
		Value:      value,
		TtlSeconds: ttlSeconds,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_setfloat(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse error-only response
	var response struct {
		Error string `json:"error,omitempty"`
	}
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return err
	}
	if response.Error != "" {
		return errors.New(response.Error)
	}
	return nil
}

// CacheGetFloat calls the cache_getfloat host function.
// GetFloat retrieves a float value from the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//
// Returns the value and whether the key exists. If the key doesn't exist
// or the stored value is not a float, exists will be false.
func CacheGetFloat(key string) (float64, bool, error) {
	// Marshal request to JSON
	req := cacheGetFloatRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return 0, false, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_getfloat(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response cacheGetFloatResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return 0, false, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return 0, false, errors.New(response.Error)
	}

	return response.Value, response.Exists, nil
}

// CacheSetBytes calls the cache_setbytes host function.
// SetBytes stores a byte slice in the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//   - value: The byte slice to store
//   - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours)
//
// Returns an error if the operation fails.
func CacheSetBytes(key string, value []byte, ttlSeconds int64) error {
	// Marshal request to JSON
	req := cacheSetBytesRequest{
		Key:        key,
		Value:      value,
		TtlSeconds: ttlSeconds,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_setbytes(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse error-only response
	var response struct {
		Error string `json:"error,omitempty"`
	}
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return err
	}
	if response.Error != "" {
		return errors.New(response.Error)
	}
	return nil
}

// CacheGetBytes calls the cache_getbytes host function.
// GetBytes retrieves a byte slice from the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//
// Returns the value and whether the key exists. If the key doesn't exist
// or the stored value is not a byte slice, exists will be false.
func CacheGetBytes(key string) ([]byte, bool, error) {
	// Marshal request to JSON
	req := cacheGetBytesRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return nil, false, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_getbytes(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response cacheGetBytesResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return nil, false, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return nil, false, errors.New(response.Error)
	}

	return response.Value, response.Exists, nil
}

// CacheHas calls the cache_has host function.
// Has checks if a key exists in the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//
// Returns true if the key exists and has not expired.
func CacheHas(key string) (bool, error) {
	// Marshal request to JSON
	req := cacheHasRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return false, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_has(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response cacheHasResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return false, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return false, errors.New(response.Error)
	}

	return response.Exists, nil
}

// CacheRemove calls the cache_remove host function.
// Remove deletes a value from the cache.
//
// Parameters:
//   - key: The cache key (will be namespaced with plugin ID)
//
// Returns an error if the operation fails. Does not return an error if the key doesn't exist.
func CacheRemove(key string) error {
	// Marshal request to JSON
	req := cacheRemoveRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := cache_remove(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse error-only response
	var response struct {
		Error string `json:"error,omitempty"`
	}
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return err
	}
	if response.Error != "" {
		return errors.New(response.Error)
	}
	return nil
}