github.com/searKing/golang/go@v1.2.117/encoding/internal/tag/tagFunc.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package tag
     6  
     7  import (
     8  	"reflect"
     9  	"sync"
    10  )
    11  
    12  type tagFunc func(e *tagState, v reflect.Value, opts tagOpts) (isUserDefined bool)
    13  
    14  // map[reflect.Type]tagFunc
    15  type tagFuncMap struct {
    16  	tagFns sync.Map
    17  }
    18  
    19  func (t *tagFuncMap) Store(reflectType reflect.Type, fn tagFunc) {
    20  	t.tagFns.Store(reflectType, fn)
    21  }
    22  
    23  func (t *tagFuncMap) LoadOrStore(reflectType reflect.Type, fn tagFunc) (tagFunc, bool) {
    24  	actual, loaded := t.tagFns.LoadOrStore(reflectType, fn)
    25  	if actual == nil {
    26  		return nil, loaded
    27  	}
    28  	return actual.(tagFunc), loaded
    29  }
    30  
    31  func (t *tagFuncMap) Load(reflectType reflect.Type) (tagFunc, bool) {
    32  	fn, ok := t.tagFns.Load(reflectType)
    33  	if fn == nil {
    34  		return nil, ok
    35  	}
    36  	return fn.(tagFunc), ok
    37  }
    38  
    39  func (t *tagFuncMap) Delete(reflectType reflect.Type) {
    40  	t.tagFns.Delete(reflectType)
    41  }
    42  
    43  func (t *tagFuncMap) Range(f func(reflectType reflect.Type, fn tagFunc) bool) {
    44  	t.tagFns.Range(func(reflectType, fn any) bool {
    45  		return f(reflectType.(reflect.Type), fn.(tagFunc))
    46  	})
    47  }