blob: 3c4b0fff66ded733ac9b5d9dc4ced90d54ca3a1c [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package commands
17
18import (
19 "github.com/golang/protobuf/proto"
20 "github.com/jhump/protoreflect/desc"
21 "github.com/jhump/protoreflect/dynamic"
22 "google.golang.org/grpc/metadata"
23 "google.golang.org/grpc/status"
24 "io"
25)
26
27type RpcEventHandler struct {
28 Response proto.Message
29 Status *status.Status
30 Data []byte
31 Fields map[string]map[string]interface{}
32}
33
34func (h *RpcEventHandler) OnResolveMethod(*desc.MethodDescriptor) {
35}
36
37func (h *RpcEventHandler) OnSendHeaders(metadata.MD) {
38}
39
40func (h *RpcEventHandler) OnReceiveHeaders(metadata.MD) {
41}
42
43func (h *RpcEventHandler) OnReceiveResponse(m proto.Message) {
44 h.Response = m
45}
46
47func (h *RpcEventHandler) OnReceiveTrailers(s *status.Status, m metadata.MD) {
48 h.Status = s
49}
50
51func (h *RpcEventHandler) GetParams(msg proto.Message) error {
52 dmsg, err := dynamic.AsDynamicMessage(msg)
53 if err != nil {
54 return err
55 }
56
57 if h.Fields == nil || len(h.Fields) == 0 {
58 return io.EOF
59 }
60
61 fields, ok := h.Fields[dmsg.XXX_MessageName()]
62 if !ok {
63 return nil
64 }
65
66 for k, v := range fields {
67 dmsg.TrySetFieldByName(k, v)
68 }
69 delete(h.Fields, dmsg.XXX_MessageName())
70
71 return nil
72}