github.com/jimmyx0x/go-ethereum@v1.10.28/cmd/abigen/namefilter.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type nameFilter struct {
     9  	fulls map[string]bool // path/to/contract.sol:Type
    10  	files map[string]bool // path/to/contract.sol:*
    11  	types map[string]bool // *:Type
    12  }
    13  
    14  func newNameFilter(patterns ...string) (*nameFilter, error) {
    15  	f := &nameFilter{
    16  		fulls: make(map[string]bool),
    17  		files: make(map[string]bool),
    18  		types: make(map[string]bool),
    19  	}
    20  	for _, pattern := range patterns {
    21  		if err := f.add(pattern); err != nil {
    22  			return nil, err
    23  		}
    24  	}
    25  	return f, nil
    26  }
    27  
    28  func (f *nameFilter) add(pattern string) error {
    29  	ft := strings.Split(pattern, ":")
    30  	if len(ft) != 2 {
    31  		// filenames and types must not include ':' symbol
    32  		return fmt.Errorf("invalid pattern: %s", pattern)
    33  	}
    34  
    35  	file, typ := ft[0], ft[1]
    36  	if file == "*" {
    37  		f.types[typ] = true
    38  		return nil
    39  	} else if typ == "*" {
    40  		f.files[file] = true
    41  		return nil
    42  	}
    43  	f.fulls[pattern] = true
    44  	return nil
    45  }
    46  
    47  func (f *nameFilter) Matches(name string) bool {
    48  	ft := strings.Split(name, ":")
    49  	if len(ft) != 2 {
    50  		// If contract names are always of the fully-qualified form
    51  		// <filePath>:<type>, then this case will never happen.
    52  		return false
    53  	}
    54  
    55  	file, typ := ft[0], ft[1]
    56  	// full paths > file paths > types
    57  	return f.fulls[name] || f.files[file] || f.types[typ]
    58  }