DTO生成器

dtogen 是一个用于从json生成DTO和实体的命令行应用程序。

简要信息

dtogen --help

A command-line app which is used to generate DTOs and Entities from json.

Usage: dtogen [arguments]

Options:
    --no-from-json      Don't generate `fromJson` factory for DTO
    --no-to-json        Don't generate `toJson` method for DTO
    --no-entity         Don't generate Entity for DTO. Entity extends `Equatable` and uses `DateTime` instead of `String` with date
    --no-from-entity    Don't generate `fromEntity` factory for DTO
    --no-to-entity      Don't generate `toEntity` method for DTO
    --no-copy           Don't generate `CopyWith` annotation
-i, --input             Path to the json file
-o, --output            Path to the output dart file. Prints to console if not specified
    --init-class        Name of the root model
                        (defaults to "Generated")
-p, --prefix            Prefix for all generated class names

使用示例

我们有一个名为 shelf.json 的json文件。我们想生成一个模型,用于创建书架。该模型是书架领域的一部分。所以我们需要添加 CreateShelf 前缀。此外,我们不希望为DTO生成 fromJson 工厂和 toEntity

{
  "user_id": "string",
  "created_at": "2022-06-16",
  "updated_at": "2022-06-16",
  "names": ["string"],
  "books": [
    {
      "books_needed": 0,
      "store_locations": [
        {
          "city": "string",
          "state": "string",
          "country": "string"
        }
      ],
      "book_blueprint": {
        "amount": 0,
        "hours_to_reed": 0,
        "cost": 0,
        "description": "string",
        "currency": "usd",
        "payment_method": "card"
      }
    }
  ]
}

为了生成满足我们需求的模型,我们可以使用此命令

dtogen -i shelf.json -o shelf.dart -p CreateShelf --init-class Shelf --no-from-json --no-to-entity 

此命令将创建一个名为 shelf.dart 的文件

@CopyWith()
class CreateShelf extends Equatable {
  const CreateShelf({
    required this.userId,
    required this.createdAt,
    required this.updatedAt,
    required this.books,
  });

  final String userId;
  final DateTime createdAt;
  final DateTime updatedAt;
  final List<CreateShelfBook> books;

  @override
  List<Object?> get props => [
        userId,
        createdAt,
        updatedAt,
        books,
      ];
}

@JsonSerializable(fieldRename: FieldRename.snake, createFactory: false)
class CreateShelfDto {
  const CreateShelfDto({
    required this.userId,
    required this.createdAt,
    required this.updatedAt,
    required this.books,
  });

  factory CreateShelfDto.fromEntity(CreateShelf entity) {
    return CreateShelfDto(
      userId: entity.userId,
      createdAt: entity.createdAt.toString(),
      updatedAt: entity.updatedAt.toString(),
      books: entity.books.map(CreateShelfBookDto.fromEntity).toList(),
    );
  }

  final String userId;
  final String createdAt;
  final String updatedAt;
  final List<CreateShelfBookDto> books;

  Map<String, dynamic> toJson() => _$CreateShelfDtoToJson(this);
}

@CopyWith()
class CreateShelfBook extends Equatable {
  const CreateShelfBook({
    required this.booksNeeded,
    required this.storeLocations,
    required this.bookBlueprint,
  });

  final int booksNeeded;
  final List<CreateShelfStoreLocation> storeLocations;
  final CreateShelfBookBlueprint bookBlueprint;

  @override
  List<Object?> get props => [
        booksNeeded,
        storeLocations,
        bookBlueprint,
      ];
}

@JsonSerializable(fieldRename: FieldRename.snake, createFactory: false)
class CreateShelfBookDto {
  const CreateShelfBookDto({
    required this.booksNeeded,
    required this.storeLocations,
    required this.bookBlueprint,
  });

  factory CreateShelfBookDto.fromEntity(CreateShelfBook entity) {
    return CreateShelfBookDto(
      booksNeeded: entity.booksNeeded,
      storeLocations: entity.storeLocations.map(CreateShelfStoreLocationDto.fromEntity).toList(),
      bookBlueprint: CreateShelfBookBlueprintDto.fromEntity(entity.bookBlueprint),
    );
  }

  final int booksNeeded;
  final List<CreateShelfStoreLocationDto> storeLocations;
  final CreateShelfBookBlueprintDto bookBlueprint;

  Map<String, dynamic> toJson() => _$CreateShelfBookDtoToJson(this);
}

@CopyWith()
class CreateShelfBookBlueprint extends Equatable {
  const CreateShelfBookBlueprint({
    required this.amount,
    required this.hoursToReed,
    required this.cost,
    required this.description,
    required this.currency,
    required this.paymentMethod,
  });

  final int amount;
  final int hoursToReed;
  final int cost;
  final String description;
  final String currency;
  final String paymentMethod;

  @override
  List<Object?> get props => [
        amount,
        hoursToReed,
        cost,
        description,
        currency,
        paymentMethod,
      ];
}

@JsonSerializable(fieldRename: FieldRename.snake, createFactory: false)
class CreateShelfBookBlueprintDto {
  const CreateShelfBookBlueprintDto({
    required this.amount,
    required this.hoursToReed,
    required this.cost,
    required this.description,
    required this.currency,
    required this.paymentMethod,
  });

  factory CreateShelfBookBlueprintDto.fromEntity(CreateShelfBookBlueprint entity) {
    return CreateShelfBookBlueprintDto(
      amount: entity.amount,
      hoursToReed: entity.hoursToReed,
      cost: entity.cost,
      description: entity.description,
      currency: entity.currency,
      paymentMethod: entity.paymentMethod,
    );
  }

  final int amount;
  final int hoursToReed;
  final int cost;
  final String description;
  final String currency;
  final String paymentMethod;

  Map<String, dynamic> toJson() => _$CreateShelfBookBlueprintDtoToJson(this);
}

@CopyWith()
class CreateShelfStoreLocation extends Equatable {
  const CreateShelfStoreLocation({
    required this.city,
    required this.state,
    required this.country,
  });

  final String city;
  final String state;
  final String country;

  @override
  List<Object?> get props => [
        city,
        state,
        country,
      ];
}

@JsonSerializable(fieldRename: FieldRename.snake, createFactory: false)
class CreateShelfStoreLocationDto {
  const CreateShelfStoreLocationDto({
    required this.city,
    required this.state,
    required this.country,
  });

  factory CreateShelfStoreLocationDto.fromEntity(CreateShelfStoreLocation entity) {
    return CreateShelfStoreLocationDto(
      city: entity.city,
      state: entity.state,
      country: entity.country,
    );
  }

  final String city;
  final String state;
  final String country;

  Map<String, dynamic> toJson() => _$CreateShelfStoreLocationDtoToJson(this);
}

GitHub

查看 Github