github.com/ethereum/go-ethereum@v1.16.1/cmd/abigen/namefilter.go (about) 1 // Copyright 2022 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // go-ethereum 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "fmt" 21 "strings" 22 ) 23 24 type nameFilter struct { 25 fulls map[string]bool // path/to/contract.sol:Type 26 files map[string]bool // path/to/contract.sol:* 27 types map[string]bool // *:Type 28 } 29 30 func newNameFilter(patterns ...string) (*nameFilter, error) { 31 f := &nameFilter{ 32 fulls: make(map[string]bool), 33 files: make(map[string]bool), 34 types: make(map[string]bool), 35 } 36 for _, pattern := range patterns { 37 if err := f.add(pattern); err != nil { 38 return nil, err 39 } 40 } 41 return f, nil 42 } 43 44 func (f *nameFilter) add(pattern string) error { 45 ft := strings.Split(pattern, ":") 46 if len(ft) != 2 { 47 // filenames and types must not include ':' symbol 48 return fmt.Errorf("invalid pattern: %s", pattern) 49 } 50 51 file, typ := ft[0], ft[1] 52 if file == "*" { 53 f.types[typ] = true 54 return nil 55 } else if typ == "*" { 56 f.files[file] = true 57 return nil 58 } 59 f.fulls[pattern] = true 60 return nil 61 } 62 63 func (f *nameFilter) Matches(name string) bool { 64 ft := strings.Split(name, ":") 65 if len(ft) != 2 { 66 // If contract names are always of the fully-qualified form 67 // <filePath>:<type>, then this case will never happen. 68 return false 69 } 70 71 file, typ := ft[0], ft[1] 72 // full paths > file paths > types 73 return f.fulls[name] || f.files[file] || f.types[typ] 74 }