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

     1  package holochain
     2  
     3  import (
     4  	"encoding/json"
     5  	. "github.com/holochain/holochain-proto/hash"
     6  )
     7  
     8  const (
     9  	MigrateEntryType = SysEntryTypePrefix + "migrate"
    10  	// currently both to/from have the same schema
    11  	MigrateEntrySchema = `
    12  {
    13    "$id": "http://example.com/example.json",
    14    "type": "object",
    15    "definitions": {},
    16    "$schema": "http://json-schema.org/draft-07/schema#",
    17    "properties": {
    18  		"Type": {
    19  			"$id": "/properties/Type",
    20  			"type": "string",
    21  			"title": "The Type Schema ",
    22  			"default": ""
    23  		},
    24      "DNAHash": {
    25        "$id": "/properties/DNAHash",
    26        "type": "string",
    27        "title": "The DNAHash Schema ",
    28        "default": ""
    29      },
    30      "Key": {
    31        "$id": "/properties/Key",
    32        "type": "string",
    33        "title": "The Key Schema ",
    34        "default": ""
    35      },
    36      "Data": {
    37        "$id": "/properties/Data",
    38        "type": "string",
    39        "title": "The Data Schema ",
    40        "default": ""
    41      }
    42    },
    43    "required": ["Type", "DNAHash", "Key"]
    44  }
    45  `
    46  
    47  	// Type can only be one of two things... open or close
    48  	MigrateEntryTypeClose = "close"
    49  	MigrateEntryTypeOpen  = "open"
    50  )
    51  
    52  // MigrateEntry struct is the record of a chain opening or closing
    53  type MigrateEntry struct {
    54  	Type  string
    55  	DNAHash Hash
    56  	Key  Hash
    57  	Data  string
    58  }
    59  
    60  var MigrateEntryDef = &EntryDef{Name: MigrateEntryType, DataFormat: DataFormatJSON, Sharing: Public, Schema: MigrateEntrySchema}
    61  
    62  // @see https://github.com/holochain/holochain-proto/issues/731
    63  func (e *MigrateEntry) Def() *EntryDef {
    64  	return MigrateEntryDef
    65  }
    66  
    67  func (e *MigrateEntry) ToJSON() (encodedEntry string, err error) {
    68  	var x struct {
    69  		Type string
    70  		DNAHash string
    71  		Key  string
    72  		Data  string
    73  	}
    74  	x.Type = e.Type
    75  	x.DNAHash = e.DNAHash.String()
    76  	x.Key = e.Key.String()
    77  	x.Data = e.Data
    78  	var j []byte
    79  	j, err = json.Marshal(x)
    80  	encodedEntry = string(j)
    81  	return
    82  }
    83  
    84  func MigrateEntryFromJSON(j string) (entry MigrateEntry, err error) {
    85  	var x struct {
    86  		Type  string
    87  		DNAHash string
    88  		Key  string
    89  		Data  string
    90  	}
    91  	err = json.Unmarshal([]byte(j), &x)
    92  	if err != nil {
    93  		return
    94  	}
    95  
    96  	entry.Type = x.Type
    97  	entry.DNAHash, err = NewHash(x.DNAHash)
    98  	entry.Key, err = NewHash(x.Key)
    99  	entry.Data = x.Data
   100  	return
   101  }