github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/gen/codegen/gen_utils.go (about)

     1  package codegen
     2  
     3  import (
     4  	"path"
     5  	"reflect"
     6  	"unicode"
     7  )
     8  
     9  func IsEmptyValue(rv reflect.Value) bool {
    10  	if !rv.IsValid() || !rv.CanInterface() {
    11  		return false
    12  	}
    13  
    14  	if chk, ok := rv.Interface().(interface{ IsZero() bool }); ok && chk.IsZero() {
    15  		return false
    16  	}
    17  
    18  	switch rv.Kind() {
    19  	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
    20  		return rv.Len() == 0
    21  	case reflect.Bool:
    22  		return !rv.Bool()
    23  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    24  		return rv.Int() == 0
    25  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    26  		return rv.Uint() == 0
    27  	case reflect.Float32, reflect.Float64:
    28  		return rv.Float() == 0
    29  	case reflect.Interface, reflect.Ptr:
    30  		return rv.IsNil()
    31  	}
    32  
    33  	return false
    34  }
    35  
    36  func IsValidIdent(s string) bool {
    37  	if len(s) == 0 {
    38  		return false
    39  	}
    40  	if IsReserved(s) {
    41  		return false
    42  	}
    43  	for _, c := range s {
    44  		if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' {
    45  			return false
    46  		}
    47  	}
    48  	return true
    49  }
    50  
    51  func IsReserved(s string) bool {
    52  	_, ok := ReservedKeys[s]
    53  	if ok {
    54  		return ok
    55  	}
    56  	_, ok = BuiltinFuncs[s]
    57  	return ok
    58  }
    59  
    60  func IsBuiltinFunc(s string) bool {
    61  	_, ok := BuiltinFuncs[s]
    62  	return ok
    63  }
    64  
    65  const (
    66  	Bool       BuiltInType = "bool"
    67  	Int        BuiltInType = "int"
    68  	Int8       BuiltInType = "int8"
    69  	Int16      BuiltInType = "int16"
    70  	Int32      BuiltInType = "int32"
    71  	Int64      BuiltInType = "int64"
    72  	Uint       BuiltInType = "uint"
    73  	Uint8      BuiltInType = "uint8"
    74  	Uint16     BuiltInType = "uint16"
    75  	Uint32     BuiltInType = "uint32"
    76  	Uint64     BuiltInType = "uint64"
    77  	Uintptr    BuiltInType = "uintptr"
    78  	Float32    BuiltInType = "float32"
    79  	Float64    BuiltInType = "float64"
    80  	Complex64  BuiltInType = "complex64"
    81  	Complex128 BuiltInType = "complex128"
    82  	String     BuiltInType = "string"
    83  	Byte       BuiltInType = "byte"
    84  	Rune       BuiltInType = "rune"
    85  	Error      BuiltInType = "error"
    86  )
    87  
    88  const (
    89  	Iota        SnippetBuiltIn = "iota"
    90  	True        SnippetBuiltIn = "true"
    91  	False       SnippetBuiltIn = "false"
    92  	Nil         SnippetBuiltIn = "nil"
    93  	Break       SnippetBuiltIn = "break"
    94  	Continue    SnippetBuiltIn = "continue"
    95  	Fallthrough SnippetBuiltIn = "fallthrough"
    96  )
    97  
    98  var (
    99  	ReservedKeys = map[string]bool{
   100  		"bool":        true,
   101  		"int":         true,
   102  		"int8":        true,
   103  		"int16":       true,
   104  		"int32":       true,
   105  		"int64":       true,
   106  		"uint":        true,
   107  		"uint8":       true,
   108  		"uint16":      true,
   109  		"uint32":      true,
   110  		"uint64":      true,
   111  		"uintptr":     true,
   112  		"float32":     true,
   113  		"float64":     true,
   114  		"complex64":   true,
   115  		"complex128":  true,
   116  		"string":      true,
   117  		"byte":        true,
   118  		"rune":        true,
   119  		"error":       true,
   120  		"iota":        true,
   121  		"true":        true,
   122  		"false":       true,
   123  		"nil":         true,
   124  		"break":       true,
   125  		"continue":    true,
   126  		"fallthrough": true,
   127  	}
   128  	BuiltinFuncs = map[string]bool{
   129  		"append":  true,
   130  		"complex": true,
   131  		"cap":     true,
   132  		"close":   true,
   133  		"copy":    true,
   134  		"delete":  true,
   135  		"imag":    true,
   136  		"len":     true,
   137  		"make":    true,
   138  		"new":     true,
   139  		"panic":   true,
   140  		"print":   true,
   141  		"println": true,
   142  		"real":    true,
   143  		"recover": true,
   144  	}
   145  )
   146  
   147  const AnonymousIdent SnippetIdent = "_"
   148  
   149  var naming = path.Base
   150  
   151  func SetPkgNaming(fn func(string) string) {
   152  	if fn != nil {
   153  		naming = fn
   154  	}
   155  }
   156  
   157  var (
   158  	Valuer = ValueWithAlias(naming)
   159  	Typer  = TypeWithAlias(naming)
   160  	Exprer = ExprWithAlias(naming)
   161  )
   162  
   163  func Stringify(s Snippet) string { return string(s.Bytes()) }