xform

package
v0.0.0-...-406b1e7 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(m json.RawMessage) bool

Bool decodes a json.RawMessage as a bool

func DeleteEntity

func DeleteEntity(changes wire.Changes, typeName string)

DeleteEntity deletes an entity type by its DB name (name=... in limestone.Meta definitions)

func DeleteFields

func DeleteFields(changes wire.Changes, typeName string, fieldName ...string)

DeleteFields deletes the given fields from an entity type

func IP

func IP(m json.RawMessage) net.IP

IP decodes a json.RawMessage as a IP

func Int

func Int(m json.RawMessage) int

Int decodes a json.RawMessage as an int

func Marshal

func Marshal(v any) json.RawMessage

Marshal returns its argument encoded as json.RawMessage

func RenameEntity

func RenameEntity(changes wire.Changes, typeNameOld, typeNameNew string)

RenameEntity changes the DB name of an entity type (name=... in limestone.Meta definitions)

func RenameField

func RenameField(changes wire.Changes, typeName string, fieldNameOld, fieldNameNew string)

RenameField renames the given field in an entity type

func RenameFields

func RenameFields(changes wire.Changes, typeName string, mapping map[string]string)

RenameFields renames fields in an entity type by given mapping between old and new field names

func ReplaceStringValue

func ReplaceStringValue(changes wire.Changes, typeName, fieldName, oldValue, newValue string)

ReplaceStringValue replaces the given string value of the given field in an entity type

func ReplaceStringValues

func ReplaceStringValues(changes wire.Changes, typeName, fieldName string, mapping map[string]string)

ReplaceStringValues replaces the string values of the given field in an entity type by given mapping

func String

func String(m json.RawMessage) string

String decodes a json.RawMessage as a string

func Time

func Time(m json.RawMessage) time.Time

Time decodes a json.RawMessage as a time

Types

type Transformation

type Transformation func(logger *zap.Logger, source <-chan wire.Transaction, dest chan<- wire.Transaction)

Transformation is a database upgrade or other cleanup procedure that takes a sequence of transactions as input and produces a sequence of transactions as output. The function should read all transactions from source until the end of stream and write the converted stream into dest. Do not close dest.

The transformation may pass transactions without change, modify them, introduce novel transactions, drop, join, split and reorder transactions.

All transactions written into dest must have their TS field set, and these must be in a strictly ascending order.

Panic to fail the transformation.

func ForEach

func ForEach(conv func(logger *zap.Logger, txn *wire.Transaction)) Transformation

ForEach is a shorthand for writing a typical stateless upgrade transformation.

It returns an upgrade transformation that takes a stateless conversion and applies it to each transaction in the stream. Transactions that end up empty after the conversion are omitted from the output.

Example:

var Transform = xform.ForEach(func(logger *zap.Logger, txn *wire.Transaction) {
    xform.DeleteEntity(txn.Changes, "old_entity")
    xform.DeleteFields(txn.Changes, "another_entity", "Field1", "Field2")
})

func ForEachWithState

func ForEachWithState(kinds convertdb.KindList, conv func(logger *zap.Logger, txn convertdb.Transaction) bool) Transformation

ForEachWithState is a shorthand for writing a typical stateful upgrade transformation.

It returns an upgrade transformation that takes a stateful conversion and applies it to each transaction in the stream. Transactions that end up empty after the conversion are omitted from the output. If true is returned by the conversion, txn.Source will be cleared so that the transaction can bypass producer checking. Read the doc of convertdb in limestone/converter/convertdb/doc.go

Example:

type foo struct {
     ufoo.Common
     ufoo.New
}

var (
    indexFleetID  = limestone.Index("FleetID")
    kindForwarder = convertdb.KindOf(forwarder{}, indexFleetID)
    kinds         = convertdb.KindList{kindForwarder}
)

var Transform = xform.ForEachWithState(func(logger *zap.Logger, txn convertdb.Transaction) bool {
     xform.DeleteEntity(txn.Other, "old_entity")
     xform.DeleteFields(txn.Other, "another_entity", "Field1", "Field2")

     var clearProducer bool
     for fooID, diff := range txn.Raw.Changes["foo"] {
          if diff["Field1"] != nil {
               var f foo
               limestone.MustGet(fooID, &f)
               f.Field2 = f.Field1 + f.Field3
               txn.Set(f)
               // Field1 is owned by service1 but Field2 is owned by service2, so producer has to be cleared
               clearProducer = true
          }
     }
     return clearProducer
})

Jump to

Keyboard shortcuts

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