github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/rlp/rlpgen/main.go (about)

     1  // Copyright 2021 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 main
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"flag"
    23  	"fmt"
    24  	"go/types"
    25  	"io/ioutil"
    26  	"os"
    27  
    28  	"golang.org/x/tools/go/packages"
    29  )
    30  
    31  const pathOfPackageRLP = "github.com/ethereum/go-ethereum/rlp"
    32  
    33  func main() {
    34  	var (
    35  		pkgdir     = flag.String("dir", ".", "input package")
    36  		output     = flag.String("out", "-", "output file (default is stdout)")
    37  		genEncoder = flag.Bool("encoder", true, "generate EncodeRLP?")
    38  		genDecoder = flag.Bool("decoder", false, "generate DecodeRLP?")
    39  		typename   = flag.String("type", "", "type to generate methods for")
    40  	)
    41  	flag.Parse()
    42  
    43  	cfg := Config{
    44  		Dir:             *pkgdir,
    45  		Type:            *typename,
    46  		GenerateEncoder: *genEncoder,
    47  		GenerateDecoder: *genDecoder,
    48  	}
    49  	code, err := cfg.process()
    50  	if err != nil {
    51  		fatal(err)
    52  	}
    53  	if *output == "-" {
    54  		os.Stdout.Write(code)
    55  	} else if err := ioutil.WriteFile(*output, code, 0644); err != nil {
    56  		fatal(err)
    57  	}
    58  }
    59  
    60  func fatal(args ...interface{}) {
    61  	fmt.Fprintln(os.Stderr, args...)
    62  	os.Exit(1)
    63  }
    64  
    65  type Config struct {
    66  	Dir  string // input package directory
    67  	Type string
    68  
    69  	GenerateEncoder bool
    70  	GenerateDecoder bool
    71  }
    72  
    73  // process generates the Go code.
    74  func (cfg *Config) process() (code []byte, err error) {
    75  	// Load packages.
    76  	pcfg := &packages.Config{
    77  		Mode:       packages.NeedName | packages.NeedTypes | packages.NeedImports | packages.NeedDeps,
    78  		Dir:        cfg.Dir,
    79  		BuildFlags: []string{"-tags", "norlpgen"},
    80  	}
    81  	ps, err := packages.Load(pcfg, pathOfPackageRLP, ".")
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	if len(ps) == 0 {
    86  		return nil, fmt.Errorf("no Go package found in %s", cfg.Dir)
    87  	}
    88  	packages.PrintErrors(ps)
    89  
    90  	// Find the packages that were loaded.
    91  	var (
    92  		pkg        *types.Package
    93  		packageRLP *types.Package
    94  	)
    95  	for _, p := range ps {
    96  		if len(p.Errors) > 0 {
    97  			return nil, fmt.Errorf("package %s has errors", p.PkgPath)
    98  		}
    99  		if p.PkgPath == pathOfPackageRLP {
   100  			packageRLP = p.Types
   101  		} else {
   102  			pkg = p.Types
   103  		}
   104  	}
   105  	bctx := newBuildContext(packageRLP)
   106  
   107  	// Find the type and generate.
   108  	typ, err := lookupStructType(pkg.Scope(), cfg.Type)
   109  	if err != nil {
   110  		return nil, fmt.Errorf("can't find %s in %s: %v", typ, pkg, err)
   111  	}
   112  	code, err = bctx.generate(typ, cfg.GenerateEncoder, cfg.GenerateDecoder)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	// Add build comments.
   118  	// This is done here to avoid processing these lines with gofmt.
   119  	var header bytes.Buffer
   120  	fmt.Fprint(&header, "// Code generated by rlpgen. DO NOT EDIT.\n\n")
   121  	fmt.Fprint(&header, "//go:build !norlpgen\n")
   122  	fmt.Fprint(&header, "// +build !norlpgen\n\n")
   123  	return append(header.Bytes(), code...), nil
   124  }
   125  
   126  func lookupStructType(scope *types.Scope, name string) (*types.Named, error) {
   127  	typ, err := lookupType(scope, name)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	_, ok := typ.Underlying().(*types.Struct)
   132  	if !ok {
   133  		return nil, errors.New("not a struct type")
   134  	}
   135  	return typ, nil
   136  }
   137  
   138  func lookupType(scope *types.Scope, name string) (*types.Named, error) {
   139  	obj := scope.Lookup(name)
   140  	if obj == nil {
   141  		return nil, errors.New("no such identifier")
   142  	}
   143  	typ, ok := obj.(*types.TypeName)
   144  	if !ok {
   145  		return nil, errors.New("not a type")
   146  	}
   147  	return typ.Type().(*types.Named), nil
   148  }