Skip to content

Commit

Permalink
chore: refact dart code generation (#3726)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Nov 18, 2023
1 parent da67ea2 commit 9c1aa6d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
6 changes: 3 additions & 3 deletions tools/goctl/api/dartgen/genapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import '../data/{{with .Info}}{{getBaseName .Title}}{{end}}.dart';
{{range .Routes}}
/// --{{.Path}}--
///
/// 请求: {{with .RequestType}}{{.Name}}{{end}}
/// 返回: {{with .ResponseType}}{{.Name}}{{end}}
/// request: {{with .RequestType}}{{.Name}}{{end}}
/// response: {{with .ResponseType}}{{.Name}}{{end}}
Future {{pathToFuncName .Path}}( {{if ne .Method "get"}}{{with .RequestType}}{{.Name}} request,{{end}}{{end}}
{Function({{with .ResponseType}}{{.Name}}{{end}}) ok,
Function(String) fail,
Expand All @@ -35,7 +35,7 @@ import '../data/{{with .Service}}{{.Name}}{{end}}.dart';
/// {{.Name}}
{{range $i, $Route := .Routes}}
/// --{{.Path}}--
/// --{{.AtDoc}}--
///
/// request: {{with .RequestType}}{{.Name}}{{end}}
/// response: {{with .ResponseType}}{{.Name}}{{end}}
Future {{normalizeHandlerName .Handler}}(
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/dartgen/gendata.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class {{.Name}} {
{{else if isClassListType .Type.Name}}
((m['{{getPropertyFromMember .}}'] {{appendDefaultEmptyValue .Type.Name}}) as List<dynamic>).map((i) => {{getCoreType .Type.Name}}.fromJson(i)).toList()
{{else if isMapType .Type.Name}}
{{if isNumberType .Type.Name}}num{{else}}{{.Type.Name}}{{end}}.from(m['{{getPropertyFromMember .}}'] ?? {})
{{if isNumberType .Type.Name}}num{{else}}{{.Type.Name}}{{end}}.from(m['{{getPropertyFromMember .}}'] ?? {})
{{else}}
{{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}}
,{{end}}
Expand All @@ -64,7 +64,7 @@ class {{.Name}} {
{{if isDirectType .Type.Name}}
{{lowCamelCase .Name}}
{{else if isMapType .Type.Name}}
{{lowCamelCase .Name}}
{{lowCamelCase .Name}}
{{else if isClassListType .Type.Name}}
{{lowCamelCase .Name}}{{if isNullableType .Type.Name}}?{{end}}.map((i) => i{{if isListItemsNullable .Type.Name}}?{{end}}.toJson())
{{else}}
Expand Down
50 changes: 27 additions & 23 deletions tools/goctl/api/dartgen/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ package dartgen
import "text/template"

var funcMap = template.FuncMap{
"appendNullCoalescing": appendNullCoalescing,
"appendDefaultEmptyValue": appendDefaultEmptyValue,
"extractPositionalParamsFromPath": extractPositionalParamsFromPath,
"getBaseName": getBaseName,
"getCoreType": getCoreType,
"getPropertyFromMember": getPropertyFromMember,
"isDirectType": isDirectType,
"hasUrlPathParams": hasUrlPathParams,
"isAtomicListType": isAtomicListType,
"isAtomicType": isAtomicType,
"isNumberType": isNumberType,
"isDirectType": isDirectType,
"isClassListType": isClassListType,
"isAtomicListType": isAtomicListType,
"isListItemsNullable": isListItemsNullable,
"isMapType": isMapType,
"isNullableType": isNullableType,
"appendNullCoalescing": appendNullCoalescing,
"appendDefaultEmptyValue": appendDefaultEmptyValue,
"getCoreType": getCoreType,
"isNumberType": isNumberType,
"lowCamelCase": lowCamelCase,
"normalizeHandlerName": normalizeHandlerName,
"hasUrlPathParams": hasUrlPathParams,
"extractPositionalParamsFromPath": extractPositionalParamsFromPath,
"makeDartRequestUrlPath": makeDartRequestUrlPath,
"isMapType": isMapType,
"normalizeHandlerName": normalizeHandlerName,
}

const (
Expand All @@ -29,28 +29,32 @@ import 'dart:convert';
import '../vars/kv.dart';
import '../vars/vars.dart';
/// 发送POST请求.
/// Send GET request.
///
/// data:为你要post的结构体,我们会帮你转换成json字符串;
/// ok函数:请求成功的时候调用,fail函数:请求失败的时候会调用,eventually函数:无论成功失败都会调用
Future apiPost(String path, dynamic data,
/// ok: the function that will be called on success.
/// fail:the fuction that will be called on failure.
/// eventually:the function that will be called regardless of success or failure.
Future apiGet(String path,
{Map<String, String> header,
Function(Map<String, dynamic>) ok,
Function(String) fail,
Function eventually}) async {
await _apiRequest('POST', path, data,
await _apiRequest('GET', path, null,
header: header, ok: ok, fail: fail, eventually: eventually);
}
/// 发送GET请求.
/// Send POST request.
///
/// ok函数:请求成功的时候调用,fail函数:请求失败的时候会调用,eventually函数:无论成功失败都会调用
Future apiGet(String path,
/// data: the data to post, it will be marshaled to json automatically.
/// ok: the function that will be called on success.
/// fail:the fuction that will be called on failure.
/// eventually:the function that will be called regardless of success or failure.
Future apiPost(String path, dynamic data,
{Map<String, String> header,
Function(Map<String, dynamic>) ok,
Function(String) fail,
Function eventually}) async {
await _apiRequest('GET', path, null,
await _apiRequest('POST', path, data,
header: header, ok: ok, fail: fail, eventually: eventually);
}
Expand Down Expand Up @@ -213,11 +217,11 @@ Future _apiRequest(String method, String path, dynamic data,
}`

tokensFileContent = `class Tokens {
/// 用于访问的token, 每次请求都必须带在Header里面
/// the token used to access, it must be carried in the header of each request
final String accessToken;
final int accessExpire;
/// 用于刷新token
/// the token used to refresh
final String refreshToken;
final int refreshExpire;
final int refreshAfter;
Expand Down Expand Up @@ -248,11 +252,11 @@ Future _apiRequest(String method, String path, dynamic data,
`

tokensFileContentV2 = `class Tokens {
/// 用于访问的token, 每次请求都必须带在Header里面
/// the token used to access, it must be carried in the header of each request
final String accessToken;
final int accessExpire;
/// 用于刷新token
/// the token used to refresh
final String refreshToken;
final int refreshExpire;
final int refreshAfter;
Expand Down

0 comments on commit 9c1aa6d

Please sign in to comment.