Skip to content

Commit

Permalink
Adjust Point parsing (#6)
Browse files Browse the repository at this point in the history
The Point object stills not being able to be send to the server.
  • Loading branch information
bigmontz authored Jul 19, 2024
1 parent 04cf06c commit b96cbe6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/http-connection/query.codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RawQueryValueTypes = 'Null' | 'Boolean' | 'Integer' | 'Float' | 'String' |
'Duration' | 'Point' | 'Base64' | 'Map' | 'List' | 'Node' | 'Relationship' |
'Path'

type PointShape = { srid: number, x: string, y: string, z?: string }
type PointShape = { coordinates: [number, number, number?], crs: { srid: number } }
type NodeShape = { _element_id: string, _labels: string[], _properties?: Record<string, RawQueryValue>}
type RelationshipShape = { _element_id: string, _start_node_element_id: string, _end_node_element_id: string, _type: string, _properties?: Record<string, RawQueryValue> }
type PathShape = (RawQueryRelationship | RawQueryNode)[]
Expand Down Expand Up @@ -426,10 +426,8 @@ export class QueryResponseCodec {

_decodePoint(value: PointShape): Point<Integer | bigint | number> {
return new Point(
this._normalizeInteger(int(value.srid)),
this._decodeFloat(value.x),
this._decodeFloat(value.y),
value.z != null ? this._decodeFloat(value.z) : undefined
this._normalizeInteger(int(value.crs.srid)),
...value.coordinates
)
}

Expand Down Expand Up @@ -577,10 +575,14 @@ export class QueryRequestCodec {
return this._encodeValue(Array.from(value))
} else if (isPoint(value)) {
return { $type: 'Point', _value: {
srid: int(value.srid).toNumber(),
x: value.x.toString(),
y: value.y.toString(),
z: value.z?.toString()
crs: {
srid: int(value.srid).toNumber(),
},
coordinates: [
value.x,
value.y,
value.z
].filter(v => v != null) as [number, number, number?]
}}
} else if (isDuration(value)) {
return { $type: 'Duration', _value: value.toString()}
Expand Down
15 changes: 15 additions & 0 deletions test/integration/minimum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ describe('minimum requirement', () => {
}
})

it.each([
['CARTESIAN Point 2D', 'point({x: 2.3, y: 4.5})', new Point(int(7203), 2.3, 4.5)],
['CARTESIAN Point 3D', 'point({x: 2.3, y: 4.5, z:7.5})', new Point(int(9157), 2.3, 4.5, 7.5)],
['WGS Point 2D', 'point({longitude: 12.78, latitude: 56.7})', new Point(int(4326), 12.78, 56.7)],
['WGS Point 3D', 'point({longitude: 12.78, latitude: 56.7, height: 120.57})', new Point(int(4979), 12.78, 56.7, 120.57)]
])('should be able to echo "%s" (%s)', async (_, statement, expectedOutput) => {
const session = wrapper.session({ database: config.database })
try {
const { records: [first] } = await session.run(`RETURN ${statement} as output`)
expect(first.get('output')).toEqual(expectedOutput)
} finally {
await session.close()
}
})

function v<T, K = T>(value: T, mapper: (value: T)=> K = (v) => v as unknown as K): [T, K] {
return [value, mapper(value)]
}
Expand Down

0 comments on commit b96cbe6

Please sign in to comment.