github.com/klaytn/klaytn@v1.12.1/rlp/rlpgen/main.go (about)

     1  // Modifications Copyright 2022 The klaytn Authors
     2  // Copyright 2021 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from rlp/rlpgen/main.go(2022/05/19)
    19  // Modified and improved for the klaytn development.
    20  
    21  package main
    22  
    23  import (
    24  	"bytes"
    25  	"errors"
    26  	"flag"
    27  	"fmt"
    28  	"go/types"
    29  	"os"
    30  
    31  	"golang.org/x/tools/go/packages"
    32  )
    33  
    34  const pathOfPackageRLP = "github.com/klaytn/klaytn/rlp"
    35  
    36  func main() {
    37  	var (
    38  		pkgdir     = flag.String("dir", ".", "input package")
    39  		output     = flag.String("out", "-", "output file (default is stdout)")
    40  		genEncoder = flag.Bool("encoder", true, "generate EncodeRLP?")
    41  		genDecoder = flag.Bool("decoder", false, "generate DecodeRLP?")
    42  		typename   = flag.String("type", "", "type to generate methods for")
    43  	)
    44  	flag.Parse()
    45  
    46  	cfg := Config{
    47  		Dir:             *pkgdir,
    48  		Type:            *typename,
    49  		GenerateEncoder: *genEncoder,
    50  		GenerateDecoder: *genDecoder,
    51  	}
    52  	code, err := cfg.process()
    53  	if err != nil {
    54  		fatal(err)
    55  	}
    56  	if *output == "-" {
    57  		os.Stdout.Write(code)
    58  	} else if err := os.WriteFile(*output, code, 0o644); err != nil {
    59  		fatal(err)
    60  	}
    61  }
    62  
    63  func fatal(args ...interface{}) {
    64  	fmt.Fprintln(os.Stderr, args...)
    65  	os.Exit(1)
    66  }
    67  
    68  type Config struct {
    69  	Dir  string // input package directory
    70  	Type string
    71  
    72  	GenerateEncoder bool
    73  	GenerateDecoder bool
    74  }
    75  
    76  // process generates the Go code.
    77  func (cfg *Config) process() (code []byte, err error) {
    78  	// Load packages.
    79  	pcfg := &packages.Config{
    80  		Mode:       packages.NeedName | packages.NeedTypes | packages.NeedImports | packages.NeedDeps,
    81  		Dir:        cfg.Dir,
    82  		BuildFlags: []string{"-tags", "norlpgen"},
    83  	}
    84  	ps, err := packages.Load(pcfg, pathOfPackageRLP, ".")
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	if len(ps) == 0 {
    89  		return nil, fmt.Errorf("no Go package found in %s", cfg.Dir)
    90  	}
    91  	packages.PrintErrors(ps)
    92  
    93  	// Find the packages that were loaded.
    94  	var (
    95  		pkg        *types.Package
    96  		packageRLP *types.Package
    97  	)
    98  	for _, p := range ps {
    99  		if len(p.Errors) > 0 {
   100  			return nil, fmt.Errorf("package %s has errors", p.PkgPath)
   101  		}
   102  		if p.PkgPath == pathOfPackageRLP {
   103  			packageRLP = p.Types
   104  		} else {
   105  			pkg = p.Types
   106  		}
   107  	}
   108  	bctx := newBuildContext(packageRLP)
   109  
   110  	// Find the type and generate.
   111  	typ, err := lookupStructType(pkg.Scope(), cfg.Type)
   112  	if err != nil {
   113  		return nil, fmt.Errorf("can't find %s in %s: %v", typ, pkg, err)
   114  	}
   115  	code, err = bctx.generate(typ, cfg.GenerateEncoder, cfg.GenerateDecoder)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	// Add build comments.
   121  	// This is done here to avoid processing these lines with gofmt.
   122  	var header bytes.Buffer
   123  	fmt.Fprint(&header, "// Code generated by rlpgen. DO NOT EDIT.\n\n")
   124  	fmt.Fprint(&header, "//go:build !norlpgen\n")
   125  	fmt.Fprint(&header, "// +build !norlpgen\n\n")
   126  	return append(header.Bytes(), code...), nil
   127  }
   128  
   129  func lookupStructType(scope *types.Scope, name string) (*types.Named, error) {
   130  	typ, err := lookupType(scope, name)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	_, ok := typ.Underlying().(*types.Struct)
   135  	if !ok {
   136  		return nil, errors.New("not a struct type")
   137  	}
   138  	return typ, nil
   139  }
   140  
   141  func lookupType(scope *types.Scope, name string) (*types.Named, error) {
   142  	obj := scope.Lookup(name)
   143  	if obj == nil {
   144  		return nil, errors.New("no such identifier")
   145  	}
   146  	typ, ok := obj.(*types.TypeName)
   147  	if !ok {
   148  		return nil, errors.New("not a type")
   149  	}
   150  	return typ.Type().(*types.Named), nil
   151  }