github.com/luckypickle/go-ethereum-vet@v1.14.2/signer/storage/storage.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  //
    17  
    18  package storage
    19  
    20  import (
    21  	"fmt"
    22  )
    23  
    24  type Storage interface {
    25  	// Put stores a value by key. 0-length keys results in no-op
    26  	Put(key, value string)
    27  	// Get returns the previously stored value, or the empty string if it does not exist or key is of 0-length
    28  	Get(key string) string
    29  }
    30  
    31  // EphemeralStorage is an in-memory storage that does
    32  // not persist values to disk. Mainly used for testing
    33  type EphemeralStorage struct {
    34  	data      map[string]string
    35  	namespace string
    36  }
    37  
    38  func (s *EphemeralStorage) Put(key, value string) {
    39  	if len(key) == 0 {
    40  		return
    41  	}
    42  	fmt.Printf("storage: put %v -> %v\n", key, value)
    43  	s.data[key] = value
    44  }
    45  
    46  func (s *EphemeralStorage) Get(key string) string {
    47  	if len(key) == 0 {
    48  		return ""
    49  	}
    50  	fmt.Printf("storage: get %v\n", key)
    51  	if v, exist := s.data[key]; exist {
    52  		return v
    53  	}
    54  	return ""
    55  }
    56  
    57  func NewEphemeralStorage() Storage {
    58  	s := &EphemeralStorage{
    59  		data: make(map[string]string),
    60  	}
    61  	return s
    62  }