skip

package module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2025 License: MIT Imports: 7 Imported by: 0

README

goskip · GitHub license

goskip is an unoffical open-source client for Skip.

Usage

If you're unfamiliar with Skip, you can find the documentation on the Skip website.

This repository is a wrapper for the skip API. It exposes two clients, ControlClient and StreamClient. These clients are used across collections and resources and then read functions are used to parse the underlying data.

To get started, create the clients:

controlClient := skip.NewControlClient("<control_url>")
streamClient := skip.NewStreamClient("<stream_url>")

Then you can use the exported methods on the clients to call the API.

Streaming Data

To create a resource instance:

uuid, err := controlClient.CreateResourceInstance(ctx, "<resource_name>", <params>)

You can then use the uuid to stream data:

err := streamClient.Stream(ctx, uuid, func(event skip.StreamType, data []byte) error {
    // handle untyped data
    return nil
})

The above example handles untyped data. If you want to handle typed data, you can use the skip.ReadStream function:

err := streamClient.Stream(ctx, uuid, skip.ReadStream(func(event skip.StreamType, data []skip.CollectionValue[<key_type>, <value_type>]) error {
    // handle typed data
    return nil
}))
Updating Data

To insert data, create a data type and use the UpdateInputCollection method:

type DataValue struct {
    Name       string `json:"name"`
    DrankWater bool `json:"drank_water"`
}

err = controlClient.UpdateInputCollection(ctx, "<collection_name>", []skip.CollectionData{
    {
        Key: <key_value>,
        Values: skip.Values(
            DataValue{
                Name:    "Some Name",
                DrankWater: true,
            },
            DataValue{
                Name:    "Other Name",
                DrankWater: false,
            },
        ),
    },
})
Snapshoting Data

Using Skip's API, you can snapshot a resource collection or an individual key in a collection:

// Collection:
snapshot, err := skip.ReadResourceSnapshot[<key_type>, <value_type>](controlClient.GetResourceSnapshot(ctx, "<resource_name>", <params>))

// Individual Key:
key, err := skip.ReadResourceKey[<value_type>](controlClient.GetResourceKey(ctx, "<resource_name>", <resource_key>, <params>))

Examples

The examples directory contains examples that have a client.go file and a skip directory. To run an example, run:

go run examples/<example>/client.go

These are the examples available:

goskip Image

This repository also manages a simple Skip image goskip-image that's published to Docker Hub as lidtop/goskip.

Contributing

Contributions and pull requests are welcome! Feel free to drop an issue if you have any ideas or suggestions.

License

goskip is MIT licensed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadResourceKey added in v0.0.7

func ReadResourceKey[V any](data []byte, err error) ([]V, error)

ReadResourceKey takes raw resource key data and unmarshals it with the given type.

func ReadStream added in v0.0.7

func ReadStream[K any, V any](callback func(event StreamType, data []CollectionValue[K, V]) error) func(event StreamType, data []byte) error

func Values added in v0.0.7

func Values[T any](v ...T) []skipValue

Types

type CollectionData added in v0.0.7

type CollectionData struct {
	Key    interface{}
	Values []skipValue
}

CollectionData represents a untyped set of collection values. This is used when posting new data to a collection.

func (*CollectionData) MarshalJSON added in v0.0.7

func (u *CollectionData) MarshalJSON() ([]byte, error)

func (*CollectionData) UnmarshalJSON added in v0.0.7

func (u *CollectionData) UnmarshalJSON(data []byte) error

type CollectionValue added in v0.0.7

type CollectionValue[K any, V any] struct {
	Key    K
	Values []V
}

CollectionValue is a typed set of collection values. This is used when reading data from a collection.

func ReadResourceSnapshot added in v0.0.7

func ReadResourceSnapshot[K any, V any](data []byte, err error) ([]CollectionValue[K, V], error)

ReadResourceSnapshot takes raw resource snapshot data and unmarshals it with the given types.

func (*CollectionValue[K, V]) UnmarshalJSON added in v0.0.7

func (c *CollectionValue[K, V]) UnmarshalJSON(data []byte) error

type ControlClient

type ControlClient interface {
	// GetSnapshot retrieves a snapshot of the entire resource.
	// Corresponds to the POST /v1/snapshot/:resource endpoint.
	GetResourceSnapshot(ctx context.Context, resource string, params interface{}) ([]byte, error)

	// GetResourceKey retrieves the data associated with a specific key in a resource.
	// Corresponds to the POST /v1/snapshot/:resource/lookup endpoint.
	GetResourceKey(ctx context.Context, resource string, key interface{}, params interface{}) ([]byte, error)

	// UpdateInputCollection updates a collection of key-value pairs in the specified input collection.
	// Corresponds to the PATCH /v1/inputs/:collection endpoint.
	UpdateInputCollection(ctx context.Context, collection string, updates []CollectionData) error

	// CreateResourceInstance creates a new resource instance and returns its UUID.
	// Corresponds to the POST /v1/streams/:resource endpoint.
	CreateResourceInstance(ctx context.Context, resource string, params interface{}) (string, error)

	// DeleteResourceInstance deletes a resource instance by its UUID.
	// Corresponds to the DELETE /v1/streams/:uuid endpoint.
	DeleteResourceInstance(ctx context.Context, uuid string) error
}

ControlClient defines access to Skip's Control API.

func NewControlClient

func NewControlClient(baseURL string) ControlClient

NewControlClient creates a new instance of ControlClient.

type StreamClient

type StreamClient interface {
	// Stream is a live data stream for a resource instance represented by the UUID.
	// Corresponds to the GET /v1/streams/:uuid endpoint.
	Stream(ctx context.Context, uuid string, callback func(event StreamType, data []byte) error) error
}

StreamClient defines access to Skip's Stream API.

func NewStreamClient added in v0.0.7

func NewStreamClient(baseURL string) StreamClient

NewStreamClient creates a new instance of StreamClient.

type StreamType

type StreamType string
const (
	InitStreamType   StreamType = "init"
	UpdateStreamType StreamType = "update"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL