...

Source file src/github.com/aws/smithy-go/document/internal/serde/field_cache.go

Documentation: github.com/aws/smithy-go/document/internal/serde

     1  package serde
     2  
     3  import (
     4  	"strings"
     5  	"sync"
     6  )
     7  
     8  var fieldCache fieldCacher
     9  
    10  type fieldCacher struct {
    11  	cache sync.Map
    12  }
    13  
    14  func (c *fieldCacher) Load(t interface{}) (*CachedFields, bool) {
    15  	if v, ok := c.cache.Load(t); ok {
    16  		return v.(*CachedFields), true
    17  	}
    18  	return nil, false
    19  }
    20  
    21  func (c *fieldCacher) LoadOrStore(t interface{}, fs *CachedFields) (*CachedFields, bool) {
    22  	v, ok := c.cache.LoadOrStore(t, fs)
    23  	return v.(*CachedFields), ok
    24  }
    25  
    26  // CachedFields is a cache entry for a type's fields.
    27  type CachedFields struct {
    28  	fields       []Field
    29  	fieldsByName map[string]int
    30  }
    31  
    32  // All returns all the fields for the cached type.
    33  func (f *CachedFields) All() []Field {
    34  	return f.fields
    35  }
    36  
    37  // FieldByName retrieves a field by name.
    38  func (f *CachedFields) FieldByName(name string) (Field, bool) {
    39  	if i, ok := f.fieldsByName[name]; ok {
    40  		return f.fields[i], ok
    41  	}
    42  	for _, f := range f.fields {
    43  		if strings.EqualFold(f.Name, name) {
    44  			return f, true
    45  		}
    46  	}
    47  	return Field{}, false
    48  }
    49  

View as plain text