github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/datatype.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  
    22  	"github.com/kaleido-io/firefly/internal/i18n"
    23  )
    24  
    25  type ValidatorType = LowerCasedType
    26  
    27  const (
    28  	// ValidatorTypeJSON is the validator type for JSON Schema validation
    29  	ValidatorTypeJSON ValidatorType = "json"
    30  	// ValidatorTypeSystemDefinition is the validator type for system definitions
    31  	ValidatorTypeSystemDefinition ValidatorType = "definition"
    32  )
    33  
    34  // Datatype is the structure defining a data definition, such as a JSON schema
    35  type Datatype struct {
    36  	ID        *UUID         `json:"id,omitempty"`
    37  	Message   *UUID         `json:"message,omitempty"`
    38  	Validator ValidatorType `json:"validator"`
    39  	Namespace string        `json:"namespace,omitempty"`
    40  	Name      string        `json:"name,omitempty"`
    41  	Version   string        `json:"version,omitempty"`
    42  	Hash      *Bytes32      `json:"hash,omitempty"`
    43  	Created   *FFTime       `json:"created,omitempty"`
    44  	Value     Byteable      `json:"value,omitempty"`
    45  }
    46  
    47  func (dt *Datatype) Validate(ctx context.Context, existing bool) (err error) {
    48  	if dt.Validator != ValidatorTypeJSON {
    49  		return i18n.NewError(ctx, i18n.MsgUnknownFieldValue, "validator", dt.Validator)
    50  	}
    51  	if err = ValidateFFNameField(ctx, dt.Namespace, "namespace"); err != nil {
    52  		return err
    53  	}
    54  	if err = ValidateFFNameField(ctx, dt.Name, "name"); err != nil {
    55  		return err
    56  	}
    57  	if err = ValidateFFNameField(ctx, dt.Version, "version"); err != nil {
    58  		return err
    59  	}
    60  	if len(dt.Value) == 0 {
    61  		return i18n.NewError(ctx, i18n.MsgMissingRequiredField, "value")
    62  	}
    63  	if existing {
    64  		if dt.ID == nil {
    65  			return i18n.NewError(ctx, i18n.MsgNilID)
    66  		}
    67  		hash := dt.Value.Hash()
    68  		if dt.Hash == nil || *dt.Hash != *hash {
    69  			return i18n.NewError(ctx, i18n.MsgDataInvalidHash, hash, dt.Hash)
    70  		}
    71  	}
    72  	return nil
    73  }
    74  
    75  func (dt *Datatype) Topic() string {
    76  	return namespaceTopic(dt.Namespace)
    77  }
    78  
    79  func (dt *Datatype) SetBroadcastMessage(msgID *UUID) {
    80  	dt.Message = msgID
    81  }