github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/batch.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  	"crypto/sha256"
    22  	"database/sql/driver"
    23  	"encoding/json"
    24  
    25  	"github.com/kaleido-io/firefly/internal/i18n"
    26  )
    27  
    28  type Batch struct {
    29  	ID         *UUID        `json:"id"`
    30  	Namespace  string       `json:"namespace"`
    31  	Type       MessageType  `json:"type"`
    32  	Author     string       `json:"author"`
    33  	Group      *Bytes32     `jdon:"group,omitempty"`
    34  	Hash       *Bytes32     `json:"hash"`
    35  	Created    *FFTime      `json:"created"`
    36  	Confirmed  *FFTime      `json:"confirmed"`
    37  	Payload    BatchPayload `json:"payload"`
    38  	PayloadRef *Bytes32     `json:"payloadRef,omitempty"`
    39  }
    40  
    41  type BatchPayload struct {
    42  	TX       TransactionRef `json:"tx"`
    43  	Messages []*Message     `json:"messages"`
    44  	Data     []*Data        `json:"data"`
    45  }
    46  
    47  // Value implements sql.Valuer
    48  func (ma BatchPayload) Value() (driver.Value, error) {
    49  	return json.Marshal(&ma)
    50  }
    51  
    52  func (ma *BatchPayload) Hash() *Bytes32 {
    53  	b, _ := json.Marshal(&ma)
    54  	var b32 Bytes32 = sha256.Sum256(b)
    55  	return &b32
    56  }
    57  
    58  // Scan implements sql.Scanner
    59  func (ma *BatchPayload) Scan(src interface{}) error {
    60  	switch src := src.(type) {
    61  	case nil:
    62  		return nil
    63  
    64  	case string, []byte:
    65  		if src == "" {
    66  			return nil
    67  		}
    68  		return json.Unmarshal(src.([]byte), &ma)
    69  
    70  	default:
    71  		return i18n.NewError(context.Background(), i18n.MsgScanFailed, src, ma)
    72  	}
    73  
    74  }