github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/subscription.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package fftypes
    18  
    19  import (
    20  	"context"
    21  	"database/sql/driver"
    22  	"encoding/json"
    23  
    24  	"github.com/kaleido-io/firefly/internal/i18n"
    25  )
    26  
    27  // SubscriptionFilter contains regular expressions to match against events. All must match for an event to be dispatched to a subscription
    28  type SubscriptionFilter struct {
    29  	Events string `json:"events,omitempty"`
    30  	Topics string `json:"topics,omitempty"`
    31  	Tag    string `json:"tag,omitempty"`
    32  	Group  string `json:"group,omitempty"`
    33  }
    34  
    35  // SubOptsFirstEvent picks the first event that should be dispatched on the subscription, and can be a string containing an exact sequence as well as one of the enum values
    36  type SubOptsFirstEvent string
    37  
    38  const (
    39  	// SubOptsFirstEventOldest indicates all events should be dispatched to the subscription
    40  	SubOptsFirstEventOldest SubOptsFirstEvent = "oldest"
    41  	// SubOptsFirstEventNewest indicates only newly received events should be dispatched to the subscription
    42  	SubOptsFirstEventNewest SubOptsFirstEvent = "newest"
    43  )
    44  
    45  // SubscriptionOptions cutomize the behavior of subscriptions
    46  type SubscriptionOptions struct {
    47  	FirstEvent *SubOptsFirstEvent `json:"firstEvent,omitempty"`
    48  	ReadAhead  *uint16            `json:"readAhead,omitempty"`
    49  }
    50  
    51  // SubscriptionRef are the fields that can be used to refer to a subscription
    52  type SubscriptionRef struct {
    53  	ID        *UUID  `json:"id"`
    54  	Namespace string `json:"namespace"`
    55  	Name      string `json:"name"`
    56  }
    57  
    58  // Subscription is a binding between the stream of events within a namespace, and an event interface - such as an application listening on websockets
    59  type Subscription struct {
    60  	SubscriptionRef
    61  
    62  	Transport string              `json:"transport"`
    63  	Filter    SubscriptionFilter  `json:"filter"`
    64  	Options   SubscriptionOptions `json:"options"`
    65  	Ephemeral bool                `json:"ephemeral,omitempty"`
    66  	Created   *FFTime             `json:"created"`
    67  }
    68  
    69  // Scan implements sql.Scanner
    70  func (so *SubscriptionOptions) Scan(src interface{}) error {
    71  	switch src := src.(type) {
    72  	case []byte:
    73  		return json.Unmarshal(src, &so)
    74  	case string:
    75  		return json.Unmarshal([]byte(src), &so)
    76  
    77  	default:
    78  		return i18n.NewError(context.Background(), i18n.MsgScanFailed, src, so)
    79  	}
    80  
    81  }
    82  
    83  // Value implements sql.Valuer
    84  func (so SubscriptionOptions) Value() (driver.Value, error) {
    85  	return json.Marshal(&so)
    86  }