Skip to content

Commit

Permalink
added dart tenplate
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Feb 19, 2024
1 parent 6967aa1 commit 96afe06
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
95 changes: 95 additions & 0 deletions otohttp/templates/client.dart.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Code generated by oto; DO NOT EDIT.

import 'package:json_annotation/json_annotation.dart';
import 'package:dio/dio.dart';

class Client {
final Dio httpClient;
final String endpoint;
final String bearerToken;

Client(
this.httpClient,
this.endpoint,
this.bearerToken,
);

// getAuthorizationHeader returns the value for the Authorization header.
String getAuthorizationHeader() {
return 'Bearer $bearerToken';
}
}

class Error {
@JsonKey(name: 'error')
final String errorMessage;

Error({
required this.errorMessage,
});

factory Error.fromJson(Map<String, dynamic> json) {
return Error(
errorMessage: json['error'],
);
}

Map<String, dynamic> toJson() {
return {
'error': errorMessage,
};
}
}

<%= for (service) in def.Services { %>
class <%= service.Name %>Service {
final Client Client;
<%= service.Name %>Service({
required this.Client,
});
<%= for (method) in service.Methods { %>
Future<<%= method.OutputObject.TypeName %>> <%= camelize_down(method.Name) %>(<%= method.InputObject.TypeName %> request) async {
final response = await Client.httpClient.post(
'${Client.endpoint}/<%= service.Name %>/<%= method.Name %>',
data: request.toJson(),
options: Options(headers: {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
'Agent': 'otohttp-dart-client/0.1.0',
'Authorization': Client.getAuthorizationHeader(),
})
);
if (response.statusCode != 200) {
throw Error.fromJson(response.data);
}
return <%= method.OutputObject.TypeName %>.fromJson(response.data);
}
<% } %>
}
<% } %>

<%= for (object) in def.Objects { %>
@JsonSerializable()
class <%= object.Name %> {
<%= for (field) in object.Fields { %>
@JsonKey(name: '<%= camelize_down(field.Name) %>')
final <%= raw(field.Type.DartType) %><%= if (!field.Metadata["required"]) { %>?<% } %> <%= camelize_down(field.Name) %>;
<% } %>
<%= object.Name %>(<%= if (len(object.Fields) > 0) { %>{<%= for (field) in object.Fields { %>
<%= if (field.Metadata["required"] == true) { %>required <% } %>this.<%= camelize_down(field.Name) %>,<% } %>
}<% } %>);

factory <%= object.Name %>.fromJson(Map<String, dynamic> json) {
return <%= object.Name %>(<%= for (field) in object.Fields { %>
<%= camelize_down(field.Name) %>: <%= if (field.Type.IsObject) { %> <%= field.Type.CleanObjectName %>.fromJson(json['<%= camelize_down(field.Name) %>']) <% } else { %> json['<%= camelize_down(field.Name) %>']<% } %>,<% } %>
);
}

Map<String, dynamic> toJson() {
return {<%= for (field) in object.Fields { %>
'<%= camelize_down(field.Name) %>': <%= camelize_down(field.Name) %>,<% } %>
};
}

}
<% } %>
8 changes: 8 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ type FieldType struct {
JSType string `json:"jsType"`
TSType string `json:"tsType"`
SwiftType string `json:"swiftType"`
DartType string `json:"dartType"`
}

// IsOptional returns true for pointer types (optional).
Expand Down Expand Up @@ -457,6 +458,7 @@ func (p *Parser) parseFieldType(pkg *packages.Package, obj types.Object) (FieldT
ftype.TSType = ftype.CleanObjectName
ftype.JSType = ftype.CleanObjectName
ftype.SwiftType = ftype.CleanObjectName
ftype.DartType = ftype.CleanObjectName
if ftype.IsObject {
ftype.JSType = "object"
//ftype.SwiftType = "Any"
Expand All @@ -466,24 +468,29 @@ func (p *Parser) parseFieldType(pkg *packages.Package, obj types.Object) (FieldT
ftype.JSType = "any"
ftype.SwiftType = "Any"
ftype.TSType = "object"
ftype.DartType = "dynamic"
case "map[string]interface{}":
ftype.JSType = "object"
ftype.TSType = "object"
ftype.SwiftType = "Any"
ftype.DartType = "Map<String, dynamic>"
case "string":
ftype.JSType = "string"
ftype.SwiftType = "String"
ftype.TSType = "string"
ftype.DartType = "String"
case "bool":
ftype.JSType = "boolean"
ftype.SwiftType = "Bool"
ftype.TSType = "boolean"
ftype.DartType = "bool"
case "int", "int16", "int32", "int64",
"uint", "uint16", "uint32", "uint64",
"float32", "float64":
ftype.JSType = "number"
ftype.SwiftType = "Double"
ftype.TSType = "number"
ftype.DartType = "double"
}
}

Expand All @@ -503,6 +510,7 @@ func (p *Parser) addOutputFields() error {
JSType: "string",
SwiftType: "String",
TSType: "string",
DartType: "String",
},
Metadata: map[string]interface{}{},
Example: "something went wrong",
Expand Down

0 comments on commit 96afe06

Please sign in to comment.