Skip to content

Commit

Permalink
patch: doc, extract schema, cleaner bindnode use
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed May 25, 2022
1 parent 61df9d7 commit 14cd881
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 52 deletions.
7 changes: 7 additions & 0 deletions traversal/patch/eval.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// Package patch provides an implementation of the IPLD Patch specification.
// IPLD Patch is a system for declaratively specifying patches to a document,
// which can then be applied to produce a new, modified document.
//
//
// This package is EXPERIMENTAL; its behavior and API might change as it's still
// in development.
package patch

import (
Expand Down
63 changes: 11 additions & 52 deletions traversal/patch/parse.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,25 @@
package patch

import (
_ "embed"

"bytes"
"io"

"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema"

"github.com/ipld/go-ipld-prime/codec/json"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/schema"
)

var ts = func() *schema.TypeSystem {
ts, err := ipld.LoadSchemaBytes(
// This could be more accurately modelled as an inline union,
// but that seems like work, given how high the overlap is.
//
// This schema may also belong in the specs repo,
// but if so, we'd still replicate it here,
// because as a rule, we don't require the specs repo submodule be present for the source to compile (just for all the tests to run).
[]byte(`
# Op represents the kind of operation to perfrom
# The current set is based on the JSON Patch specification
# We may end up adding more operations in the future
type Op enum {
| add
| remove
| replace
| move
| copy
| test
}
//go:embed patch.ipldsch
var embedSchema []byte

# Operation and OperationSequence are the types that describe operations (but not what to apply them on).
# See the Instruction type for describing both operations and what to apply them on.
type Operation struct {
op Op
path String
value optional Any
from optional String
}
type OperationSequence [Operation]
type Instruction struct {
startAt Link
operations OperationSequence
# future: optional field for adl signalling and/or other lenses
}
type InstructionResult union {
| Error "error"
| Link "result"
} representation keyed
type Error struct {
code String # enum forthcoming
message String
details {String:String}
}
`))
var ts = func() *schema.TypeSystem {
ts, err := ipld.LoadSchemaBytes(embedSchema)
if err != nil {
panic(err)
}
Expand All @@ -80,11 +41,9 @@ func Parse(r io.Reader, dec codec.Decoder) ([]Operation, error) {
for _, opRaw := range *opsRaw {
// TODO check the Op string
op := Operation{
Op: Op(opRaw.Op),
Path: datamodel.ParsePath(opRaw.Path),
}
if opRaw.Value != nil {
op.Value = *opRaw.Value
Op: Op(opRaw.Op),
Path: datamodel.ParsePath(opRaw.Path),
Value: opRaw.Value,
}
if opRaw.From != nil {
op.From = datamodel.ParsePath(*opRaw.From)
Expand All @@ -99,6 +58,6 @@ func Parse(r io.Reader, dec codec.Decoder) ([]Operation, error) {
type operationRaw struct {
Op string
Path string
Value *datamodel.Node
Value datamodel.Node
From *string
}
39 changes: 39 additions & 0 deletions traversal/patch/patch.ipldsch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Op represents the kind of operation to perfrom
# The current set is based on the JSON Patch specification
# We may end up adding more operations in the future
type Op enum {
| add
| remove
| replace
| move
| copy
| test
}

# Operation and OperationSequence are the types that describe operations (but not what to apply them on).
# See the Instruction type for describing both operations and what to apply them on.
type Operation struct {
op Op
path String
value optional Any
from optional String
}

type OperationSequence [Operation]

type Instruction struct {
startAt Link
operations OperationSequence
# future: optional field for adl signalling and/or other lenses
}

type InstructionResult union {
| Error "error"
| Link "result"
} representation keyed

type Error struct {
code String # enum forthcoming
message String
details {String:String}
}

0 comments on commit 14cd881

Please sign in to comment.