Line data Source code
1 : import 'package:class_fields/domain/class.dart'; 2 : import 'package:class_fields/domain/field.dart'; 3 : import 'package:class_fields/templates/template.dart'; 4 : import 'package:class_fields/util/string_buffer_ext.dart'; 5 : 6 : /// {@template fields_template} 7 : /// The template for the field keys 8 : /// {@endtemplate} 9 : class FieldsTemplate extends Template { 10 : /// {@macro fields_template} 11 2 : const FieldsTemplate(Class subject) : super(subject); 12 : 13 1 : @override 14 : void generate(StringBuffer buffer) { 15 1 : buffer.writeObject( 16 3 : 'class ${subject.genName}', 17 1 : body: () { 18 : buffer 19 4 : ..writeln('const ${subject.genName}();') 20 1 : ..writeln() 21 4 : ..writeAll(subject.fields.instanciators, '\n') 22 1 : ..writeln(); 23 : }, 24 : ); 25 : } 26 : } 27 : 28 : extension on Class { 29 1 : String get genName { 30 2 : return '_\$${name}Fields'; 31 : } 32 : } 33 : 34 : extension on Iterable<Field> { 35 1 : Iterable<String> get instanciators { 36 5 : return map((field) => "final ${field.cleanName} = '${field.key}';"); 37 : } 38 : }