github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/internal/tpl/util.go (about)

     1  package tpl
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"regexp"
     7  	"strings"
     8  )
     9  
    10  func goType(t reflect.Type) string {
    11  	return goTypeRecursive("", t)
    12  }
    13  
    14  func goTypeRecursive(prefix string, t reflect.Type) string {
    15  	// we need a hacky fix to handle correctly json.RawMessage and kit.RawMessage in auto-generated code
    16  	// of the stubs
    17  	switch t.String() {
    18  	case "json.RawMessage":
    19  		return fmt.Sprintf("%s%s", prefix, "kit.JSONMessage")
    20  	case "kit.RawMessage":
    21  		return fmt.Sprintf("%s%s", prefix, "kit.Message")
    22  	}
    23  
    24  	//nolint:exhaustive
    25  	switch t.Kind() {
    26  	case reflect.Slice:
    27  		prefix += "[]"
    28  
    29  		return goTypeRecursive(prefix, t.Elem())
    30  	case reflect.Array:
    31  		prefix += fmt.Sprintf("[%d]", t.Len())
    32  
    33  		return goTypeRecursive(prefix, t.Elem())
    34  	case reflect.Ptr:
    35  		prefix += "*"
    36  
    37  		return goTypeRecursive(prefix, t.Elem())
    38  	case reflect.Interface:
    39  		in := t.Name()
    40  		if in == "" {
    41  			in = "any"
    42  		}
    43  
    44  		return fmt.Sprintf("%s%s", prefix, in)
    45  	case reflect.Struct:
    46  		return fmt.Sprintf("%s%s", prefix, t.Name())
    47  	case reflect.Map:
    48  		return fmt.Sprintf("map[%s]%s", goTypeRecursive("", t.Key()), goTypeRecursive("", t.Elem()))
    49  	default:
    50  		return fmt.Sprintf("%s%s", prefix, t.Kind().String())
    51  	}
    52  }
    53  
    54  func tsType(t reflect.Type) string {
    55  	return tsTypeRecursive("", t, "")
    56  }
    57  
    58  func tsTypeRecursive(prefix string, t reflect.Type, postfix string) string {
    59  	// we need a hacky fix to handle correctly json.RawMessage and kit.RawMessage in auto-generated code
    60  	// of the stubs
    61  	switch t.String() {
    62  	case "json.RawMessage":
    63  		return fmt.Sprintf("%s%s", prefix, "any")
    64  	case "kit.RawMessage":
    65  		return fmt.Sprintf("%s%s", prefix, "any")
    66  	}
    67  
    68  	//nolint:exhaustive
    69  	switch t.Kind() {
    70  	case reflect.Slice:
    71  		postfix += "[]"
    72  
    73  		return tsTypeRecursive(prefix, t.Elem(), postfix)
    74  	case reflect.Array:
    75  		postfix += fmt.Sprintf("[%d]", t.Len())
    76  
    77  		return tsTypeRecursive(prefix, t.Elem(), postfix)
    78  	case reflect.Ptr:
    79  		return tsTypeRecursive(prefix, t.Elem(), postfix)
    80  	case reflect.Struct:
    81  		return fmt.Sprintf("%s%s%s", prefix, t.Name(), postfix)
    82  	case reflect.Map:
    83  		return fmt.Sprintf("{[key: %s]: %s}",
    84  			tsTypeRecursive("", t.Key(), ""),
    85  			tsTypeRecursive("", t.Elem(), ""),
    86  		)
    87  	case reflect.Interface:
    88  		return fmt.Sprintf("%s%s%s", prefix, "any", postfix)
    89  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
    90  		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
    91  		reflect.Float32, reflect.Float64:
    92  		return fmt.Sprintf("%s%s%s", prefix, "number", postfix)
    93  	case reflect.Bool:
    94  		return fmt.Sprintf("%s%s%s", prefix, "boolean", postfix)
    95  	default:
    96  		return fmt.Sprintf("%s%s%s", prefix, t.Kind().String(), postfix)
    97  	}
    98  }
    99  
   100  func strAppend(arr []string, elem string) []string {
   101  	return append(arr, elem)
   102  }
   103  
   104  func strEmptySlice() []string {
   105  	return []string{}
   106  }
   107  
   108  var pathParamRegEX = regexp.MustCompile(`{([^}]+)}`)
   109  
   110  func tsReplacePathParams(path string, prefix string) string {
   111  	return pathParamRegEX.ReplaceAllStringFunc(path, func(s string) string {
   112  		return fmt.Sprintf(`${%s%s}`, prefix, strings.Trim(s, "{}"))
   113  	})
   114  }