从单个 JSON 输入生成 dart 代码

发布于 2025-01-14 01:15:45 字数 635 浏览 2 评论 0原文

我正在尝试构建一个 dart 包,它从 OData JSON 表示文件生成 API 源代码。我尝试使用 source_gen,但它似乎是根据注释或现有源代码生成代码(例如,JSON 序列化程序为现有类生成代码)。

目标:根据 JSON 数据生成多个 dart 代码。例如:假设我的 JSON 是:

{
  "user": {
     "income": "Decimal", //Decimal is the type, which I translate it into dart type
  }
}

,我生成的代码将是:

user.dart

import "package:decimal/decimal.dart";

class User {

   final Decimal age;

   User(this.age);

}

I'm trying to build a dart package which generates API source code from OData JSON representation file. I tried using source_gen, but it appears to generate code based on annotations or existing source code (e.g. JSON serializer generated code for existing class).

Goal: Generate multiple dart codes based on JSON data. E.g.: Say my JSON is:

{
  "user": {
     "income": "Decimal", //Decimal is the type, which I translate it into dart type
  }
}

and my generated code would be:

user.dart:

import "package:decimal/decimal.dart";

class User {

   final Decimal age;

   User(this.age);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2025-01-21 01:15:48

当涉及到 API 描述时,你想要做的事情已经实现了。

您可以使用 .proto 或 swagger .yaml/ .json 文件中的类生成代码。

  1. gRPC API 生成原型文件

原型文件示例:

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

message SearchResponse {
 ...
}
  1. 从 .yml/json 文件生成 Swagger Rest api 代码

看:
img

What are you trying to do is already implemented, when it comes to API description.

You can generate code with classes from .proto or swagger .yaml/ .json files.

  1. Proto file generation for gRPC API

Proto file example:

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

message SearchResponse {
 ...
}
  1. Swagger rest api code generation from .yml/json files

Look:
img

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文