generateValueForVariable method

String generateValueForVariable(
  1. ClassElement element,
  2. String variableName,
  3. dynamic rawValue,
  4. String filePath,
)

Implementation

String generateValueForVariable(ClassElement element, String variableName,
    dynamic rawValue, String filePath) {
  final FieldElement? field = element.getField(variableName.trim());
  if (field == null) {
    throw ('field $variableName not found for class $element, but it was set in your config json file $filePath!');
  }
  String value;
  final fieldElement = field.type.element;
  if (field.type.isDartCoreString) {
    value = asDartLiteral(rawValue.toString());
  } else if (field.type.isDartCoreBool) {
    if (rawValue is! bool) {
      throw '$variableName should be a bool, but got $rawValue (${rawValue.runtimeType})';
    }
    value = rawValue.toString();
  } else if (field.type.isDartCoreInt) {
    if (rawValue is! int) {
      throw '$variableName should be an int, but got $rawValue (${rawValue.runtimeType})';
    }
    value = rawValue.toString();
  } else if (field.type.isDartCoreDouble) {
    if (rawValue is! num) {
      throw '$variableName should be a double, but got $rawValue (${rawValue.runtimeType})';
    }
    value = rawValue.toString();
  } else if (fieldElement is ClassElement && fieldElement.isEnum) {
    //TODO add nullability support
    value =
        '${field.type.getDisplayString(withNullability: false)}.$rawValue';
  } else {
    throw 'unsupported type: ${field.type}';
  }
  return '$variableName:$value';
}