protectDefaultValue function

String? protectDefaultValue(
  1. Object? name, {
  2. String? type,
  3. bool isEnum = false,
  4. bool isArray = false,
  5. bool dart = true,
})

Protect default value from incorrect symbols, keywords, etc.

Implementation

String? protectDefaultValue(
  Object? name, {
  String? type,
  bool isEnum = false,
  bool isArray = false,
  bool dart = true,
}) {
  final nameStr = name?.toString();
  if (nameStr == null) {
    return null;
  }

  /// Json is not supported
  if (nameStr.startsWith('{') && nameStr.endsWith('}')) {
    return null;
  }

  if (nameStr.startsWith('[') && nameStr.endsWith(']')) {
    return nameStr;
  }

  if (isEnum) {
    return protectEnumItemsNames([nameStr]).first.name;
  }

  if (isArray) {
    return null;
  }

  if (type == 'string') {
    final quote = dart ? "'" : '"';
    return '$quote${nameStr.replaceAll(quote, dart ? r"\'" : r'\"')}$quote';
  }

  return nameStr;
}