Skip to content

Commit

Permalink
feat: triple options (#33)
Browse files Browse the repository at this point in the history
* feat: add optional Options to triple values

* feat: add options to triple value type

* feat: make each options field optional

* feat: changeset
  • Loading branch information
baiirun authored Feb 18, 2025
1 parent f906741 commit 424b98e
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 141 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-coats-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphprotocol/grc-20": minor
---

Add triple value options to encoding
2 changes: 1 addition & 1 deletion src/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type PublishEditProposalArgs = {
export async function publishEdit(args: PublishEditProposalArgs): Promise<string> {
const { name, ops, author } = args;

const edit = EditProposal.make({ name, ops, author });
const edit = EditProposal.encode({ name, ops, author });

const blob = new Blob([edit], { type: 'application/octet-stream' });
const formData = new FormData();
Expand Down
57 changes: 51 additions & 6 deletions src/proto/edit.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { describe, expect, it } from 'vitest';

import { Relation } from '../relation.js';
import { make } from './edit.js';
import { encode } from './edit.js';
import { ActionType, Edit, OpType, ValueType } from './gen/src/proto/ipfs_pb.js';

describe('create-edit-proposal', () => {
describe('Edit', () => {
it('encodes and decodes Edit with SET_TRIPLE ops correctly', () => {
const editBinary = make({
const editBinary = encode({
name: 'test',
ops: [
{
Expand Down Expand Up @@ -43,10 +43,54 @@ describe('create-edit-proposal', () => {
},
},
]);

const editBinaryWithOptions = encode({
name: 'test',
ops: [
{
type: 'SET_TRIPLE',
triple: {
attribute: 'test-attribute-id',
entity: 'test-entity-id',
value: {
type: 'TEXT',
value: 'test value',
options: {
format: 'format'
}
},
},
},
],
author: '0x1234',
});

const resultWithOptions = Edit.fromBinary(editBinaryWithOptions);
expect(resultWithOptions.name).toBe('test');
expect(resultWithOptions.type).toBe(ActionType.ADD_EDIT);
expect(resultWithOptions.version).toBe('1.0.0');
expect(resultWithOptions.ops.length).toBe(1);
expect(resultWithOptions.ops).toEqual([
{
type: OpType.SET_TRIPLE,
triples: [],
triple: {
attribute: 'test-attribute-id',
entity: 'test-entity-id',
value: {
type: ValueType.TEXT,
value: 'test value',
options: {
format: 'format',
}
},
},
},
]);
});

it('encodes and decodes Edit with DELETE_TRIPLE ops correctly', () => {
const editBinary = make({
const editBinary = encode({
name: 'test',
ops: [
{
Expand Down Expand Up @@ -78,7 +122,7 @@ describe('create-edit-proposal', () => {
});

it('encodes and decoded Edit with CREATE_RELATION ops correctly', () => {
const editBinary = make({
const editBinary = encode({
name: 'test',
ops: [
Relation.make({
Expand Down Expand Up @@ -113,7 +157,7 @@ describe('create-edit-proposal', () => {
});

it('encodes and decoded Edit with CREATE_RELATION ops correctly', () => {
const editBinary = make({
const editBinary = encode({
name: 'test',
ops: [
{
Expand Down Expand Up @@ -145,4 +189,5 @@ describe('create-edit-proposal', () => {
},
]);
});

});
2 changes: 1 addition & 1 deletion src/proto/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type MakeEditProposalArgs = {
author: string;
};

export function make({ name, ops, author }: MakeEditProposalArgs): Uint8Array {
export function encode({ name, ops, author }: MakeEditProposalArgs): Uint8Array {
return new Edit({
type: ActionType.ADD_EDIT,
version: '1.0.0',
Expand Down
Loading

0 comments on commit 424b98e

Please sign in to comment.