Line data Source code
1 : import 'package:enum_assist/src/configs/additional_extension_config.dart'; 2 : import 'package:enum_assist/src/util/enum_helpers.dart'; 3 : import 'package:enum_assist_annotation/enum_assist_annotation.dart'; 4 : import 'package:source_gen/source_gen.dart'; 5 : 6 : /// {@template enum_assist.class_config} 7 : /// Represents most values from [EnumAssist] when merged with local 8 : /// configuration. 9 : /// 10 : /// Values are all known, so types are non-nullable. 11 : /// {@endtemplate} 12 : 13 : class ClassConfig { 14 : /// {@macro enum_assist.class_config} 15 3 : const ClassConfig({ 16 : required this.createJsonConv, 17 : required this.serializedFormat, 18 : required this.useDocCommentAsDescription, 19 : required this.additionalExtensions, 20 : }); 21 : 22 : /// Merges [config] with [reader].annotation 23 : /// 24 : /// priority is given to `annotation` 25 0 : factory ClassConfig.mergeConfigs(ClassConfig config, ConstantReader reader) { 26 0 : final annotation = _getAnnotation(reader); 27 0 : final additionalExtensions = _getAdditionalExtensions(reader); 28 : 29 0 : return ClassConfig( 30 0 : createJsonConv: annotation.createJsonConv ?? config.createJsonConv, 31 0 : serializedFormat: annotation.serializedFormat ?? config.serializedFormat, 32 0 : useDocCommentAsDescription: annotation.useDocCommentAsDescription ?? 33 0 : config.useDocCommentAsDescription, 34 0 : additionalExtensions: additionalExtensions ?? config.additionalExtensions, 35 : ); 36 : } 37 : 38 : /// {@macro enum_assist_annotation.enum_assist.create_json_conv} 39 : final bool createJsonConv; 40 : 41 : /// {@macro enum_assist_annotation.enum_assist.serialized_format} 42 : final SerializedFormat serializedFormat; 43 : 44 : /// {@macro enum_assist_annotation.enum_assist.use_doc_comment_as_description} 45 : final bool useDocCommentAsDescription; 46 : 47 : /// {@macro enum_assist_annotation.enum_assist.additional_methods} 48 : final List<AdditionalExtensionConfig> additionalExtensions; 49 : 50 : /// all the default values for [ClassConfig] 51 : static const defaults = ClassConfig( 52 : createJsonConv: true, 53 : serializedFormat: SerializedFormat.none, 54 : useDocCommentAsDescription: true, 55 : additionalExtensions: <AdditionalExtensionConfig>[], 56 : ); 57 : 58 0 : @override 59 : String toString() { 60 : return ''' 61 : ClassConfig{ 62 0 : createJsonConv: $createJsonConv, 63 0 : serializedFormat: $serializedFormat, 64 0 : useDocCommentAsDescription: $useDocCommentAsDescription, 65 0 : additionalExtensions: $additionalExtensions 66 : } 67 0 : '''; 68 : } 69 : } 70 : 71 0 : List<AdditionalExtensionConfig>? _getAdditionalExtensions( 72 : ConstantReader reader) { 73 : final additionalExtensionsRaw = 74 0 : reader.peek('additionalExtensions')?.listValue; 75 : 76 : if (additionalExtensionsRaw == null) return null; 77 : 78 0 : final additionalExtensions = <AdditionalExtensionConfig>[]; 79 0 : for (final extension in additionalExtensionsRaw) { 80 0 : final entry = ConstantReader(extension); 81 : 82 : AdditionalExtensionConfig? config; 83 : try { 84 0 : config = AdditionalExtensionConfig.resolve(entry); 85 : } catch (e) { 86 0 : print('Error resolving extension:\n\nmessage: $e'); // ignore: avoid_print 87 : } 88 : if (config == null) continue; 89 : 90 0 : additionalExtensions.add(config); 91 : } 92 : return additionalExtensions; 93 : } 94 : 95 0 : EnumAssist _getAnnotation(ConstantReader reader) { 96 0 : final serializedFormatObject = reader.peek('serializedFormat')?.objectValue; 97 0 : final serializedFormat = getEnumFromDartObject( 98 : serializedFormatObject, 99 : SerializedFormat.values, 100 : ); 101 : 102 0 : final createJsonConv = reader.peek('createJsonConv')?.boolValue; 103 : 104 : final useDocCommentAsDescription = 105 0 : reader.peek('useDocCommentAsDescription')?.boolValue; 106 : 107 0 : return EnumAssist( 108 : createJsonConv: createJsonConv, 109 : serializedFormat: serializedFormat, 110 : useDocCommentAsDescription: useDocCommentAsDescription, 111 : ); 112 : }