github.com/chain5j/chain5j-pkg@v1.0.7/codec/rlp/rlpgen/main.go (about)

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