types

package
v0.0.0-...-917641f Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2019 License: MIT Imports: 2 Imported by: 2

Documentation

Overview

Package types implements OT-compatible immutable values.

Strings are implemented by S8 and S16. S8 implements the standard Go string where offsets refer to bytes. S16 is a better choice for working with UFT16 encoded values such as is native to Javascript. In this case, the offsets count the number of UF16 units (which maps to native JS string offsets).

General arrays values can be represented by A while M implements maps.

Counter implements a 32-bit integer counter. This also serves as an example of an interesting data structure as it uses a virtual array as far as OT is concerned but only stores the accumuated count.

A much richer type is available at https://godoc.org/github.com/dotchain/dot/x/rt which also demonstrates how to implement a custom change that is applicable to the type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type A

type A []changes.Value

The A type represents a slice of arbitrary values. It implements the changes.Value interface. The actual elements can be nil (unlike the regular requirement for values to be non-nil). Nil values are treated as if they were changes.Nil

func (A) Apply

func (a A) Apply(ctx changes.Context, c changes.Change) changes.Value

Apply applies the change and returns the updated value

Note: deleting an element via changes.Replace simply replaces it with nil. It does not actually remove the element -- that needs a changes.Splice.

func (A) ApplyCollection

func (a A) ApplyCollection(ctx changes.Context, c changes.Change) changes.Collection

ApplyCollection implements changes.Collection

func (A) Count

func (a A) Count() int

Count returns size of the array

func (A) Slice

func (a A) Slice(offset, count int) changes.Collection

Slice implements changes.Collection.Slice

type Counter

type Counter int32

Counter implements a 32-bit counter that can be incremented, decremented or replaced. Counter implements the changes.Value interface.

func (Counter) Apply

func (c Counter) Apply(ctx changes.Context, cx changes.Change) changes.Value

Apply only supports Replace and Inserts

func (Counter) ApplyCollection

func (c Counter) ApplyCollection(ctx changes.Context, cx changes.Change) changes.Collection

ApplyCollection implements Collection interface

func (Counter) Count

func (c Counter) Count() int

Count always returns 1 for counters

func (Counter) Increment

func (c Counter) Increment(by int32) changes.Change

Increment returns a change which implements the increment operation.

func (Counter) Set

func (c Counter) Set(v int32) changes.Change

Set returns a change which implements updating the value

func (Counter) Slice

func (c Counter) Slice(offset, count int) changes.Collection

Slice implements changes.Value.Slice but it is not expected to ever by used for Counters

type Generic

type Generic struct {
	Get    func(key interface{}) changes.Value
	Set    func(key interface{}, v changes.Value) changes.Value
	Splice func(offset, count int, insert changes.Collection) changes.Collection
}

Generic is a helper to build value and collection types

func (Generic) Apply

func (g Generic) Apply(ctx changes.Context, c changes.Change, current changes.Value) changes.Value

Apply applies a change to produce a new value. Note that this requires the current value as input unlike changes.Value.

func (Generic) ApplyCollection

func (g Generic) ApplyCollection(ctx changes.Context, c changes.Change, current changes.Collection) changes.Collection

ApplyCollection implements changes.ApplyCollection but with using the provided current value and the helper functions

type M

type M map[interface{}]changes.Value

The M type represents a map of arbitrary key/values. It implements the changes.Value interface. As with all values, a nil value should be expressed via changes.Atomic{nil} to distinguish it from an attempt to remove a key.

func (M) Apply

func (m M) Apply(ctx changes.Context, c changes.Change) changes.Value

Apply applies the change and returns the updated value

type S16

type S16 string

S16 implements string with offsets and counts referring to utf16 runes. The UTF16 offsets map to native Javascript string offsets.

func (S16) Apply

func (s S16) Apply(ctx changes.Context, c changes.Change) changes.Value

Apply implements Value.Apply

func (S16) ApplyCollection

func (s S16) ApplyCollection(ctx changes.Context, c changes.Change) changes.Collection

ApplyCollection implements changes.Collection

func (S16) Count

func (s S16) Count() int

Count returns the number of UTF16 characters

func (S16) FromUTF16

func (s S16) FromUTF16(idx int) int

FromUTF16 converts an UTF16 offset into a regular string offset It panics if the UTF16 offset lies within a UTF32 character

func (S16) Slice

func (s S16) Slice(offset, count int) changes.Collection

Slice implements changes.Value.Slice. Offset and count are in UTF16 units.

func (S16) ToUTF16

func (s S16) ToUTF16(idx int) int

ToUTF16 converts a regular index to UTF16 offsets

type S8

type S8 string

S8 implements a string whose offsets are UTF8 bytes

func (S8) Apply

func (s S8) Apply(ctx changes.Context, c changes.Change) changes.Value

Apply implements Value.Apply

func (S8) ApplyCollection

func (s S8) ApplyCollection(ctx changes.Context, c changes.Change) changes.Collection

ApplyCollection implements changes.Collection

func (S8) Count

func (s S8) Count() int

Count returns the number of UTF8 bytes in the string

func (S8) Slice

func (s S8) Slice(offset, count int) changes.Collection

Slice implements Value.Slice. Offset and count is based on UTF8

Jump to

Keyboard shortcuts

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