mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
[types] Get flow type working again; Fix majority of firestore type issues
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
// @flow
|
||||
/**
|
||||
* @flow
|
||||
*/
|
||||
|
||||
import DocumentReference from '../DocumentReference';
|
||||
import { DELETE_FIELD_VALUE, SERVER_TIMESTAMP_FIELD_VALUE } from '../FieldValue';
|
||||
@@ -6,8 +8,10 @@ import GeoPoint from '../GeoPoint';
|
||||
import Path from '../Path';
|
||||
import { typeOf } from '../../../utils';
|
||||
|
||||
type TypeMap = {
|
||||
type: 'array' | 'boolean' | 'geopoint' | 'null' | 'number' | 'object' | 'reference' | 'string',
|
||||
import type Firestore from '../';
|
||||
|
||||
export type TypeMap = {
|
||||
type: 'array' | 'boolean' | 'date' | 'fieldvalue' | 'geopoint' | 'null' | 'number' | 'object' | 'reference' | 'string',
|
||||
value: any,
|
||||
}
|
||||
|
||||
@@ -17,65 +21,86 @@ type TypeMap = {
|
||||
* for transmission to the native side
|
||||
*/
|
||||
|
||||
export const buildNativeMap = (data: Object): Object => {
|
||||
export const buildNativeMap = (data: Object): { [string]: TypeMap } => {
|
||||
const nativeData = {};
|
||||
if (data) {
|
||||
Object.keys(data).forEach((key) => {
|
||||
nativeData[key] = buildTypeMap(data[key]);
|
||||
const typeMap = buildTypeMap(data[key]);
|
||||
if (typeMap) {
|
||||
nativeData[key] = typeMap;
|
||||
}
|
||||
});
|
||||
}
|
||||
return nativeData;
|
||||
};
|
||||
|
||||
export const buildNativeArray = (array: Object[]): any[] => {
|
||||
export const buildNativeArray = (array: Object[]): TypeMap[] => {
|
||||
const nativeArray = [];
|
||||
if (array) {
|
||||
array.forEach((value) => {
|
||||
nativeArray.push(buildTypeMap(value));
|
||||
const typeMap = buildTypeMap(value);
|
||||
if (typeMap) {
|
||||
nativeArray.push(typeMap);
|
||||
}
|
||||
});
|
||||
}
|
||||
return nativeArray;
|
||||
};
|
||||
|
||||
export const buildTypeMap = (value: any): any => {
|
||||
const typeMap = {};
|
||||
export const buildTypeMap = (value: any): TypeMap | null => {
|
||||
const type = typeOf(value);
|
||||
if (value === null || value === undefined) {
|
||||
typeMap.type = 'null';
|
||||
typeMap.value = null;
|
||||
return {
|
||||
type: 'null',
|
||||
value: null,
|
||||
};
|
||||
} else if (value === DELETE_FIELD_VALUE) {
|
||||
typeMap.type = 'fieldvalue';
|
||||
typeMap.value = 'delete';
|
||||
return {
|
||||
type: 'fieldvalue',
|
||||
value: 'delete',
|
||||
};
|
||||
} else if (value === SERVER_TIMESTAMP_FIELD_VALUE) {
|
||||
typeMap.type = 'fieldvalue';
|
||||
typeMap.value = 'timestamp';
|
||||
return {
|
||||
type: 'fieldvalue',
|
||||
value: 'timestamp',
|
||||
};
|
||||
} else if (type === 'boolean' || type === 'number' || type === 'string') {
|
||||
typeMap.type = type;
|
||||
typeMap.value = value;
|
||||
return {
|
||||
type,
|
||||
value,
|
||||
};
|
||||
} else if (type === 'array') {
|
||||
typeMap.type = type;
|
||||
typeMap.value = buildNativeArray(value);
|
||||
return {
|
||||
type,
|
||||
value: buildNativeArray(value),
|
||||
};
|
||||
} else if (type === 'object') {
|
||||
if (value instanceof DocumentReference) {
|
||||
typeMap.type = 'reference';
|
||||
typeMap.value = value.path;
|
||||
return {
|
||||
type: 'reference',
|
||||
value: value.path,
|
||||
};
|
||||
} else if (value instanceof GeoPoint) {
|
||||
typeMap.type = 'geopoint';
|
||||
typeMap.value = {
|
||||
latitude: value.latitude,
|
||||
longitude: value.longitude,
|
||||
return {
|
||||
type: 'geopoint',
|
||||
value: {
|
||||
latitude: value.latitude,
|
||||
longitude: value.longitude,
|
||||
},
|
||||
};
|
||||
} else if (value instanceof Date) {
|
||||
typeMap.type = 'date';
|
||||
typeMap.value = value.getTime();
|
||||
} else {
|
||||
typeMap.type = 'object';
|
||||
typeMap.value = buildNativeMap(value);
|
||||
return {
|
||||
type: 'date',
|
||||
value: value.getTime(),
|
||||
};
|
||||
}
|
||||
} else {
|
||||
console.warn(`Unknown data type received ${type}`);
|
||||
return {
|
||||
type: 'object',
|
||||
value: buildNativeMap(value),
|
||||
};
|
||||
}
|
||||
return typeMap;
|
||||
console.warn(`Unknown data type received ${type}`);
|
||||
return null;
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -83,7 +108,7 @@ export const buildTypeMap = (value: any): any => {
|
||||
* side and converts to the correct Firestore JS types
|
||||
*/
|
||||
|
||||
export const parseNativeMap = (firestore: Object, nativeData: Object): Object => {
|
||||
export const parseNativeMap = (firestore: Firestore, nativeData: { [string]: TypeMap }): Object | void => {
|
||||
let data;
|
||||
if (nativeData) {
|
||||
data = {};
|
||||
@@ -94,7 +119,7 @@ export const parseNativeMap = (firestore: Object, nativeData: Object): Object =>
|
||||
return data;
|
||||
};
|
||||
|
||||
const parseNativeArray = (firestore: Object, nativeArray: Object[]): any[] => {
|
||||
const parseNativeArray = (firestore: Firestore, nativeArray: TypeMap[]): any[] => {
|
||||
const array = [];
|
||||
if (nativeArray) {
|
||||
nativeArray.forEach((typeMap) => {
|
||||
@@ -104,7 +129,7 @@ const parseNativeArray = (firestore: Object, nativeArray: Object[]): any[] => {
|
||||
return array;
|
||||
};
|
||||
|
||||
const parseTypeMap = (firestore: Object, typeMap: TypeMap): any => {
|
||||
const parseTypeMap = (firestore: Firestore, typeMap: TypeMap): any => {
|
||||
const { type, value } = typeMap;
|
||||
if (type === 'null') {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user