github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/x/misc/clone/internal/main/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  
     9  	. "github.com/machinefi/w3bstream/pkg/depends/gen/codegen"
    10  	"github.com/machinefi/w3bstream/pkg/depends/x/stringsx"
    11  )
    12  
    13  func main() {
    14  	pkg := "clone"
    15  	_, path, _, _ := runtime.Caller(0)
    16  	root := filepath.Join(filepath.Dir(path), "../..")
    17  
    18  	if name := filepath.Base(root); name != pkg {
    19  		log.Panicf("wrong execute location: \n\tpath: %s\n\tbase: %s", root, name)
    20  	}
    21  
    22  	{
    23  		filename := filepath.Join(root, pkg+".go")
    24  		_, err := os.Stat(filename)
    25  		if err != nil && !os.IsExist(err) {
    26  			file := NewFile(pkg, filename)
    27  			file.WriteSnippet(
    28  				Comments("pls add your assert function here, or add type and re-generate"),
    29  			)
    30  			if _, err := file.Write(); err != nil {
    31  				log.Panic(err)
    32  			}
    33  		}
    34  	}
    35  
    36  	types := []BuiltInType{
    37  		Byte,
    38  		String,
    39  		Int,
    40  		Int8,
    41  		Int16,
    42  		Int32,
    43  		Int64,
    44  		Uint,
    45  		Uint8,
    46  		Uint16,
    47  		Uint32,
    48  		Uint64,
    49  		Rune,
    50  		Float32,
    51  		Float64,
    52  	}
    53  
    54  	{
    55  		filename := filepath.Join(root, pkg+"_generated.go")
    56  		file := NewFile(pkg, filename)
    57  		for _, t := range types {
    58  			fn := stringsx.UpperCamelCase(string(t)) + "s"
    59  			st := Slice(t)
    60  			file.WriteSnippet(
    61  				Func(Var(st, "orig")).
    62  					Named(fn).
    63  					Return(Var(st)).
    64  					Do(
    65  						Define(Ident("cloned")).
    66  							By(
    67  								Call("make", st, Call("len", Ident("orig"))),
    68  							),
    69  						Call("copy", Ident("cloned"), Ident("orig")),
    70  						Return(Ident("cloned")),
    71  					),
    72  			)
    73  		}
    74  
    75  		if _, err := file.Write(); err != nil {
    76  			log.Panic(err)
    77  		}
    78  	}
    79  }