github.com/bingoohuang/pkger@v0.0.0-20210127185155-a71b9df4c4c7/internal/maps/files.go (about)

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