diff --git a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.dart b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.dart index 57d30436d9..e1b6bac485 100644 --- a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.dart +++ b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.dart @@ -1,9 +1,8 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import 'package:aws_common/aws_common.dart'; +import 'package:amplify_core/amplify_core.dart'; import 'package:aws_logging_cloudwatch/aws_logging_cloudwatch.dart'; -import 'package:json_annotation/json_annotation.dart'; part 'plugin_config.g.dart'; @@ -63,27 +62,8 @@ class LoggingConstraint with AWSDebuggable { factory LoggingConstraint.fromJson(Map json) => _$LoggingConstraintFromJson(json); - // /// Converts an [LoggingConstraint] instance to a [Map]. - // Map toJson() => _$LoggingConstraintToJson(this); - /// Converts an [LoggingConstraint] instance to a [Map]. - Map toJson() { - final jsonMap = { - 'defaultLogLevel': - defaultLogLevel.toString().split('.').last, // Convert enum to string - 'categoryLogLevel': categoryLogLevel?.map( - (key, value) => MapEntry(key, value.toString().split('.').last), - ), - }; - - if (userLogLevel != null) { - jsonMap['userLogLevel'] = userLogLevel!.map( - (key, value) => MapEntry(key, value.toJson()), - ); - } - - return jsonMap; - } + Map toJson() => _$LoggingConstraintToJson(this); /// The default [LogLevel] for sending logs to CloudWatch. final LogLevel defaultLogLevel; @@ -112,18 +92,7 @@ class UserLogLevel { _$UserLogLevelFromJson(json); /// Converts a [UserLogLevel] instance to a [Map]. - Map toJson() => { - 'defaultLogLevel': defaultLogLevel - ?.toString() - .split('.') - .last, // Convert enum to string - 'categoryLogLevel': categoryLogLevel?.map( - (key, value) => MapEntry( - key, - value.toString().split('.').last, - ), // Convert enum to string - ), - }; + Map toJson() => _$UserLogLevelToJson(this); /// The default [LogLevel] for sending logs to CloudWatch. final LogLevel? defaultLogLevel; diff --git a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.g.dart b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.g.dart index 2ec0bf735a..64733e29d2 100644 --- a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.g.dart +++ b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/plugin_config.g.dart @@ -20,13 +20,25 @@ LoggingConstraint _$LoggingConstraintFromJson(Map json) => ), ); -Map _$LoggingConstraintToJson(LoggingConstraint instance) => - { - 'defaultLogLevel': _$LogLevelEnumMap[instance.defaultLogLevel]!, - 'categoryLogLevel': instance.categoryLogLevel - ?.map((k, e) => MapEntry(k, _$LogLevelEnumMap[e]!)), - 'userLogLevel': instance.userLogLevel, - }; +Map _$LoggingConstraintToJson(LoggingConstraint instance) { + final val = { + 'defaultLogLevel': _$LogLevelEnumMap[instance.defaultLogLevel]!, + }; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull( + 'categoryLogLevel', + instance.categoryLogLevel + ?.map((k, e) => MapEntry(k, _$LogLevelEnumMap[e]!))); + writeNotNull('userLogLevel', + instance.userLogLevel?.map((k, e) => MapEntry(k, e.toJson()))); + return val; +} const _$LogLevelEnumMap = { LogLevel.verbose: 'verbose', @@ -46,9 +58,19 @@ UserLogLevel _$UserLogLevelFromJson(Map json) => UserLogLevel( ), ); -Map _$UserLogLevelToJson(UserLogLevel instance) => - { - 'defaultLogLevel': _$LogLevelEnumMap[instance.defaultLogLevel], - 'categoryLogLevel': instance.categoryLogLevel - ?.map((k, e) => MapEntry(k, _$LogLevelEnumMap[e]!)), - }; +Map _$UserLogLevelToJson(UserLogLevel instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('defaultLogLevel', _$LogLevelEnumMap[instance.defaultLogLevel]); + writeNotNull( + 'categoryLogLevel', + instance.categoryLogLevel + ?.map((k, e) => MapEntry(k, _$LogLevelEnumMap[e]!))); + return val; +}