Skip to content

Commit

Permalink
fix neo4j serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Jan 10, 2025
1 parent 2b657b7 commit fb75de8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 98 deletions.
111 changes: 13 additions & 98 deletions sdk/assemblyscript/src/assembly/neo4j.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,16 @@ export function executeQuery(
return response;
}


@json
export class EagerResult {
Keys: string[] = [];
Records: Record[] = [];

__INITIALIZE(): this {
return this;
}

__SERIALIZE(): string {
return JSON.stringify(this);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
__DESERIALIZE(
data: string,
key_start: i32,
key_end: i32,
value_start: i32,
value_end: i32,
): boolean {
const obj = JSON.parse<EagerResult>(data);
this.Keys = obj.Keys;
this.Records = obj.Records;
return true;
}
}

export class Record {
Values: string[] = [];
Keys: string[] = [];
Values: string[] = [];

get(key: string): string {
for (let i = 0; i < this.Keys.length; i++) {
Expand Down Expand Up @@ -138,7 +118,15 @@ export class Record {
}

__SERIALIZE(): string {
return JSON.stringify(this);
let result = "{";
for (let i = 0; i < this.Keys.length; i++) {
const keyJson = JSON.stringify(this.Keys[i]);
result += `${keyJson}:${this.Values[i]}`;
if (i < this.Keys.length - 1) {
result += ",";
}
}
return result + "}";
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand All @@ -149,10 +137,7 @@ export class Record {
value_start: i32,
value_end: i32,
): boolean {
const obj = JSON.parse<Record>(data);
this.Keys = obj.Keys;
this.Values = obj.Values;
return true;
throw new Error("Not implemented.");
}
}

Expand Down Expand Up @@ -190,29 +175,6 @@ export class Node extends Entity {

@alias("Labels")
Labels!: string[];

__INITIALIZE(): this {
return this;
}

__SERIALIZE(): string {
return JSON.stringify(this);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
__DESERIALIZE(
data: string,
key_start: i32,
key_end: i32,
value_start: i32,
value_end: i32,
): boolean {
const obj = JSON.parse<Node>(data);
this.ElementId = obj.ElementId;
this.Props = obj.Props;
this.Labels = obj.Labels;
return true;
}
}


Expand All @@ -229,31 +191,6 @@ export class Relationship extends Entity {

@alias("Type")
Type!: string;

__INITIALIZE(): this {
return this;
}

__SERIALIZE(): string {
return JSON.stringify(this);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
__DESERIALIZE(
data: string,
key_start: i32,
key_end: i32,
value_start: i32,
value_end: i32,
): boolean {
const obj = JSON.parse<Relationship>(data);
this.ElementId = obj.ElementId;
this.Props = obj.Props;
this.StartElementId = obj.StartElementId;
this.EndElementId = obj.EndElementId;
this.Type = obj.Type;
return true;
}
}


Expand All @@ -266,28 +203,6 @@ export class Path {

@alias("Relationships")
Relationships!: Relationship[];

__INITIALIZE(): this {
return this;
}

__SERIALIZE(): string {
return JSON.stringify(this);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
__DESERIALIZE(
data: string,
key_start: i32,
key_end: i32,
value_start: i32,
value_end: i32,
): boolean {
const obj = JSON.parse<Path>(data);
this.Nodes = obj.Nodes;
this.Relationships = obj.Relationships;
return true;
}
}


Expand Down
16 changes: 16 additions & 0 deletions sdk/go/pkg/neo4j/neo4j.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ func (r *Record) AsMap() map[string]string {
return result
}

func (r *Record) JSONMarshal() ([]byte, error) {
result := "{"
for i, k := range r.Keys {
keyBytes, err := utils.JsonSerialize(k)
if err != nil {
return nil, err
}
result += fmt.Sprintf("%s:%s", keyBytes, r.Values[i])
if i < len(r.Keys)-1 {
result += ","
}
}
result += "}"
return []byte(result), nil
}

func GetProperty[T PropertyValue](e Entity, key string) (T, error) {
var val T
rawVal, ok := e.GetProperties()[key]
Expand Down

0 comments on commit fb75de8

Please sign in to comment.