github.com/uber/kraken@v0.1.4/lib/store/metadata/metadata.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 "regexp" 17 18 // Metadata defines types of matadata file. 19 // All implementations of Metadata must register themselves. 20 type Metadata interface { 21 GetSuffix() string 22 Movable() bool 23 Serialize() ([]byte, error) 24 Deserialize([]byte) error 25 } 26 27 var _factories = make(map[*regexp.Regexp]Factory) 28 29 // Factory creates Metadata objects given suffix. 30 type Factory interface { 31 Create(suffix string) Metadata 32 } 33 34 // Register registers new Factory with corresponding suffix regexp. 35 func Register(suffix *regexp.Regexp, factory Factory) { 36 _factories[suffix] = factory 37 } 38 39 // CreateFromSuffix creates a Metadata obj based on suffix. 40 // This is not a very efficient method; It's mostly used during reload. 41 func CreateFromSuffix(suffix string) Metadata { 42 for re, factory := range _factories { 43 if re.MatchString(suffix) { 44 return factory.Create(suffix) 45 } 46 } 47 return nil 48 }