github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/plugins/plugcmds/plug_map.go (about)

     1  //go:generate mapgen -name "plug" -zero "plug{}" -go-type "plug" -pkg "" -a "nil" -b "nil" -c "nil" -bb "nil" -destination "plugcmds"
     2  // Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
     3  
     4  package plugcmds
     5  
     6  import (
     7  	"sort"
     8  	"sync"
     9  )
    10  
    11  // plugMap wraps sync.Map and uses the following types:
    12  // key:   string
    13  // value: plug
    14  type plugMap struct {
    15  	data sync.Map
    16  }
    17  
    18  // Delete the key from the map
    19  func (m *plugMap) Delete(key string) {
    20  	m.data.Delete(key)
    21  }
    22  
    23  // Load the key from the map.
    24  // Returns plug or bool.
    25  // A false return indicates either the key was not found
    26  // or the value is not of type plug
    27  func (m *plugMap) Load(key string) (plug, bool) {
    28  	i, ok := m.data.Load(key)
    29  	if !ok {
    30  		return plug{}, false
    31  	}
    32  	s, ok := i.(plug)
    33  	return s, ok
    34  }
    35  
    36  // LoadOrStore will return an existing key or
    37  // store the value if not already in the map
    38  func (m *plugMap) LoadOrStore(key string, value plug) (plug, bool) {
    39  	i, _ := m.data.LoadOrStore(key, value)
    40  	s, ok := i.(plug)
    41  	return s, ok
    42  }
    43  
    44  // Range over the plug values in the map
    45  func (m *plugMap) Range(f func(key string, value plug) bool) {
    46  	m.data.Range(func(k, v interface{}) bool {
    47  		key, ok := k.(string)
    48  		if !ok {
    49  			return false
    50  		}
    51  		value, ok := v.(plug)
    52  		if !ok {
    53  			return false
    54  		}
    55  		return f(key, value)
    56  	})
    57  }
    58  
    59  // Store a plug in the map
    60  func (m *plugMap) Store(key string, value plug) {
    61  	m.data.Store(key, value)
    62  }
    63  
    64  // Keys returns a list of keys in the map
    65  func (m *plugMap) Keys() []string {
    66  	var keys []string
    67  	m.Range(func(key string, value plug) bool {
    68  		keys = append(keys, key)
    69  		return true
    70  	})
    71  	sort.Strings(keys)
    72  	return keys
    73  }