github.com/falafeljan/pkger@v0.18.0/internal/maps/infos.go (about)

     1  // Code generated by github.com/markbates/pkger/mapgen. DO NOT EDIT.
     2  
     3  package maps
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"sort"
     9  	"sync"
    10  
    11  	"github.com/markbates/pkger/here"
    12  )
    13  
    14  // Infos wraps sync.Map and uses the following types:
    15  // key:   string
    16  // value: here.Info
    17  type Infos struct {
    18  	data *sync.Map
    19  	once *sync.Once
    20  }
    21  
    22  func (m *Infos) Data() *sync.Map {
    23  	if m.once == nil {
    24  		m.once = &sync.Once{}
    25  	}
    26  	m.once.Do(func() {
    27  		if m.data == nil {
    28  			m.data = &sync.Map{}
    29  		}
    30  	})
    31  	return m.data
    32  }
    33  
    34  func (m *Infos) MarshalJSON() ([]byte, error) {
    35  	mm := map[string]interface{}{}
    36  	m.data.Range(func(key, value interface{}) bool {
    37  		mm[fmt.Sprintf("%s", key)] = value
    38  		return true
    39  	})
    40  	return json.Marshal(mm)
    41  }
    42  
    43  func (m *Infos) UnmarshalJSON(b []byte) error {
    44  	mm := map[string]here.Info{}
    45  
    46  	if err := json.Unmarshal(b, &mm); err != nil {
    47  		return err
    48  	}
    49  	for k, v := range mm {
    50  		m.Store(k, v)
    51  	}
    52  	return nil
    53  }
    54  
    55  // Delete the key from the map
    56  func (m *Infos) Delete(key string) {
    57  	m.Data().Delete(key)
    58  }
    59  
    60  // Load the key from the map.
    61  // Returns here.Info or bool.
    62  // A false return indicates either the key was not found
    63  // or the value is not of type here.Info
    64  func (m *Infos) Load(key string) (here.Info, bool) {
    65  	m.Data()
    66  	i, ok := m.data.Load(key)
    67  	if !ok {
    68  		return here.Info{}, false
    69  	}
    70  	s, ok := i.(here.Info)
    71  	return s, ok
    72  }
    73  
    74  // Range over the here.Info values in the map
    75  func (m *Infos) Range(f func(key string, value here.Info) bool) {
    76  	m.Data().Range(func(k, v interface{}) bool {
    77  		key, ok := k.(string)
    78  		if !ok {
    79  			return false
    80  		}
    81  		value, ok := v.(here.Info)
    82  		if !ok {
    83  			return false
    84  		}
    85  		return f(key, value)
    86  	})
    87  }
    88  
    89  // Store a here.Info in the map
    90  func (m *Infos) Store(key string, value here.Info) {
    91  	m.Data().Store(key, value)
    92  }
    93  
    94  // Keys returns a list of keys in the map
    95  func (m *Infos) Keys() []string {
    96  	var keys []string
    97  	m.Range(func(key string, value here.Info) bool {
    98  		keys = append(keys, key)
    99  		return true
   100  	})
   101  	sort.Strings(keys)
   102  	return keys
   103  }