Skip to content

Commit

Permalink
Improving on generate_classes_for_json_value
Browse files Browse the repository at this point in the history
  • Loading branch information
eernstg committed Feb 2, 2023
1 parent 2456296 commit 53b129b
Showing 1 changed file with 75 additions and 9 deletions.
84 changes: 75 additions & 9 deletions lib/generate_classes_for_json_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,111 @@ import 'package:inline_union_type/inline_json_type.dart';

abstract class JsonClassBase {
String get typeAnnotation;
toString() => typeAnnotation;
}


class JsonClassNull extends JsonClassBase {
get typeAnnotation => 'Null';

int get hashCode => (JsonClassNull).hashCode;
bool operator ==(other) => other is JsonClassNull;
}

class JsonClassBool extends JsonClassBase {
get typeAnnotation => 'bool';


int get hashCode => (JsonClassBool).hashCode;
bool operator ==(other) => other is JsonClassBool;
}

class JsonClassInt extends JsonClassBase {
get typeAnnotation => 'int';

int get hashCode => (JsonClassInt).hashCode;
bool operator ==(other) => other is JsonClassInt;
}

class JsonClassDouble extends JsonClassBase {
get typeAnnotation => 'double';

int get hashCode => (JsonClassDouble).hashCode;
bool operator ==(other) => other is JsonClassDouble;
}

class JsonClassString extends JsonClassBase {
get typeAnnotation => 'String';

int get hashCode => (JsonClassString).hashCode;
bool operator ==(other) => other is JsonClassString;
}

class JsonClassList extends JsonClassBase {
get typeAnnotation => 'List<$typeArgument>';
String typeArgument;

final JsonClassBase typeArgument;

JsonClassList(this.typeArgument);

int get hashCode => Object.hash(JsonClassList, typeArgument);

bool operator ==(other) {
if (other is! JsonClassList) return false;
return typeArgument != other.typeArgument;
}
}

class JsonClassMap extends JsonClassBase {
String typeAnnotation = '';

class JsonClassObject extends JsonClassBase {
get typeAnnotation => '$className';
final String className;
final Map<String, JsonClassBase> fields = {};

JsonClassObject(this.className);

int get hashCode =>
Object.hashAllUnordered([JsonClassObject, ...fields.keys]);

toString() {
var buffer = StringBuffer('class $className {\n');
for (var entry in fields.entries) {
buffer.write(' ${entry.value.typeAnnotation} ${entry.key};\n');
}
buffer.write('}');
return buffer.toString();
}

bool operator ==(other) {
if (other is! JsonClassObject) return false;
if (fields.length != other.fields.length) return false;
for (var entry in fields.entries) {
if (!other.fields.containsKey(entry.key)) return false;
var otherValue = other.fields[entry.key];
if (entry.value != otherValue) return false;
}
return true;
}
}

class JsonClassCollector {
var _nameIndex = 1;
String freshName(String prefix) => '$prefix${_nameIndex++}';
Set<JsonClassBase> result = {};

List<JsonClassBase> computeJsonClasses(Json value) {
return []; // TODO
JsonClassBase computeJsonClass(Json value) {
return value.splitNamed<JsonClassBase>(
onNull: () => JsonClassNull(),
onBool: (_) => JsonClassBool(),
onInt: (_) => JsonClassInt(),
onDouble: (_) => JsonClassDouble(),
onString: (_) => JsonClassString(),
onList: (jsonValues) => JsonClassList(JsonClassNull()),
onMap: (jsonMap) {
var className = freshName('JsonClass');
var jsonObject = JsonClassObject(className);
for (var entry in jsonMap.entries) {
var fieldType = computeJsonClass(entry.value);
jsonObject.fields[entry.key] = fieldType;
}
return jsonObject;
},
)!;
}
}

0 comments on commit 53b129b

Please sign in to comment.