Skip to content

Commit

Permalink
Fix: add chest types
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 13, 2021
1 parent d77187c commit 336ead0
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
83 changes: 83 additions & 0 deletions internal/bcd/ast/chest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ast

import (
"encoding/hex"
"strings"
"unicode/utf8"

"github.com/baking-bad/bcdhub/internal/bcd/consts"
"github.com/baking-bad/bcdhub/internal/bcd/forge"
"github.com/baking-bad/bcdhub/internal/bcd/formatter"
)

// Chest -
type Chest struct {
Default
}

// NewChest -
func NewChest(depth int) *Chest {
return &Chest{
Default: NewDefault(consts.CHEST, 0, depth),
}
}

// ToJSONSchema -
func (c *Chest) ToJSONSchema() (*JSONSchema, error) {
return getStringJSONSchema(c.Default), nil
}

// Compare -
func (c *Chest) Compare(second Comparable) (int, error) {
s, ok := second.(*Chest)
if !ok {
return 0, consts.ErrTypeIsNotComparable
}
return strings.Compare(c.Value.(string), s.Value.(string)), nil
}

// Distinguish -
func (c *Chest) Distinguish(x Distinguishable) (*MiguelNode, error) {
second, ok := x.(*Chest)
if !ok {
return nil, nil
}

return c.Default.Distinguish(&second.Default)
}

// FromJSONSchema -
func (c *Chest) FromJSONSchema(data map[string]interface{}) error {
setBytesJSONSchema(&c.Default, data)
return nil
}

// FindByName -
func (c *Chest) FindByName(name string, isEntrypoint bool) Node {
if c.GetName() == name {
return c
}
return nil
}

// ToMiguel -
func (c *Chest) ToMiguel() (*MiguelNode, error) {
node, err := c.Default.ToMiguel()
if err != nil {
return nil, err
}

if str, ok := node.Value.(string); ok {
tree := forge.TryUnpackString(str)
if tree != nil {
treeJSON, err := json.MarshalToString(tree)
if err == nil {
node.Value, _ = formatter.MichelineToMichelsonInline(treeJSON)
}
} else if data, err := hex.DecodeString(str); err == nil && utf8.Valid(data) {
node.Value = string(data)
}
}

return node, nil
}
83 changes: 83 additions & 0 deletions internal/bcd/ast/chest_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ast

import (
"encoding/hex"
"strings"
"unicode/utf8"

"github.com/baking-bad/bcdhub/internal/bcd/consts"
"github.com/baking-bad/bcdhub/internal/bcd/forge"
"github.com/baking-bad/bcdhub/internal/bcd/formatter"
)

// ChestKey -
type ChestKey struct {
Default
}

// NewChestKey -
func NewChestKey(depth int) *ChestKey {
return &ChestKey{
Default: NewDefault(consts.CHESTKEY, 0, depth),
}
}

// ToJSONSchema -
func (c *ChestKey) ToJSONSchema() (*JSONSchema, error) {
return getStringJSONSchema(c.Default), nil
}

// Compare -
func (c *ChestKey) Compare(second Comparable) (int, error) {
s, ok := second.(*ChestKey)
if !ok {
return 0, consts.ErrTypeIsNotComparable
}
return strings.Compare(c.Value.(string), s.Value.(string)), nil
}

// Distinguish -
func (c *ChestKey) Distinguish(x Distinguishable) (*MiguelNode, error) {
second, ok := x.(*ChestKey)
if !ok {
return nil, nil
}

return c.Default.Distinguish(&second.Default)
}

// FromJSONSchema -
func (c *ChestKey) FromJSONSchema(data map[string]interface{}) error {
setBytesJSONSchema(&c.Default, data)
return nil
}

// FindByName -
func (c *ChestKey) FindByName(name string, isEntrypoint bool) Node {
if c.GetName() == name {
return c
}
return nil
}

// ToMiguel -
func (c *ChestKey) ToMiguel() (*MiguelNode, error) {
node, err := c.Default.ToMiguel()
if err != nil {
return nil, err
}

if str, ok := node.Value.(string); ok {
tree := forge.TryUnpackString(str)
if tree != nil {
treeJSON, err := json.MarshalToString(tree)
if err == nil {
node.Value, _ = formatter.MichelineToMichelsonInline(treeJSON)
}
} else if data, err := hex.DecodeString(str); err == nil && utf8.Valid(data) {
node.Value = string(data)
}
}

return node, nil
}
4 changes: 4 additions & 0 deletions internal/bcd/ast/untyped.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ func typeNode(node *base.Node, depth int, id *int) (Node, error) {
ast = NewBLS12381g2(depth + 1)
case consts.BAKERHASH:
ast = NewBakerHash(depth + 1)
case consts.CHEST:
ast = NewChest(depth + 1)
case consts.CHESTKEY:
ast = NewChestKey(depth + 1)
default:
return nil, errors.Wrap(consts.ErrUnknownPrim, node.Prim)
}
Expand Down

0 comments on commit 336ead0

Please sign in to comment.