|
| 1 | +package ast |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "strings" |
| 6 | + "unicode/utf8" |
| 7 | + |
| 8 | + "github.com/dipdup-net/go-lib/tools/consts" |
| 9 | + "github.com/dipdup-net/go-lib/tools/forge" |
| 10 | + "github.com/dipdup-net/go-lib/tools/formatter" |
| 11 | +) |
| 12 | + |
| 13 | +// ChestKey - |
| 14 | +type ChestKey struct { |
| 15 | + Default |
| 16 | +} |
| 17 | + |
| 18 | +// NewChestKey - |
| 19 | +func NewChestKey(depth int) *ChestKey { |
| 20 | + return &ChestKey{ |
| 21 | + Default: NewDefault(consts.CHESTKEY, 0, depth), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// ToJSONSchema - |
| 26 | +func (c *ChestKey) ToJSONSchema() (*JSONSchema, error) { |
| 27 | + return getStringJSONSchema(c.Default), nil |
| 28 | +} |
| 29 | + |
| 30 | +// Compare - |
| 31 | +func (c *ChestKey) Compare(second Comparable) (int, error) { |
| 32 | + s, ok := second.(*ChestKey) |
| 33 | + if !ok { |
| 34 | + return 0, consts.ErrTypeIsNotComparable |
| 35 | + } |
| 36 | + return strings.Compare(c.Value.(string), s.Value.(string)), nil |
| 37 | +} |
| 38 | + |
| 39 | +// Distinguish - |
| 40 | +func (c *ChestKey) Distinguish(x Distinguishable) (*MiguelNode, error) { |
| 41 | + second, ok := x.(*ChestKey) |
| 42 | + if !ok { |
| 43 | + return nil, nil |
| 44 | + } |
| 45 | + |
| 46 | + return c.Default.Distinguish(&second.Default) |
| 47 | +} |
| 48 | + |
| 49 | +// FromJSONSchema - |
| 50 | +func (c *ChestKey) FromJSONSchema(data map[string]interface{}) error { |
| 51 | + return setBytesJSONSchema(&c.Default, data) |
| 52 | +} |
| 53 | + |
| 54 | +// FindByName - |
| 55 | +func (c *ChestKey) FindByName(name string, isEntrypoint bool) Node { |
| 56 | + if c.GetName() == name { |
| 57 | + return c |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// ToMiguel - |
| 63 | +func (c *ChestKey) ToMiguel() (*MiguelNode, error) { |
| 64 | + node, err := c.Default.ToMiguel() |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + if str, ok := node.Value.(string); ok { |
| 70 | + tree := forge.TryUnpackString(str) |
| 71 | + if tree != nil { |
| 72 | + treeJSON, err := json.MarshalToString(tree) |
| 73 | + if err == nil { |
| 74 | + node.Value, _ = formatter.MichelineToMichelsonInline(treeJSON) |
| 75 | + } |
| 76 | + } else if data, err := hex.DecodeString(str); err == nil && utf8.Valid(data) { |
| 77 | + node.Value = string(data) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return node, nil |
| 82 | +} |
0 commit comments