/* NAME meta.go DESCRIPTION See Readme.md AUTHOR Saxon Nelson-Milton LICENSE meta.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. */ package mts import ( "errors" "fmt" "sync" ) type Meta struct { mu sync.RWMutex data map[string]string } // Add adds metadata with key and val, if already exists return error func (m *Meta) Add(key, val string) error { m.mu.Lock() if _, exists := m.data[key]; !exists { return errors.New(fmt.Sprintf("Metadata for: %v already exists", key)) } m.data[key] = val m.mu.Unlock() return nil } // All returns the a copy of the map containing the meta data func (m *Meta) All() map[string]string { m.mu.Lock() var cpy map[string]string for k, v := range m.data { cpy[k] = v } m.mu.Unlock() return cpy } // Get returns the meta data for the passed key func (m *Meta) Get(key string) (string, error) { m.mu.Lock() val, ok := m.data[key] m.mu.Unlock() if !ok { return "", errors.New("Key does not exist in metadata map") } return val, nil } // Remove deletes a meta entry in the map and returns error if it doesn’t exist func (m *Meta) Delete(key string) error { m.mu.Lock() defer m.mu.Unlock() if _, ok := m.data[key]; ok { delete(m.data, key) return nil } return errors.New("Trying to delete map entry that doesn't exist") } func (m *Meta) Format() []byte { // TODO: complete this return []byte("someData") }