github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/x/misc/must/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 := "must"
    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, "must.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 := []struct {
    37  		BuiltInType
    38  		Name string
    39  	}{
    40  		{BuiltInType: "byte"},
    41  		{BuiltInType: "[]byte", Name: "bytes"},
    42  		{BuiltInType: "string"},
    43  		{BuiltInType: "[]string", Name: "strings"},
    44  		{BuiltInType: "int"},
    45  		{BuiltInType: "int8"},
    46  		{BuiltInType: "int16"},
    47  		{BuiltInType: "int32"},
    48  		{BuiltInType: "int64"},
    49  		{BuiltInType: "uint8"},
    50  		{BuiltInType: "uint16"},
    51  		{BuiltInType: "uint32"},
    52  		{BuiltInType: "uint64"},
    53  		{BuiltInType: "rune"},
    54  		{BuiltInType: "float32"},
    55  		{BuiltInType: "float64"},
    56  	}
    57  
    58  	{
    59  		filename := filepath.Join(root, "must_generated.go")
    60  		file := NewFile(pkg, filename)
    61  		for _, t := range types {
    62  			name := ""
    63  			if t.Name != "" {
    64  				name += stringsx.UpperCamelCase(t.Name)
    65  			} else {
    66  				name += stringsx.UpperCamelCase(string(t.BuiltInType))
    67  			}
    68  			file.WriteSnippet(
    69  				Func(Var(t.BuiltInType, "v"), Var(Error, "err")).
    70  					Named(name).
    71  					Return(Var(t.BuiltInType)).
    72  					Do(
    73  						If(Exprer("err != nil")).
    74  							Do(
    75  								Call(file.Use("log", "Panic"), Ident("err")),
    76  							),
    77  						Return(Ident("v")),
    78  					),
    79  				Func(Var(t.BuiltInType, "v"), Var(Bool, "ok")).
    80  					Named(name+"OK").
    81  					Return(Var(t.BuiltInType)).
    82  					Do(
    83  						If(Exprer("!ok")).
    84  							Do(
    85  								Call(file.Use("log", "Panic"), Valuer(name+" not ok")),
    86  							),
    87  						Return(Ident("v")),
    88  					),
    89  			)
    90  		}
    91  
    92  		if _, err := file.Write(); err != nil {
    93  			log.Panic(err)
    94  		}
    95  	}
    96  }