Skip to content

Commit

Permalink
Fix: maps, sets and vector types (#203)
Browse files Browse the repository at this point in the history
* fix: update typescript for vector, sets and maps

* fix: prettier

* fix: code cleaning

* Delete yarn.lock

* fix: return yarn.lock from develop

* fix: fixing typo

* fix: include deserializer in get params

* fix: code cleaning

* fix: removing logs

* fix: code cleaning

* fix: build lib
  • Loading branch information
osalkanovic authored Sep 16, 2022
1 parent 0719fed commit e8d1288
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 112 deletions.
4 changes: 2 additions & 2 deletions examples/src/parking-lot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class Engine {

@NearBindgen({})
class ParkingLot {
cars: LookupMap;
cars: LookupMap<CarSpecs>;
constructor() {
this.cars = new LookupMap('a');
this.cars = new LookupMap<CarSpecs>('a');
}

@call({})
Expand Down
13 changes: 7 additions & 6 deletions lib/collections/lookup-map.d.ts

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

5 changes: 3 additions & 2 deletions lib/collections/lookup-map.js

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

26 changes: 14 additions & 12 deletions lib/collections/unordered-map.d.ts

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

4 changes: 2 additions & 2 deletions lib/collections/unordered-map.js

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

16 changes: 8 additions & 8 deletions lib/collections/unordered-set.d.ts

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

23 changes: 12 additions & 11 deletions lib/collections/vector.d.ts

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

5 changes: 3 additions & 2 deletions lib/collections/vector.js

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

3 changes: 3 additions & 0 deletions lib/types/collections.d.ts

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

1 change: 1 addition & 0 deletions lib/types/collections.js

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

16 changes: 9 additions & 7 deletions src/collections/lookup-map.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as near from '../api'
import { GetOptions } from '../types/collections';
import { Bytes } from '../utils';

export class LookupMap {
export class LookupMap<DataType> {
readonly keyPrefix: Bytes;

constructor(keyPrefix: Bytes) {
Expand All @@ -13,24 +14,25 @@ export class LookupMap {
return near.storageHasKey(storageKey)
}

get(key: Bytes): unknown | null {
get(key: Bytes, options?: GetOptions<DataType>): DataType | null {
let storageKey = this.keyPrefix + JSON.stringify(key)
let raw = near.storageRead(storageKey)
if (raw !== null) {
return JSON.parse(raw)
const value = JSON.parse(raw)
return !!options?.reconstructor ? options.reconstructor(value) : value as DataType
}
return null
}

remove(key: Bytes): unknown | null {
remove(key: Bytes): DataType | null {
let storageKey = this.keyPrefix + JSON.stringify(key)
if (near.storageRemove(storageKey)) {
return JSON.parse(near.storageGetEvicted())
}
return null
}

set(key: Bytes, value: unknown): unknown | null {
set(key: Bytes, value: DataType): DataType | null {
let storageKey = this.keyPrefix + JSON.stringify(key)
let storageValue = JSON.stringify(value)
if (near.storageWrite(storageKey, storageValue)) {
Expand All @@ -39,7 +41,7 @@ export class LookupMap {
return null
}

extend(objects: [Bytes, unknown][]) {
extend(objects: [Bytes, DataType][]) {
for (let kv of objects) {
this.set(kv[0], kv[1])
}
Expand All @@ -50,7 +52,7 @@ export class LookupMap {
}

// converting plain object to class object
static reconstruct(data: LookupMap): LookupMap {
static reconstruct<DataType>(data: LookupMap<DataType>): LookupMap<DataType> {
return new LookupMap(data.keyPrefix)
}
}
2 changes: 2 additions & 0 deletions src/collections/lookup-set.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


import * as near from '../api'
import { Bytes } from '../utils';

Expand Down
Loading

0 comments on commit e8d1288

Please sign in to comment.