chore: refact dart code generation (#3726)

This commit is contained in:
Kevin Wan 2023-11-18 22:41:26 +08:00 committed by GitHub
parent da67ea2300
commit 9c1aa6da3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 38 deletions

View File

@ -15,8 +15,8 @@ import '../data/{{with .Info}}{{getBaseName .Title}}{{end}}.dart';
{{range .Routes}} {{range .Routes}}
/// --{{.Path}}-- /// --{{.Path}}--
/// ///
/// 请求: {{with .RequestType}}{{.Name}}{{end}} /// request: {{with .RequestType}}{{.Name}}{{end}}
/// 返回: {{with .ResponseType}}{{.Name}}{{end}} /// response: {{with .ResponseType}}{{.Name}}{{end}}
Future {{pathToFuncName .Path}}( {{if ne .Method "get"}}{{with .RequestType}}{{.Name}} request,{{end}}{{end}} Future {{pathToFuncName .Path}}( {{if ne .Method "get"}}{{with .RequestType}}{{.Name}} request,{{end}}{{end}}
{Function({{with .ResponseType}}{{.Name}}{{end}}) ok, {Function({{with .ResponseType}}{{.Name}}{{end}}) ok,
Function(String) fail, Function(String) fail,
@ -35,7 +35,7 @@ import '../data/{{with .Service}}{{.Name}}{{end}}.dart';
/// {{.Name}} /// {{.Name}}
{{range $i, $Route := .Routes}} {{range $i, $Route := .Routes}}
/// --{{.Path}}-- /// --{{.Path}}--
/// --{{.AtDoc}}-- ///
/// request: {{with .RequestType}}{{.Name}}{{end}} /// request: {{with .RequestType}}{{.Name}}{{end}}
/// response: {{with .ResponseType}}{{.Name}}{{end}} /// response: {{with .ResponseType}}{{.Name}}{{end}}
Future {{normalizeHandlerName .Handler}}( Future {{normalizeHandlerName .Handler}}(

View File

@ -52,7 +52,7 @@ class {{.Name}} {
{{else if isClassListType .Type.Name}} {{else if isClassListType .Type.Name}}
((m['{{getPropertyFromMember .}}'] {{appendDefaultEmptyValue .Type.Name}}) as List<dynamic>).map((i) => {{getCoreType .Type.Name}}.fromJson(i)).toList() ((m['{{getPropertyFromMember .}}'] {{appendDefaultEmptyValue .Type.Name}}) as List<dynamic>).map((i) => {{getCoreType .Type.Name}}.fromJson(i)).toList()
{{else if isMapType .Type.Name}} {{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}} {{else}}
{{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}} {{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}}
,{{end}} ,{{end}}
@ -64,7 +64,7 @@ class {{.Name}} {
{{if isDirectType .Type.Name}} {{if isDirectType .Type.Name}}
{{lowCamelCase .Name}} {{lowCamelCase .Name}}
{{else if isMapType .Type.Name}} {{else if isMapType .Type.Name}}
{{lowCamelCase .Name}} {{lowCamelCase .Name}}
{{else if isClassListType .Type.Name}} {{else if isClassListType .Type.Name}}
{{lowCamelCase .Name}}{{if isNullableType .Type.Name}}?{{end}}.map((i) => i{{if isListItemsNullable .Type.Name}}?{{end}}.toJson()) {{lowCamelCase .Name}}{{if isNullableType .Type.Name}}?{{end}}.map((i) => i{{if isListItemsNullable .Type.Name}}?{{end}}.toJson())
{{else}} {{else}}

View File

@ -3,24 +3,24 @@ package dartgen
import "text/template" import "text/template"
var funcMap = template.FuncMap{ var funcMap = template.FuncMap{
"getBaseName": getBaseName,
"getPropertyFromMember": getPropertyFromMember,
"isDirectType": isDirectType,
"isAtomicType": isAtomicType,
"isNumberType": isNumberType,
"isClassListType": isClassListType,
"isAtomicListType": isAtomicListType,
"isListItemsNullable": isListItemsNullable,
"isNullableType": isNullableType,
"appendNullCoalescing": appendNullCoalescing, "appendNullCoalescing": appendNullCoalescing,
"appendDefaultEmptyValue": appendDefaultEmptyValue, "appendDefaultEmptyValue": appendDefaultEmptyValue,
"getCoreType": getCoreType,
"lowCamelCase": lowCamelCase,
"normalizeHandlerName": normalizeHandlerName,
"hasUrlPathParams": hasUrlPathParams,
"extractPositionalParamsFromPath": extractPositionalParamsFromPath, "extractPositionalParamsFromPath": extractPositionalParamsFromPath,
"makeDartRequestUrlPath": makeDartRequestUrlPath, "getBaseName": getBaseName,
"getCoreType": getCoreType,
"getPropertyFromMember": getPropertyFromMember,
"hasUrlPathParams": hasUrlPathParams,
"isAtomicListType": isAtomicListType,
"isAtomicType": isAtomicType,
"isDirectType": isDirectType,
"isClassListType": isClassListType,
"isListItemsNullable": isListItemsNullable,
"isMapType": isMapType, "isMapType": isMapType,
"isNullableType": isNullableType,
"isNumberType": isNumberType,
"lowCamelCase": lowCamelCase,
"makeDartRequestUrlPath": makeDartRequestUrlPath,
"normalizeHandlerName": normalizeHandlerName,
} }
const ( const (
@ -29,22 +29,11 @@ import 'dart:convert';
import '../vars/kv.dart'; import '../vars/kv.dart';
import '../vars/vars.dart'; import '../vars/vars.dart';
/// 发送POST请求. /// Send GET request.
/// ///
/// data:为你要post的结构体我们会帮你转换成json字符串; /// ok: the function that will be called on success.
/// ok函数:请求成功的时候调用fail函数请求失败的时候会调用eventually函数无论成功失败都会调用 /// failthe fuction that will be called on failure.
Future apiPost(String path, dynamic data, /// eventuallythe function that will be called regardless of success or failure.
{Map<String, String> header,
Function(Map<String, dynamic>) ok,
Function(String) fail,
Function eventually}) async {
await _apiRequest('POST', path, data,
header: header, ok: ok, fail: fail, eventually: eventually);
}
/// 发送GET请求.
///
/// ok函数:请求成功的时候调用fail函数请求失败的时候会调用eventually函数无论成功失败都会调用
Future apiGet(String path, Future apiGet(String path,
{Map<String, String> header, {Map<String, String> header,
Function(Map<String, dynamic>) ok, Function(Map<String, dynamic>) ok,
@ -54,6 +43,21 @@ Future apiGet(String path,
header: header, ok: ok, fail: fail, eventually: eventually); header: header, ok: ok, fail: fail, eventually: eventually);
} }
/// Send POST request.
///
/// data: the data to post, it will be marshaled to json automatically.
/// ok: the function that will be called on success.
/// failthe fuction that will be called on failure.
/// eventuallythe 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('POST', path, data,
header: header, ok: ok, fail: fail, eventually: eventually);
}
Future _apiRequest(String method, String path, dynamic data, Future _apiRequest(String method, String path, dynamic data,
{Map<String, String> header, {Map<String, String> header,
Function(Map<String, dynamic>) ok, Function(Map<String, dynamic>) ok,
@ -213,11 +217,11 @@ Future _apiRequest(String method, String path, dynamic data,
}` }`
tokensFileContent = `class Tokens { tokensFileContent = `class Tokens {
/// 用于访问的token, 每次请求都必须带在Header里面 /// the token used to access, it must be carried in the header of each request
final String accessToken; final String accessToken;
final int accessExpire; final int accessExpire;
/// 用于刷新token /// the token used to refresh
final String refreshToken; final String refreshToken;
final int refreshExpire; final int refreshExpire;
final int refreshAfter; final int refreshAfter;
@ -248,11 +252,11 @@ Future _apiRequest(String method, String path, dynamic data,
` `
tokensFileContentV2 = `class Tokens { tokensFileContentV2 = `class Tokens {
/// 用于访问的token, 每次请求都必须带在Header里面 /// the token used to access, it must be carried in the header of each request
final String accessToken; final String accessToken;
final int accessExpire; final int accessExpire;
/// 用于刷新token /// the token used to refresh
final String refreshToken; final String refreshToken;
final int refreshExpire; final int refreshExpire;
final int refreshAfter; final int refreshAfter;