github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/datas/tag.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package datas
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/dolthub/dolt/go/store/nomdl"
    21  	"github.com/dolthub/dolt/go/store/types"
    22  )
    23  
    24  const (
    25  	TagMetaField      = "meta"
    26  	TagCommitRefField = "ref"
    27  	TagName           = "Tag"
    28  )
    29  
    30  var tagTemplate = types.MakeStructTemplate(TagName, []string{TagMetaField, TagCommitRefField})
    31  
    32  // ref is a Ref<Commit>, but 'Commit' is not defined in this snippet.
    33  // Tag refs are validated to point at Commits during write.
    34  var valueTagType = nomdl.MustParseType(`Struct Tag {
    35          meta: Struct {},
    36          ref:  Ref<Value>,
    37  }`)
    38  
    39  // TagOptions is used to pass options into Tag.
    40  type TagOptions struct {
    41  	// Meta is a Struct that describes arbitrary metadata about this Tag,
    42  	// e.g. a timestamp or descriptive text.
    43  	Meta types.Struct
    44  }
    45  
    46  // NewTag creates a new tag object.
    47  //
    48  // A tag has the following type:
    49  //
    50  // ```
    51  // struct Tag {
    52  //   meta: M,
    53  //   commitRef: T,
    54  // }
    55  // ```
    56  // where M is a struct type and R is a ref type.
    57  func NewTag(_ context.Context, commitRef types.Ref, meta types.Struct) (types.Struct, error) {
    58  	return tagTemplate.NewStruct(meta.Format(), []types.Value{meta, commitRef})
    59  }
    60  
    61  func IsTag(v types.Value) (bool, error) {
    62  	if s, ok := v.(types.Struct); !ok {
    63  		return false, nil
    64  	} else {
    65  		return types.IsValueSubtypeOf(s.Format(), v, valueTagType)
    66  	}
    67  }
    68  
    69  func makeTagStructType(metaType, refType *types.Type) (*types.Type, error) {
    70  	return types.MakeStructType(TagName,
    71  		types.StructField{
    72  			Name: TagMetaField,
    73  			Type: metaType,
    74  		},
    75  		types.StructField{
    76  			Name: TagCommitRefField,
    77  			Type: refType,
    78  		},
    79  	)
    80  }