macro_kit 0.0.13 copy "macro_kit: ^0.0.13" to clipboard
macro_kit: ^0.0.13 copied to clipboard

A development-time macro system for Dart that generates code instantly for fast iteration without build_runner.

example/main.dart

import 'package:macro_kit/macro.dart';

part 'main.g.dart';

void main() async {
  await runMacro(
    macros: {
      'DataClassMacro': DataClassMacro.initialize,
    },
    enabled: true,
  );

  final profile = UserProfile(name: 'Rebaz', age: 30);
  print(
    [
      profile.toJson(),
      profile.toString(),
      profile.copyWith(name: 'Rebe'),
    ].join('\n'),
  );

  print('-----');

  Animal animal = Cat(nickName: 'Niki', name: 'Lala');
  print(
    [
      animal.toJsonBy(),
      animal.copyWithBy(cat: (value) => value.copyWith(name: 'Nik')),
    ].join('\n'),
  );

  print('-----');

  Cat cat = CatData.fromJson(animal.toJsonBy());
  print(
    [
      cat.toJson(),
      cat.copyWith(name: 'Myaw'),
    ].join('\n'),
  );

  print('-----');

  final dog = Dog(big: true, name: 'Iby');
  final dog2 = AnimalData.fromJson(dog.toJson());
  print(dog2 == dog);
}

@dataClassMacro
class UserProfile with UserProfileData {
  UserProfile({required this.name, required this.age});

  final String name;
  final int age;
}

@Macro(DataClassMacro())
sealed class Animal with AnimalData {
  Animal({required this.name});

  final String name;
}

@Macro(DataClassMacro(includeDiscriminator: true))
class Cat extends Animal with CatData {
  Cat({required this.nickName, required super.name});

  final String nickName;
}

@Macro(DataClassMacro(discriminatorValue: 'its_dog'))
class Dog extends Animal with DogData {
  Dog({required this.big, required super.name});

  final bool big;
}

@Macro(DataClassMacro(discriminatorValue: 'bow'))
class Cow extends Animal with DogData {
  Cow({required this.big, required super.name});

  final bool big;
}
10
likes
135
points
387
downloads

Publisher

verified publisherswiftybase.com

Weekly Downloads

A development-time macro system for Dart that generates code instantly for fast iteration without build_runner.

Repository (GitHub)
View/report issues

Topics

#codegen #macro #builder #development #tooling

Documentation

API reference

License

MIT (license)

Dependencies

analyzer, analyzer_plugin, change_case, collection, dart_style, hashlib, http, logging, meta, path, shelf, shelf_router, shelf_web_socket, source_helper, synchronized, watcher, web_socket_channel, yaml

More

Packages that depend on macro_kit