buildSerializerLibrary function

Library buildSerializerLibrary(
  1. Set<ClassElement> builtClasses,
  2. String partDirectiveUrl,
  3. Set<Expression> additionalSerializers, {
  4. Expression? externalSerializers,
})

Implementation

Library buildSerializerLibrary(
  Set<ClassElement> builtClasses,
  String partDirectiveUrl,
  Set<Expression> additionalSerializers, {
  Expression? externalSerializers,
}) =>
    Library(
      (b) => b
        ..directives.add(Directive.part(partDirectiveUrl))
        ..body.addAll([
          declareFinal(
            "_serializersBuilder",
            type: refer(
                "SerializersBuilder", "package:built_value/serializer.dart"),
          )
              .assign(
                refer(r"_$serializers"),
              )
              .property("toBuilder")
              .call([])
              .withCustomSerializers(additionalSerializers)
              .applyIf(
                externalSerializers != null,
                (expr) => expr.cascade("addAll").call([externalSerializers!]),
              )
              .cascade("addPlugin")
              .call([
                refer(
                  "StandardJsonPlugin",
                  "package:built_value/standard_json_plugin.dart",
                ).call([])
              ])
              .statement,
          refer("@SerializersFor", "package:built_value/serializer.dart").call([
            literalList(
              builtClasses
                  .map<Reference>(
                    (c) => refer(c.name, c.source.uri.toString()),
                  )
                  .toList()
                ..sort((a, b) => a.symbol!.compareTo(b.symbol!)),
            )
          ]),
          declareFinal("serializers",
                  type: refer(
                      "Serializers", "package:built_value/serializer.dart"))
              .assign(refer("_serializersBuilder"))
              .property("build")
              .call([]).statement,
        ]),
    );