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

     1  package desc
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  )
     7  
     8  func typ(prefix string, t reflect.Type) string {
     9  	// we need a hacky fix to handle correctly json.RawMessage and kit.RawMessage in auto-generated code
    10  	// of the stubs
    11  	switch t.String() {
    12  	case "json.RawMessage":
    13  		return fmt.Sprintf("%s%s", prefix, "kit.JSONMessage")
    14  	case "kit.RawMessage":
    15  		return fmt.Sprintf("%s%s", prefix, "kit.Message")
    16  	}
    17  
    18  	//nolint:exhaustive
    19  	switch t.Kind() {
    20  	case reflect.Slice:
    21  		prefix += "[]"
    22  
    23  		return typ(prefix, t.Elem())
    24  	case reflect.Array:
    25  		prefix += fmt.Sprintf("[%d]", t.Len())
    26  
    27  		return typ(prefix, t.Elem())
    28  	case reflect.Ptr:
    29  		prefix += "*"
    30  
    31  		return typ(prefix, t.Elem())
    32  	case reflect.Interface:
    33  		in := t.Name()
    34  		if in == "" {
    35  			in = "any"
    36  		}
    37  
    38  		return fmt.Sprintf("%s%s", prefix, in)
    39  	case reflect.Struct:
    40  		return fmt.Sprintf("%s%s", prefix, t.Name())
    41  	case reflect.Map:
    42  		return fmt.Sprintf("map[%s]%s", typ("", t.Key()), typ("", t.Elem()))
    43  	default:
    44  		return fmt.Sprintf("%s%s", prefix, t.Kind().String())
    45  	}
    46  }