-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptor.go
98 lines (85 loc) · 3.57 KB
/
descriptor.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
94
95
96
97
98
package module
import (
"github.com/fdymylja/tmos/core/meta"
"github.com/fdymylja/tmos/core/module"
"github.com/fdymylja/tmos/pkg/protoutils/desc"
"github.com/fdymylja/tmos/runtime/authentication"
"github.com/fdymylja/tmos/runtime/authorization"
"github.com/fdymylja/tmos/runtime/orm/schema"
"github.com/fdymylja/tmos/runtime/statetransition"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
// RawDescriptor aliases module.Descriptor
type RawDescriptor = module.Descriptor
// Descriptor describes the full functionality set of a Module
type Descriptor struct {
Name string
GenesisHandler GenesisHandler
StateTransitionAdmissionHandlers []stateTransitionAdmissionHandler
StateTransitionPreExecHandlers []stateTransitionPreExecutionHandler
StateTransitionExecutionHandlers []stateTransitionExecutionHandler
StateTransitionPostExecutionHandlers []stateTransitionPostExecutionHandler
StateObjects []stateObject
Needs []meta.StateTransition
AuthAdmissionHandlers []authentication.AdmissionHandler
PostAuthenticationHandler []authentication.PostAuthenticationHandler
Services []ExtensionService
Authorizer authorization.Authorizer
}
func (d Descriptor) Raw() *module.Descriptor {
x := new(module.Descriptor)
x.Name = d.Name
x.StateObjects = make([]*module.StateObject, len(d.StateObjects))
for i, so := range d.StateObjects {
x.StateObjects[i] = &module.StateObject{
ApiDefinition: so.StateObject.APIDefinition(),
SchemaDefinition: so.SchemaDefinition,
ProtobufFullname: (string)(so.StateObject.ProtoReflect().Descriptor().FullName()),
ProtoDependencies: getDeps(so.StateObject.ProtoReflect().Descriptor().ParentFile()),
}
}
x.StateTransitions = make([]*module.StateTransition, len(d.StateTransitionExecutionHandlers))
for i, st := range d.StateTransitionExecutionHandlers {
x.StateTransitions[i] = &module.StateTransition{
ApiDefinition: st.StateTransition.APIDefinition(),
ProtobufFullname: (string)(st.StateTransition.ProtoReflect().Descriptor().FullName()),
ProtoDependencies: getDeps(st.StateTransition.ProtoReflect().Descriptor().ParentFile()),
}
}
x.RequiredResources = make([]*module.Dependency, len(d.Needs))
for i, st := range d.Needs {
x.RequiredResources[i] = &module.Dependency{Resource: st.APIDefinition()}
}
return x
}
func getDeps(file protoreflect.FileDescriptor) []*descriptorpb.FileDescriptorProto {
deps := desc.Dependencies(file)
rawDeps := make([]*descriptorpb.FileDescriptorProto, 0, len(deps)+1)
for _, x := range deps {
rawDeps = append(rawDeps, protodesc.ToFileDescriptorProto(x))
}
rawDeps = append(rawDeps, protodesc.ToFileDescriptorProto(file))
return rawDeps
}
type stateObject struct {
StateObject meta.StateObject
SchemaDefinition *schema.Definition
}
type stateTransitionAdmissionHandler struct {
StateTransition meta.StateTransition
AdmissionHandler statetransition.AdmissionHandler
}
type stateTransitionPreExecutionHandler struct {
StateTransition meta.StateTransition
Handler statetransition.PreExecutionHandler
}
type stateTransitionExecutionHandler struct {
StateTransition meta.StateTransition
Handler statetransition.ExecutionHandler
}
type stateTransitionPostExecutionHandler struct {
StateTransition statetransition.StateTransition
Handler statetransition.PostExecutionHandler
}