github.com/uber/kraken@v0.1.4/lib/store/metadata/persist.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, 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  package metadata
    15  
    16  import (
    17  	"regexp"
    18  	"strconv"
    19  )
    20  
    21  const _persistSuffix = "_persist"
    22  
    23  func init() {
    24  	Register(regexp.MustCompile(_persistSuffix), &persistFactory{})
    25  }
    26  
    27  type persistFactory struct{}
    28  
    29  func (f persistFactory) Create(suffix string) Metadata {
    30  	return &Persist{}
    31  }
    32  
    33  // Persist marks whether a blob should be persisted.
    34  type Persist struct {
    35  	Value bool
    36  }
    37  
    38  // NewPersist creates a new Persist, where true means the blob
    39  // should be persisted, and false means the blob is safe to delete.
    40  func NewPersist(v bool) *Persist {
    41  	return &Persist{v}
    42  }
    43  
    44  // GetSuffix returns a static suffix.
    45  func (m *Persist) GetSuffix() string {
    46  	return _persistSuffix
    47  }
    48  
    49  // Movable is true.
    50  func (m *Persist) Movable() bool {
    51  	return true
    52  }
    53  
    54  // Serialize converts m to bytes.
    55  func (m *Persist) Serialize() ([]byte, error) {
    56  	return []byte(strconv.FormatBool(m.Value)), nil
    57  }
    58  
    59  // Deserialize loads b into m.
    60  func (m *Persist) Deserialize(b []byte) error {
    61  	v, err := strconv.ParseBool(string(b))
    62  	if err != nil {
    63  		return err
    64  	}
    65  	m.Value = v
    66  	return nil
    67  }