Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 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 | |
| 27 | type RpcEventHandler struct { |
| 28 | Response proto.Message |
| 29 | Status *status.Status |
| 30 | Data []byte |
| 31 | Fields map[string]map[string]interface{} |
| 32 | } |
| 33 | |
| 34 | func (h *RpcEventHandler) OnResolveMethod(*desc.MethodDescriptor) { |
| 35 | } |
| 36 | |
| 37 | func (h *RpcEventHandler) OnSendHeaders(metadata.MD) { |
| 38 | } |
| 39 | |
| 40 | func (h *RpcEventHandler) OnReceiveHeaders(metadata.MD) { |
| 41 | } |
| 42 | |
| 43 | func (h *RpcEventHandler) OnReceiveResponse(m proto.Message) { |
| 44 | h.Response = m |
| 45 | } |
| 46 | |
| 47 | func (h *RpcEventHandler) OnReceiveTrailers(s *status.Status, m metadata.MD) { |
| 48 | h.Status = s |
| 49 | } |
| 50 | |
| 51 | func (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 { |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 67 | err := dmsg.TrySetFieldByName(k, v) |
| 68 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 69 | Error.Fatalf("Failed to set field %s in proto %s, err %v\n", k, dmsg.XXX_MessageName(), err.Error()) |
Scott Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 70 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 71 | } |
| 72 | delete(h.Fields, dmsg.XXX_MessageName()) |
| 73 | |
| 74 | return nil |
| 75 | } |