github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/data.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 "encoding/json" 23 "fmt" 24 25 "github.com/kaleido-io/firefly/internal/i18n" 26 ) 27 28 type DataRef struct { 29 ID *UUID `json:"id,omitempty"` 30 Hash *Bytes32 `json:"hash,omitempty"` 31 } 32 33 type Data struct { 34 ID *UUID `json:"id,omitempty"` 35 Validator ValidatorType `json:"validator"` 36 Blobstore bool `json:"blobstore,omitempty"` 37 Namespace string `json:"namespace,omitempty"` 38 Hash *Bytes32 `json:"hash,omitempty"` 39 Created *FFTime `json:"created,omitempty"` 40 Datatype *DatatypeRef `json:"datatype,omitempty"` 41 Value Byteable `json:"value"` 42 } 43 44 type DatatypeRef struct { 45 Name string `json:"name,omitempty"` 46 Version string `json:"version,omitempty"` 47 } 48 49 func (dr *DatatypeRef) String() string { 50 if dr == nil { 51 return "null" 52 } 53 return fmt.Sprintf("%s/%s", dr.Name, dr.Version) 54 } 55 56 type DataRefs []*DataRef 57 58 func (d DataRefs) Hash() *Bytes32 { 59 b, _ := json.Marshal(&d) 60 var b32 Bytes32 = sha256.Sum256(b) 61 return &b32 62 } 63 64 func (d *Data) Seal(ctx context.Context) error { 65 if d.Value == nil || d.Value.String() == "null" { 66 return i18n.NewError(ctx, i18n.MsgDataValueIsNull) 67 } 68 if d.ID == nil { 69 d.ID = NewUUID() 70 } 71 if d.Created == nil { 72 d.Created = Now() 73 } 74 d.Hash = d.Value.Hash() 75 return nil 76 }