github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/rlp/typecache.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rlp
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"strings"
    23  	"sync"
    24  )
    25  
    26  var (
    27  	typeCacheMutex sync.RWMutex
    28  	typeCache      = make(map[typekey]*typeinfo)
    29  )
    30  
    31  type typeinfo struct {
    32  	decoder    decoder
    33  	decoderErr error // error from makeDecoder
    34  	writer     writer
    35  	writerErr  error // error from makeWriter
    36  }
    37  
    38  // represents struct tags
    39  type tags struct {
    40  	// rlp:"nil" controls whether empty input results in a nil pointer.
    41  	nilOK bool
    42  	// rlp:"tail" controls whether this field swallows additional list
    43  	// elements. It can only be set for the last field, which must be
    44  	// of slice type.
    45  	tail bool
    46  	// rlp:"-" ignores fields.
    47  	ignored bool
    48  }
    49  
    50  type typekey struct {
    51  	reflect.Type
    52  	// the key must include the struct tags because they
    53  	// might generate a different decoder.
    54  	tags
    55  }
    56  
    57  type decoder func(*Stream, reflect.Value) error
    58  
    59  type writer func(reflect.Value, *encbuf) error
    60  
    61  func cachedDecoder(typ reflect.Type) (decoder, error) {
    62  	info := cachedTypeInfo(typ, tags{})
    63  	return info.decoder, info.decoderErr
    64  }
    65  
    66  func cachedWriter(typ reflect.Type) (writer, error) {
    67  	info := cachedTypeInfo(typ, tags{})
    68  	return info.writer, info.writerErr
    69  }
    70  
    71  func cachedTypeInfo(typ reflect.Type, tags tags) *typeinfo {
    72  	typeCacheMutex.RLock()
    73  	info := typeCache[typekey{typ, tags}]
    74  	typeCacheMutex.RUnlock()
    75  	if info != nil {
    76  		return info
    77  	}
    78  	// not in the cache, need to generate info for this type.
    79  	typeCacheMutex.Lock()
    80  	defer typeCacheMutex.Unlock()
    81  	return cachedTypeInfo1(typ, tags)
    82  }
    83  
    84  func cachedTypeInfo1(typ reflect.Type, tags tags) *typeinfo {
    85  	key := typekey{typ, tags}
    86  	info := typeCache[key]
    87  	if info != nil {
    88  		// another goroutine got the write lock first
    89  		return info
    90  	}
    91  	// put a dummy value into the cache before generating.
    92  	// if the generator tries to lookup itself, it will get
    93  	// the dummy value and won't call itself recursively.
    94  	info = new(typeinfo)
    95  	typeCache[key] = info
    96  	info.generate(typ, tags)
    97  	return info
    98  }
    99  
   100  type field struct {
   101  	index int
   102  	info  *typeinfo
   103  }
   104  
   105  func structFields(typ reflect.Type) (fields []field, err error) {
   106  	lastPublic := lastPublicField(typ)
   107  	for i := 0; i < typ.NumField(); i++ {
   108  		if f := typ.Field(i); f.PkgPath == "" { // exported
   109  			tags, err := parseStructTag(typ, i, lastPublic)
   110  			if err != nil {
   111  				return nil, err
   112  			}
   113  			if tags.ignored {
   114  				continue
   115  			}
   116  			info := cachedTypeInfo1(f.Type, tags)
   117  			fields = append(fields, field{i, info})
   118  		}
   119  	}
   120  	return fields, nil
   121  }
   122  
   123  func parseStructTag(typ reflect.Type, fi, lastPublic int) (tags, error) {
   124  	f := typ.Field(fi)
   125  	var ts tags
   126  	for _, t := range strings.Split(f.Tag.Get("rlp"), ",") {
   127  		switch t = strings.TrimSpace(t); t {
   128  		case "":
   129  		case "-":
   130  			ts.ignored = true
   131  		case "nil":
   132  			ts.nilOK = true
   133  		case "tail":
   134  			ts.tail = true
   135  			if fi != lastPublic {
   136  				return ts, fmt.Errorf(`rlp: invalid struct tag "tail" for %v.%s (must be on last field)`, typ, f.Name)
   137  			}
   138  			if f.Type.Kind() != reflect.Slice {
   139  				return ts, fmt.Errorf(`rlp: invalid struct tag "tail" for %v.%s (field type is not slice)`, typ, f.Name)
   140  			}
   141  		default:
   142  			return ts, fmt.Errorf("rlp: unknown struct tag %q on %v.%s", t, typ, f.Name)
   143  		}
   144  	}
   145  	return ts, nil
   146  }
   147  
   148  func lastPublicField(typ reflect.Type) int {
   149  	last := 0
   150  	for i := 0; i < typ.NumField(); i++ {
   151  		if typ.Field(i).PkgPath == "" {
   152  			last = i
   153  		}
   154  	}
   155  	return last
   156  }
   157  
   158  func (i *typeinfo) generate(typ reflect.Type, tags tags) {
   159  	i.decoder, i.decoderErr = makeDecoder(typ, tags)
   160  	i.writer, i.writerErr = makeWriter(typ, tags)
   161  }
   162  
   163  func isUint(k reflect.Kind) bool {
   164  	return k >= reflect.Uint && k <= reflect.Uintptr
   165  }