-
Notifications
You must be signed in to change notification settings - Fork 2
fix: make GeoPoint portable through json #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,4 +35,4 @@ | |
| "@effect-firebase/client": "workspace:*", | ||
| "@example/shared": "workspace:*" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,4 +19,4 @@ | |
| "@example/shared": "workspace:*" | ||
| }, | ||
| "devDependencies": {} | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,4 +29,4 @@ | |
| "devDependencies": { | ||
| "effect-firebase": "workspace:*" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,4 +86,4 @@ | |
| "packages/*", | ||
| "example/*" | ||
| ] | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/effect-firebase/src/lib/firestore/model/geopoint.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { Schema } from 'effect'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { Model } from 'effect/unstable/schema'; | ||
| import { GeoPoint } from './geopoint.js'; | ||
| import { GeoPoint as GeoPointClass } from '../schema/geopoint.js'; | ||
|
|
||
| describe('Model.GeoPoint', () => { | ||
| const PlaceId = Schema.String.pipe(Schema.brand('PlaceId')); | ||
|
|
||
| class TestModel extends Model.Class<TestModel>('TestModel')({ | ||
| id: Model.GeneratedByDb(PlaceId), | ||
| location: GeoPoint, | ||
| }) {} | ||
|
|
||
| describe('select variant', () => { | ||
| it('should decode a GeoPoint instance to a GeoPoint instance', () => { | ||
| const decode = Schema.decodeUnknownSync(TestModel); | ||
| const result = decode({ | ||
| id: 'place-1', | ||
| location: new GeoPointClass({ | ||
| latitude: 37.7749, | ||
| longitude: -122.4194, | ||
| }), | ||
| }); | ||
|
|
||
| expect(result.location).toBeInstanceOf(GeoPointClass); | ||
| expect(result.location.latitude).toBe(37.7749); | ||
| expect(result.location.longitude).toBe(-122.4194); | ||
| }); | ||
|
|
||
| it('should encode a GeoPoint instance to a GeoPoint instance', () => { | ||
| const encode = Schema.encodeSync(TestModel); | ||
| const result = encode( | ||
| new TestModel({ | ||
| id: PlaceId.make('place-1'), | ||
| location: new GeoPointClass({ | ||
| latitude: 37.7749, | ||
| longitude: -122.4194, | ||
| }), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.location).toBeInstanceOf(GeoPointClass); | ||
| expect(result.location.latitude).toBe(37.7749); | ||
| expect(result.location.longitude).toBe(-122.4194); | ||
| }); | ||
| }); | ||
|
|
||
| describe('insert variant', () => { | ||
| it('should encode a GeoPoint instance to a GeoPoint instance', () => { | ||
| const encode = Schema.encodeSync(TestModel.insert); | ||
| const result = encode({ | ||
| location: new GeoPointClass({ latitude: 51.5074, longitude: -0.1278 }), | ||
| }); | ||
|
|
||
| expect(result.location).toBeInstanceOf(GeoPointClass); | ||
| }); | ||
| }); | ||
|
|
||
| describe('json variant', () => { | ||
| it('should decode a plain object to a GeoPoint instance', () => { | ||
| const decode = Schema.decodeUnknownSync(TestModel.json); | ||
| const result = decode({ | ||
| id: 'place-1', | ||
| location: { latitude: 35.6762, longitude: 139.6503 }, | ||
| }); | ||
|
|
||
| expect(result.location).toBeInstanceOf(GeoPointClass); | ||
| expect(result.location.latitude).toBe(35.6762); | ||
| expect(result.location.longitude).toBe(139.6503); | ||
| }); | ||
|
|
||
| it('should encode a GeoPoint instance to a plain object', () => { | ||
| const encode = Schema.encodeSync(TestModel.json); | ||
| const result = encode({ | ||
| id: PlaceId.make('place-1'), | ||
| location: new GeoPointClass({ latitude: 35.6762, longitude: 139.6503 }), | ||
| }); | ||
|
|
||
| expect(result.location).toEqual({ | ||
| latitude: 35.6762, | ||
| longitude: 139.6503, | ||
| }); | ||
| expect(result.location).not.toBeInstanceOf(GeoPointClass); | ||
| }); | ||
|
|
||
| it('should survive a JSON.stringify/parse roundtrip', () => { | ||
| const encode = Schema.encodeSync(TestModel.json); | ||
| const decode = Schema.decodeUnknownSync(TestModel.json); | ||
|
|
||
| const original = new TestModel({ | ||
| id: PlaceId.make('place-1'), | ||
| location: new GeoPointClass({ | ||
| latitude: -33.8688, | ||
| longitude: 151.2093, | ||
| }), | ||
| }); | ||
|
|
||
| const roundtripped = decode(JSON.parse(JSON.stringify(encode(original)))); | ||
|
|
||
| expect(roundtripped.location).toBeInstanceOf(GeoPointClass); | ||
| expect(roundtripped.location.latitude).toBe(-33.8688); | ||
| expect(roundtripped.location.longitude).toBe(151.2093); | ||
| }); | ||
| }); | ||
| }); |
42 changes: 42 additions & 0 deletions
42
packages/effect-firebase/src/lib/firestore/model/geopoint.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { Model, VariantSchema } from 'effect/unstable/schema'; | ||
| import * as FirestoreSchema from '../schema/schema.js'; | ||
|
|
||
| /** | ||
| * A field that stores a GeoPoint in Firestore and survives JSON serialization. | ||
| * | ||
| * - App (Type): `GeoPoint` class instance | ||
| * - DB (Encoded): `GeoPoint` class instance (preserved through the driver) | ||
| * - JSON (Encoded): plain `{ latitude, longitude }` object | ||
| * | ||
| * The DB variants keep the class instance intact so the Firestore driver can | ||
| * map it to a native GeoPoint, while the JSON variants encode to a plain object | ||
| * so the value can round-trip through `JSON.stringify`/`JSON.parse` like the | ||
| * other model field types. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { Model } from 'effect-firebase'; | ||
| * | ||
| * class PlaceModel extends Model.Class<PlaceModel>('PlaceModel')({ | ||
| * id: Model.GeneratedByDb(PlaceId), | ||
| * location: Model.GeoPoint, | ||
| * }) {} | ||
| * ``` | ||
| */ | ||
| export type GeoPoint = VariantSchema.Field<{ | ||
| select: typeof FirestoreSchema.GeoPointInstance; | ||
| insert: typeof FirestoreSchema.GeoPointInstance; | ||
| update: typeof FirestoreSchema.GeoPointInstance; | ||
| json: typeof FirestoreSchema.GeoPoint; | ||
| jsonCreate: typeof FirestoreSchema.GeoPoint; | ||
| jsonUpdate: typeof FirestoreSchema.GeoPoint; | ||
| }>; | ||
|
|
||
| export const GeoPoint: GeoPoint = Model.Field({ | ||
| select: FirestoreSchema.GeoPointInstance, | ||
| insert: FirestoreSchema.GeoPointInstance, | ||
| update: FirestoreSchema.GeoPointInstance, | ||
| json: FirestoreSchema.GeoPoint, | ||
| jsonCreate: FirestoreSchema.GeoPoint, | ||
| jsonUpdate: FirestoreSchema.GeoPoint, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export * from './array.js'; | ||
| export * from './datetime.js'; | ||
| export * from './geopoint.js'; | ||
| export * from './optional.js'; | ||
| export * from './reference.js'; | ||
| export * from './repository.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.