sampleLiteral method

({String dart, String json}) sampleLiteral()

A literal sample value for this field, used in generated round-trip tests. Returns the Dart literal and its matching JSON literal (which differ for DateTime, where JSON carries an ISO-8601 string).

Implementation

({String dart, String json}) sampleLiteral() {
  if (nullable) return (dart: 'null', json: 'null');
  if (isModel && !_isList) {
    return (
      dart: '$type.fromJson(${_modelSampleJson()})',
      json: _modelSampleJson()
    );
  }
  if (_isList) {
    return (dart: 'const []', json: '[]');
  }
  switch (type) {
    case 'int':
      return (dart: '1', json: '1');
    case 'double':
      return (dart: '1.5', json: '1.5');
    case 'num':
      return (dart: '1', json: '1');
    case 'bool':
      return (dart: 'true', json: 'true');
    case 'DateTime':
      return (
        dart: "DateTime.parse('2020-01-01T00:00:00.000')",
        json: "'2020-01-01T00:00:00.000'"
      );
    default:
      return (dart: "'sample'", json: "'sample'");
  }
}