github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/entry_del.go (about)

     1  package holochain
     2  
     3  import (
     4  	"encoding/json"
     5  	. "github.com/holochain/holochain-proto/hash"
     6  )
     7  
     8  const (
     9    DelEntryType = SysEntryTypePrefix + "del"
    10    DelEntrySchema = `
    11  {
    12    "$id": "http://example.com/example.json",
    13    "type": "object",
    14    "definitions": {},
    15    "$schema": "http://json-schema.org/draft-07/schema#",
    16    "properties": {
    17      "Hash": {
    18        "$id": "/properties/Hash",
    19        "type": "string",
    20        "title": "The Hash Schema ",
    21        "default": ""
    22      },
    23      "Message": {
    24        "$id": "/properties/message",
    25        "type": "string",
    26        "title": "The Message Schema ",
    27        "default": ""
    28      }
    29    },
    30    "required": ["Hash"]
    31  }
    32  `
    33  )
    34  
    35  // DelEntry struct holds the record of an entry's deletion
    36  type DelEntry struct {
    37  	Hash Hash
    38  	Message string
    39  }
    40  
    41  var DelEntryDef = &EntryDef{Name: DelEntryType, DataFormat: DataFormatJSON, Sharing: Public, Schema: DelEntrySchema}
    42  
    43  func (e *DelEntry) ToJSON() (encodedEntry string, err error) {
    44  	var x struct {
    45  		Hash string
    46  		Message string
    47  	}
    48  	x.Hash = e.Hash.String()
    49  	x.Message = e.Message
    50  	var j []byte
    51  	j, err = json.Marshal(x)
    52  	encodedEntry = string(j)
    53  	return
    54  }
    55  
    56  func DelEntryFromJSON(j string) (entry DelEntry, err error) {
    57  	var x struct {
    58  		Hash    string
    59  		Message string
    60  	}
    61  	err = json.Unmarshal([]byte(j), &x)
    62  	if err != nil {
    63  		return
    64  	}
    65  	entry.Message = x.Message
    66  	entry.Hash, err = NewHash(x.Hash)
    67  	return
    68  }