Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 85 additions & 5 deletions packages/firestore/lib/firestore.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,87 @@
library firestore;
import 'package:http/http.dart' as http;

/// A Calculator.
class Calculator {
/// Returns [value] plus 1.
int addOne(int value) => value + 1;
import 'models/enums/primitive_types.dart';

class Firestore {
/// [apiKey] the key that is used to authenticate requests
late final String apiKey;

/// [client] the http client to be used for this API. It is being passed in for testability.
late final http.Client _client;

Firestore._();

static late final Firestore _instance;

static Firestore get instance {
return _instance;
}

factory Firestore.init({required String withApiKey, required http.Client client}) {
_instance = Firestore._();
_instance._client = client;
_instance.apiKey = withApiKey;
return instance;
}

static Map<String, dynamic> expandJson(Map<String, dynamic> json) {
Map<String, dynamic> messyJson = {};
json.forEach((key, value) {
FireTypes type = PrimEx.tryParse(value: value.runtimeType.toString());
if (type == FireTypes.arrayValue) {
messyJson[key] = {
FireTypes.arrayValue.stringVal: {'values': _expandList(value)}
};
} else if (type == FireTypes.mapValue) {
Map<String, dynamic> map = _expandMap(value);
messyJson[key] = {
FireTypes.mapValue.stringVal: {"fields": map}
};
} else {
messyJson[key] = typeWiseMatcher(key, value);
}
});
return messyJson;
}
}

Map<String, dynamic> typeWiseMatcher(String key, dynamic value) {
Map<String, dynamic> nestedJson = {};
dynamic mutatedValue = value;
var keyVal = PrimEx.tryParse(value: key.runtimeType.toString()).stringVal;
switch (PrimEx.tryParse(value: key.runtimeType.toString())) {
case FireTypes.booleanValue:
mutatedValue = value == "true" ? true : false;
break;
case FireTypes.doubleValue:
mutatedValue = double.parse(value);
break;
case FireTypes.integerValue:
mutatedValue = int.parse(value);
break;
case FireTypes.stringValue:
RegExp dateMatcher = RegExp(r"\b[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\b");
if (dateMatcher.hasMatch(value.toString())) keyVal = FireTypes.timestampValue.stringVal;
mutatedValue = value.toString();
break;
default:
}
nestedJson[keyVal] = mutatedValue;
return nestedJson;
}

List<dynamic> _expandList(List<dynamic> list) {
List<dynamic> newList = [];
for (var element in list) {
newList.add(typeWiseMatcher(element, element));
}
return newList;
}

Map<String, dynamic> _expandMap(Map<String, dynamic> json) {
Map<String, dynamic> messyJson = {};
json.forEach((key, value) {
messyJson[key] = typeWiseMatcher(key, value);
});
return messyJson;
}
Empty file.
48 changes: 48 additions & 0 deletions packages/firestore/lib/models/enums/primitive_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:core';

enum FireTypes {
stringValue,
arrayValue,
booleanValue,
timestampValue,
referenceValue,
doubleValue,
intValue,
integerValue,
mapValue
}

extension PrimEx on FireTypes {
static FireTypes tryParse({required String value}) {
var val = _fromRawType[value];
assert(val != null, "This type needs to be added to the types string map");
return val!;
}

String get stringVal {
var val = _stringVal[this];
assert(val != null, "This type needs to be added to the types string map");
return val!;
}
}

final Map<FireTypes, String> _stringVal = {
FireTypes.stringValue: "stringValue",
FireTypes.arrayValue: "arrayValue",
FireTypes.booleanValue: "booleanValue",
FireTypes.timestampValue: "timestampValue",
FireTypes.referenceValue: "referenceValue",
FireTypes.doubleValue: "doubleValue",
FireTypes.intValue: "intValue",
FireTypes.integerValue: "integerValue",
FireTypes.mapValue: "mapValue"
};

final Map<String, FireTypes> _fromRawType = {
"int": FireTypes.integerValue,
"bool": FireTypes.booleanValue,
"_InternalLinkedHashMap<String, dynamic>": FireTypes.mapValue,
"List<dynamic>": FireTypes.arrayValue,
"String": FireTypes.stringValue,
"double": FireTypes.doubleValue
};
Empty file.
9 changes: 9 additions & 0 deletions packages/firestore/lib/models/primitives/array_value.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:firestore/models/primitives/firestore_primitive_base.dart';

class ArrayValue extends FireStorePrimitive {
@override
convert() {
// TODO: implement convert
throw UnimplementedError();
}
}
64 changes: 64 additions & 0 deletions packages/firestore/lib/models/primitives/document.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:json_annotation/json_annotation.dart';

part 'document.g.dart';

@JsonSerializable(explicitToJson: true)
class Document {
/// [ref] The document reference that can be used to lookup this document in firebase.
@JsonKey(name: "name")
late final String ref;

/// [createTime] The time that this document was created.
late final DateTime createTime;

/// [updateTime] The time when this document was last updated.
late final DateTime updateTime;

/// [jsonData] The data that is included in this document.
@JsonKey(name: "fields", fromJson: _flattenJson)
late final Map<String, dynamic> jsonData;

Document();

Map<String, dynamic> toJson() => _$DocumentToJson(this);
factory Document.fromJson(Map<String, dynamic> json) => _$DocumentFromJson(json);

static Map<String, dynamic> _flattenJson(Map<String, dynamic> messyJson) {
Map<String, dynamic> cleanJson = {};
// This should handle everything except list and map
messyJson.forEach((key, value) {
value.forEach((key2, value2) {
if (key2 == "mapValue") {
cleanJson[key] = _mapHandler(value2);
} else if (key2 == "arrayValue") {
cleanJson[key] = _listHandler(value2);
} else {
if (key2 == "integerValue") {
cleanJson[key] = int.parse(value2);
} else {
cleanJson[key] = value2;
}
}
});
});
return cleanJson;
}

static List<dynamic> _listHandler(Map<String, dynamic> messyJson) {
List<dynamic> cleanJson = [];
messyJson['values'].forEach((value) {
cleanJson.add(value[value.keys.first]);
});
return cleanJson;
}

static Map<String, dynamic> _mapHandler(Map<String, dynamic> messyJson) {
Map<String, dynamic> cleanJson = {};
messyJson['fields'].forEach((key, value) {
value.forEach((key2, value2) {
cleanJson[key] = value2;
});
});
return cleanJson;
}
}
20 changes: 20 additions & 0 deletions packages/firestore/lib/models/primitives/document.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class FireStorePrimitive<T> {
T convert();
}
Loading