github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/gen/javascript/funcmap.go (about) 1 package javascript 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/webrpc/webrpc/gen" 9 "github.com/webrpc/webrpc/schema" 10 ) 11 12 var fieldTypeMap = map[schema.DataType]string{ 13 schema.T_Uint: "number", 14 schema.T_Uint8: "number", 15 schema.T_Uint16: "number", 16 schema.T_Uint32: "number", 17 schema.T_Uint64: "number", 18 schema.T_Int: "number", 19 schema.T_Int8: "number", 20 schema.T_Int16: "number", 21 schema.T_Int32: "number", 22 schema.T_Int64: "number", 23 schema.T_Float32: "number", 24 schema.T_Float64: "number", 25 schema.T_String: "string", 26 schema.T_Timestamp: "string", 27 schema.T_Null: "null", 28 schema.T_Any: "any", 29 schema.T_Byte: "string", 30 schema.T_Bool: "boolean", 31 } 32 33 func fieldConcreteType(in *schema.VarType) (string, error) { 34 switch in.Type { 35 case schema.T_Struct: 36 return in.Struct.Name, nil 37 default: 38 return "", nil 39 } 40 return "", fmt.Errorf("could not represent type: %#v", in) 41 } 42 43 func constPathPrefix(in schema.VarName) (string, error) { 44 return string(in) + "PathPrefix", nil 45 } 46 47 func methodName(in interface{}) string { 48 v, _ := downcaseName(in) 49 return v 50 } 51 52 func methodInputName(in *schema.MethodArgument) string { 53 name := string(in.Name) 54 if name != "" { 55 return name 56 } 57 if in.Type != nil { 58 return in.Type.String() 59 } 60 return "" 61 } 62 63 func methodInputs(in []*schema.MethodArgument) (string, error) { 64 inputs := []string{} 65 66 if len(in) > 0 { 67 inputs = append(inputs, fmt.Sprintf("args")) 68 } 69 70 inputs = append(inputs, "headers") 71 72 return strings.Join(inputs, ", "), nil 73 } 74 75 func isStruct(t schema.MessageType) bool { 76 return t == "struct" 77 } 78 79 func exportedField(in schema.VarName) (string, error) { 80 s := string(in) 81 return strings.ToUpper(s[0:1]) + s[1:], nil 82 } 83 84 func exportedJSONField(in schema.MessageField) (string, error) { 85 for i := range in.Meta { 86 for k := range in.Meta[i] { 87 if k == "json" { 88 s := strings.Split(fmt.Sprintf("%v", in.Meta[i][k]), ",") 89 if len(s) > 0 { 90 return s[0], nil 91 } 92 } 93 } 94 } 95 return string(in.Name), nil 96 } 97 98 func isEnum(t schema.MessageType) bool { 99 return t == "enum" 100 } 101 102 func downcaseName(v interface{}) (string, error) { 103 downFn := func(s string) string { 104 if s == "" { 105 return "" 106 } 107 return strings.ToLower(s[0:1]) + s[1:] 108 } 109 switch t := v.(type) { 110 case schema.VarName: 111 return downFn(string(t)), nil 112 case string: 113 return downFn(t), nil 114 default: 115 return "", errors.New("downcaseFieldName, unknown arg type") 116 } 117 } 118 119 func listComma(item int, count int) string { 120 if item+1 < count { 121 return ", " 122 } 123 return "" 124 } 125 126 func newOutputArgResponse(in *schema.MethodArgument) (string, error) { 127 z, err := fieldConcreteType(in.Type) 128 if err != nil { 129 return "", err 130 } 131 132 typ := "" 133 switch in.Type.Type { 134 case schema.T_Struct: 135 typ = fmt.Sprintf("new %s", z) 136 default: 137 typ = "" 138 // typ = fmt.Sprintf("<%s>", z) 139 } 140 141 line := fmt.Sprintf("%s: %s(_data.%s)", in.Name, typ, in.Name) 142 143 return line, nil 144 } 145 146 func exportKeyword(opts gen.TargetOptions) func() string { 147 return func() string { 148 if opts.Extra == "noexports" { 149 return "" 150 } else { 151 return "export " 152 } 153 } 154 } 155 156 func serverServiceName(in schema.VarName) (string, error) { 157 s := string(in) 158 return strings.ToLower(s[0:1]) + s[1:] + "Server", nil 159 } 160 161 func jsFieldType(in *schema.VarType) (string, error) { 162 switch in.Type { 163 case schema.T_Map: 164 return "object", nil 165 166 case schema.T_Struct: 167 return in.Struct.Name, nil 168 169 default: 170 if fieldTypeMap[in.Type] != "" { 171 return fieldTypeMap[in.Type], nil 172 } 173 } 174 175 return "", fmt.Errorf("could not represent type: %#v", in) 176 } 177 178 func serviceInterfaceName(in schema.VarName) (string, error) { 179 s := string(in) 180 return s, nil 181 } 182 183 func templateFuncMap(opts gen.TargetOptions) map[string]interface{} { 184 return map[string]interface{}{ 185 "constPathPrefix": constPathPrefix, 186 "methodName": methodName, 187 "methodInputs": methodInputs, 188 "isStruct": isStruct, 189 "isEnum": isEnum, 190 "listComma": listComma, 191 "exportedField": exportedField, 192 "exportedJSONField": exportedJSONField, 193 "newOutputArgResponse": newOutputArgResponse, 194 "exportKeyword": exportKeyword(opts), 195 "serverServiceName": serverServiceName, 196 "jsFieldType": jsFieldType, 197 "serviceInterfaceName": serviceInterfaceName, 198 } 199 }