Skip to content

Commit

Permalink
Replace temporary types w/ binding types.
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-j committed Jul 10, 2023
1 parent e2e2124 commit 1da3ee5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 65 deletions.
8 changes: 3 additions & 5 deletions packages/realm/src/ClassMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import {
setClassHelpers,
} from "./internal";

type BindingObjectSchema = binding.Realm["schema"][0];

/**
* @internal
*/
Expand All @@ -53,7 +51,7 @@ export class ClassMap {
}

private static createClass<T extends RealmObjectConstructor = RealmObjectConstructor>(
schema: BindingObjectSchema,
schema: binding.ObjectSchema,
constructor: Constructor | undefined,
): T {
const result = ClassMap.createNamedConstructor<T>(schema.name);
Expand All @@ -71,7 +69,7 @@ export class ClassMap {

private static defineProperties(
constructor: Constructor,
schema: BindingObjectSchema,
schema: binding.ObjectSchema,
propertyMap: PropertyMap,
realm: Realm,
) {
Expand Down Expand Up @@ -114,7 +112,7 @@ export class ClassMap {
});
}

constructor(realm: Realm, realmSchema: readonly BindingObjectSchema[], canonicalRealmSchema: CanonicalRealmSchema) {
constructor(realm: Realm, realmSchema: readonly binding.ObjectSchema[], canonicalRealmSchema: CanonicalRealmSchema) {
this.mapping = Object.fromEntries(
realmSchema.map((objectSchema, index) => {
const canonicalObjectSchema: CanonicalObjectSchema = canonicalRealmSchema[index];
Expand Down
5 changes: 1 addition & 4 deletions packages/realm/src/PropertyHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ import {
getTypeHelpers,
} from "./internal";

type BindingObjectSchema = binding.Realm["schema"][0];
type BindingPropertySchema = BindingObjectSchema["persistedProperties"][0];

type PropertyContext = BindingPropertySchema & {
type PropertyContext = binding.Property & {
type: binding.PropertyType;
objectSchemaName: string;
embedded: boolean;
Expand Down
4 changes: 1 addition & 3 deletions packages/realm/src/PropertyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import { HelperOptions, PropertyHelpers, binding, createPropertyHelpers } from "./internal";

type BindingObjectSchema = binding.Realm["schema"][0];

class UninitializedPropertyMapError extends Error {
constructor() {
super("Property Map was accessed before it got initialized");
Expand All @@ -37,7 +35,7 @@ export class PropertyMap {
private nameByColumnKeyString: Map<string, string> = new Map();
private _names: string[] = [];

public initialize(objectSchema: BindingObjectSchema, defaults: Record<string, unknown>, options: HelperOptions) {
public initialize(objectSchema: binding.ObjectSchema, defaults: Record<string, unknown>, options: HelperOptions) {
const { name: objectSchemaName, persistedProperties, computedProperties } = objectSchema;
this.objectSchemaName = objectSchemaName;
const properties = [...persistedProperties, ...computedProperties];
Expand Down
85 changes: 43 additions & 42 deletions packages/realm/src/schema/from-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,67 @@
//
////////////////////////////////////////////////////////////////////////////

import { PropertyType, TableType } from "../binding";
import { assert, binding } from "../internal";

// TODO: Update these once the binding expose proper types
type BindingObjectSchema = binding.Realm["schema"][0];
type BindingProperty = binding.Realm["schema"][0]["persistedProperties"][0];
import {
ObjectSchema as BindingObjectSchema,
Property as BindingProperty,
PropertyType as BindingPropertyType,
TableType,
} from "../binding";
import { assert } from "../internal";

import { CanonicalObjectSchema, CanonicalPropertySchema, PropertyTypeName } from "./types";

const TYPE_MAPPINGS: Record<binding.PropertyType, PropertyTypeName | null> = {
[PropertyType.Int]: "int",
[PropertyType.Bool]: "bool",
[PropertyType.String]: "string",
[PropertyType.Data]: "data",
[PropertyType.Date]: "date",
[PropertyType.Float]: "float",
[PropertyType.Double]: "double",
[PropertyType.Mixed]: "mixed",
[PropertyType.ObjectId]: "objectId",
[PropertyType.Decimal]: "decimal128",
[PropertyType.Uuid]: "uuid",
[PropertyType.Array]: "list",
[PropertyType.Set]: "set",
[PropertyType.Dictionary]: "dictionary",
[PropertyType.LinkingObjects]: "linkingObjects",
[PropertyType.Object]: "object",
const TYPE_MAPPINGS: Record<BindingPropertyType, PropertyTypeName | null> = {
[BindingPropertyType.Int]: "int",
[BindingPropertyType.Bool]: "bool",
[BindingPropertyType.String]: "string",
[BindingPropertyType.Data]: "data",
[BindingPropertyType.Date]: "date",
[BindingPropertyType.Float]: "float",
[BindingPropertyType.Double]: "double",
[BindingPropertyType.Mixed]: "mixed",
[BindingPropertyType.ObjectId]: "objectId",
[BindingPropertyType.Decimal]: "decimal128",
[BindingPropertyType.Uuid]: "uuid",
[BindingPropertyType.Array]: "list",
[BindingPropertyType.Set]: "set",
[BindingPropertyType.Dictionary]: "dictionary",
[BindingPropertyType.LinkingObjects]: "linkingObjects",
[BindingPropertyType.Object]: "object",
// These have no direct
[PropertyType.Nullable]: null,
[BindingPropertyType.Nullable]: null,
//
[PropertyType.Collection]: null,
[PropertyType.Flags]: null,
[BindingPropertyType.Collection]: null,
[BindingPropertyType.Flags]: null,
};

/**
* Get the string representation of a property type's base type (not including flags)
* @internal
*/
export function getTypeName(type: PropertyType, objectType: string | undefined): string {
const baseType = type & ~PropertyType.Flags;
if (type & PropertyType.Array) {
if (baseType === PropertyType.Object) {
export function getTypeName(type: BindingPropertyType, objectType: string | undefined): string {
const baseType = type & ~BindingPropertyType.Flags;
if (type & BindingPropertyType.Array) {
if (baseType === BindingPropertyType.Object) {
return `list<${objectType}>`;
} else {
return `list<${getTypeName(baseType, objectType)}>`;
}
} else if (type & PropertyType.Set) {
} else if (type & BindingPropertyType.Set) {
return `set<${getTypeName(baseType, objectType)}>`;
} else if (type & PropertyType.Dictionary) {
} else if (type & BindingPropertyType.Dictionary) {
return `dictionary<${getTypeName(baseType, objectType)}>`;
} else if (baseType === PropertyType.Object && objectType) {
} else if (baseType === BindingPropertyType.Object && objectType) {
assert.string(objectType, "objectType");
return `<${objectType}>`;
} else {
const result = TYPE_MAPPINGS[baseType as PropertyType];
const result = TYPE_MAPPINGS[baseType as BindingPropertyType];
assert(result, `Unexpected type ${type}`);
return result;
}
}

const COLLECTION_TYPES = [PropertyType.Array, PropertyType.Set, PropertyType.Dictionary];
const COLLECTION_TYPES = [BindingPropertyType.Array, BindingPropertyType.Set, BindingPropertyType.Dictionary];

/**
* Implements https://github.com/realm/realm-js/blob/v11/src/js_schema.hpp#L433-L478
Expand Down Expand Up @@ -128,15 +129,15 @@ function fromBindingPropertyTypeName(
propertySchema: BindingProperty,
): Pick<CanonicalPropertySchema, "type" | "optional" | "objectType" | "property"> {
const { type, objectType, linkOriginPropertyName } = propertySchema;
const itemType = type & ~PropertyType.Collection;
const itemType = type & ~BindingPropertyType.Collection;

if (type & PropertyType.Nullable) {
const item = fromBindingPropertyTypeName({ ...propertySchema, type: type & ~PropertyType.Nullable });
if (type & BindingPropertyType.Nullable) {
const item = fromBindingPropertyTypeName({ ...propertySchema, type: type & ~BindingPropertyType.Nullable });
return { ...item, optional: true };
}

if (itemType === PropertyType.LinkingObjects) {
assert(type & PropertyType.Array);
if (itemType === BindingPropertyType.LinkingObjects) {
assert(type & BindingPropertyType.Array);
assert.string(linkOriginPropertyName, "linkOriginPropertyName");
return {
type: "linkingObjects",
Expand All @@ -157,13 +158,13 @@ function fromBindingPropertyTypeName(
}
}

if (type === PropertyType.Object) {
if (type === BindingPropertyType.Object) {
if (!objectType) {
throw new Error("Expected property with 'object' type to declare an objectType");
}
// TODO: Decide if this change is reasonable
return { type: "object", objectType, optional: true }; // Implicitly nullable
} else if (type === PropertyType.LinkingObjects) {
} else if (type === BindingPropertyType.LinkingObjects) {
if (!objectType) {
throw new Error("Expected property with 'object' type to declare an objectType");
}
Expand Down
31 changes: 20 additions & 11 deletions packages/realm/src/tests/schema-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
import { expect } from "chai";
import { inspect } from "util";

import { ColKey, PropertyType, Realm, TableKey, TableType } from "../binding";
import {
ObjectSchema as BindingObjectSchema,
Property as BindingProperty,
PropertyType as BindingPropertyType,
ColKey,
TableKey,
TableType,
} from "../binding";
import { CanonicalObjectSchema, CanonicalPropertySchema } from "../schema";
import { fromBindingObjectSchema, fromBindingPropertySchema } from "../schema/from-binding";

// TODO: Update these once the binding expose proper types
type BindingObjectSchema = Realm["schema"][0];
type BindingProperty = Realm["schema"][0]["persistedProperties"][0];

const columnKey = { value: 0n } as unknown as ColKey;
const tableKey = { value: 0 } as unknown as TableKey;

Expand All @@ -36,9 +39,10 @@ describe("schema-utils", () => {
[
{
name: "prop",
type: PropertyType.Int,
type: BindingPropertyType.Int,
columnKey,
isIndexed: false,
isFulltextIndexed: false,
isPrimary: false,
linkOriginPropertyName: "",
objectType: "",
Expand All @@ -49,9 +53,10 @@ describe("schema-utils", () => {
[
{
name: "prop",
type: PropertyType.Int | PropertyType.Nullable,
type: BindingPropertyType.Int | BindingPropertyType.Nullable,
columnKey,
isIndexed: false,
isFulltextIndexed: false,
isPrimary: false,
linkOriginPropertyName: "",
objectType: "",
Expand All @@ -62,9 +67,10 @@ describe("schema-utils", () => {
[
{
name: "prop",
type: PropertyType.String,
type: BindingPropertyType.String,
columnKey,
isIndexed: false,
isFulltextIndexed: false,
isPrimary: false,
linkOriginPropertyName: "",
objectType: "",
Expand All @@ -75,9 +81,10 @@ describe("schema-utils", () => {
[
{
name: "prop",
type: PropertyType.String | PropertyType.Nullable,
type: BindingPropertyType.String | BindingPropertyType.Nullable,
columnKey,
isIndexed: false,
isFulltextIndexed: false,
isPrimary: false,
linkOriginPropertyName: "",
objectType: "",
Expand All @@ -103,20 +110,22 @@ describe("schema-utils", () => {
persistedProperties: [
{
name: "name",
type: PropertyType.String,
type: BindingPropertyType.String,
columnKey,
isIndexed: false,
isFulltextIndexed: false,
isPrimary: false,
linkOriginPropertyName: "",
objectType: "",
publicName: "",
},
{
name: "friends",
type: PropertyType.Object ^ PropertyType.Array,
type: BindingPropertyType.Object ^ BindingPropertyType.Array,
objectType: "Person",
columnKey,
isIndexed: false,
isFulltextIndexed: false,
isPrimary: false,
linkOriginPropertyName: "",
publicName: "",
Expand Down

0 comments on commit 1da3ee5

Please sign in to comment.