forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnucleus.go
93 lines (78 loc) · 2.59 KB
/
nucleus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (C) 2013-2017, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
// Use of this source code is governed by GPLv3 found in the LICENSE file
//----------------------------------------------------------------------------------------
// Nucleus provides an interface for an execution environment interface for chains and their entries
// and factory code for creating nucleii instances
package holochain
import (
"errors"
"fmt"
"sort"
"strings"
)
type NucleusFactory func(h *Holochain, code string) (Nucleus, error)
type InterfaceSchemaType int
const (
STRING InterfaceSchemaType = iota
JSON
)
const (
ID_PROPERTY = "_id"
AGENT_ID_PROPERTY = "_agent_id"
)
// Interface holds the name and schema of an DNA exposed function
type Interface struct {
Name string
Schema InterfaceSchemaType
}
// Nucleus type abstracts the functions of code execution environments
type Nucleus interface {
Type() string
ValidateEntry(def *EntryDef, entry interface{}) error
InitChain() error
expose(iface Interface) error
Interfaces() (i []Interface)
Call(iface string, params interface{}) (interface{}, error)
}
var nucleusFactories = make(map[string]NucleusFactory)
// InterfaceSchema returns a functions schema type
func InterfaceSchema(n Nucleus, name string) (InterfaceSchemaType, error) {
i := n.Interfaces()
for _, f := range i {
if f.Name == name {
return f.Schema, nil
}
}
return -1, errors.New("function not found: " + name)
}
// RegisterNucleus sets up a Nucleus to be used by the CreateNucleus function
func RegisterNucleus(name string, factory NucleusFactory) {
if factory == nil {
panic("Nucleus factory for type %s does not exist." + name)
}
_, registered := nucleusFactories[name]
if registered {
panic("Nucleus factory for type %s already registered. " + name)
}
nucleusFactories[name] = factory
}
// RegisterBultinNucleii adds the built in nucleus types to the factory hash
func RegisterBultinNucleii() {
RegisterNucleus(ZygoNucleusType, NewZygoNucleus)
RegisterNucleus(JSNucleusType, NewJSNucleus)
}
// CreateNucleus returns a new Nucleus of the given type
func CreateNucleus(h *Holochain, nucleusType string, code string) (Nucleus, error) {
factory, ok := nucleusFactories[nucleusType]
if !ok {
// Factory has not been registered.
// Make a list of all available nucleus factories for error.
available := make([]string, 0)
for k := range nucleusFactories {
available = append(available, k)
}
sort.Strings(available)
return nil, fmt.Errorf("Invalid nucleus name. Must be one of: %s", strings.Join(available, ", "))
}
return factory(h, code)
}