|
| 1 | +package gen |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/webrpc/webrpc/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func templateFuncMap(proto *schema.WebRPCSchema, opts TargetOptions) map[string]interface{} { |
| 12 | + return map[string]interface{}{ |
| 13 | + // Generic template functions. |
| 14 | + "constPathPrefix": constPathPrefix, |
| 15 | + "countMethods": countMethods, |
| 16 | + "commaIfLen": commaIfLen, |
| 17 | + "isStruct": isStruct, |
| 18 | + "isEnum": isEnum, |
| 19 | + "downcaseName": downcaseName, |
| 20 | + "listComma": listComma, |
| 21 | + |
| 22 | + // Golang specific template functions. |
| 23 | + "goServiceMethodName": goServiceMethodName, |
| 24 | + "goServiceMethodJSONName": goServiceMethodJSONName, |
| 25 | + "goHasGoFieldType": goHasGoFieldType(proto), |
| 26 | + "goFieldTags": goFieldTags, |
| 27 | + "goFieldType": goFieldType, |
| 28 | + "goFieldOptional": goFieldOptional, |
| 29 | + "goFieldTypeDef": goFieldTypeDef, |
| 30 | + "goNewClientServiceName": goNewClientServiceName, |
| 31 | + "goNewServerServiceName": goNewServerServiceName, |
| 32 | + "goClientServiceName": goClientServiceName, |
| 33 | + "goServerServiceName": goServerServiceName, |
| 34 | + "goMethodInputs": goMethodInputs, |
| 35 | + "goMethodOutputs": goMethodOutputs, |
| 36 | + "goMethodArgName": goMethodArgName, |
| 37 | + "goMethodArgType": goMethodArgType, |
| 38 | + "goMethodArgNames": goMethodArgNames, |
| 39 | + "goArgsList": goArgsList, |
| 40 | + "goExportedField": goExportedField, |
| 41 | + |
| 42 | + // TypeScript specific template functions. |
| 43 | + "tsFieldType": tsFieldType, |
| 44 | + "tsInterfaceName": tsInterfaceName, |
| 45 | + "tsMethodName": tsMethodName, |
| 46 | + "tsMethodInputs": tsMethodInputs, |
| 47 | + "tsMethodOutputs": tsMethodOutputs, |
| 48 | + "tsNewOutputArgResponse": tsNewOutputArgResponse, |
| 49 | + "tsMethodArgumentInputInterfaceName": tsMethodArgumentInputInterfaceName, |
| 50 | + "tsMethodArgumentOutputInterfaceName": tsMethodArgumentOutputInterfaceName, |
| 51 | + "tsServiceInterfaceName": tsServiceInterfaceName, |
| 52 | + "tsExportableField": tsExportableField, |
| 53 | + "tsExportedField": tsExportedField, |
| 54 | + "tsExportedJSONField": tsExportedJSONField, |
| 55 | + "jsFieldType": jsFieldType, |
| 56 | + |
| 57 | + // JavaScript specific template functions. |
| 58 | + "jsExportKeyword": jsExportKeyword(opts), |
| 59 | + "jsMethodName": jsMethodName, |
| 60 | + "jsMethodInputs": jsMethodInputs, |
| 61 | + "jsNewOutputArgResponse": jsNewOutputArgResponse, |
| 62 | + "jsServiceInterfaceName": jsServiceInterfaceName, |
| 63 | + "jsExportedJSONField": jsExportedJSONField, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func commaIfLen(in []*schema.MethodArgument) string { |
| 68 | + if len(in) > 0 { |
| 69 | + return "," |
| 70 | + } |
| 71 | + return "" |
| 72 | +} |
| 73 | + |
| 74 | +func listComma(item int, count int) string { |
| 75 | + if item+1 < count { |
| 76 | + return ", " |
| 77 | + } |
| 78 | + return "" |
| 79 | +} |
| 80 | + |
| 81 | +func isStruct(t schema.MessageType) bool { |
| 82 | + return t == "struct" |
| 83 | +} |
| 84 | + |
| 85 | +func isEnum(t schema.MessageType) bool { |
| 86 | + return t == "enum" |
| 87 | +} |
| 88 | + |
| 89 | +func downcaseName(v interface{}) (string, error) { |
| 90 | + downFn := func(s string) string { |
| 91 | + if s == "" { |
| 92 | + return "" |
| 93 | + } |
| 94 | + return strings.ToLower(s[0:1]) + s[1:] |
| 95 | + } |
| 96 | + switch t := v.(type) { |
| 97 | + case schema.VarName: |
| 98 | + return downFn(string(t)), nil |
| 99 | + case string: |
| 100 | + return downFn(t), nil |
| 101 | + default: |
| 102 | + return "", errors.New("downcaseFieldName, unknown arg type") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func constPathPrefix(in schema.VarName) (string, error) { |
| 107 | + return string(in) + "PathPrefix", nil |
| 108 | +} |
| 109 | + |
| 110 | +func countMethods(in []*schema.Method) (string, error) { |
| 111 | + return strconv.Itoa(len(in)), nil |
| 112 | +} |
0 commit comments