dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/result.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package protocol 19 20 import ( 21 "fmt" 22 ) 23 24 // Result is a interface that represents RPC result 25 // 26 // SetError method sets error. 27 // 28 // Error method gets error. 29 // 30 // SetResult method sets invoker result. 31 // 32 // Result method gets invoker result. 33 // 34 // SetAttachments method replaces the existing attachments with the specified param. 35 // 36 // # Attachments method gets all attachments 37 // 38 // AddAttachment method adds the specified map to existing attachments in this instance. 39 // 40 // Attachment method gets attachment by key with default value. 41 type Result interface { 42 SetError(error) 43 Error() error 44 SetResult(interface{}) 45 Result() interface{} 46 SetAttachments(map[string]interface{}) 47 Attachments() map[string]interface{} 48 AddAttachment(string, interface{}) 49 Attachment(string, interface{}) interface{} 50 } 51 52 var _ Result = (*RPCResult)(nil) 53 54 // RPCResult is default RPC result. 55 type RPCResult struct { 56 Attrs map[string]interface{} 57 Err error 58 Rest interface{} 59 } 60 61 // SetError sets error. 62 func (r *RPCResult) SetError(err error) { 63 r.Err = err 64 } 65 66 // Error gets error. 67 func (r *RPCResult) Error() error { 68 return r.Err 69 } 70 71 // SetResult sets invoker result. 72 func (r *RPCResult) SetResult(rest interface{}) { 73 r.Rest = rest 74 } 75 76 // Result gets invoker result. 77 func (r *RPCResult) Result() interface{} { 78 return r.Rest 79 } 80 81 // SetAttachments replaces the existing attachments with the specified param. 82 func (r *RPCResult) SetAttachments(attr map[string]interface{}) { 83 r.Attrs = attr 84 } 85 86 // Attachments gets all attachments 87 func (r *RPCResult) Attachments() map[string]interface{} { 88 if r.Attrs == nil { 89 r.Attrs = make(map[string]interface{}) 90 } 91 return r.Attrs 92 } 93 94 // AddAttachment adds the specified map to existing attachments in this instance. 95 func (r *RPCResult) AddAttachment(key string, value interface{}) { 96 if r.Attrs == nil { 97 r.Attrs = make(map[string]interface{}) 98 } 99 r.Attrs[key] = value 100 } 101 102 // Attachment gets attachment by key with default value. 103 func (r *RPCResult) Attachment(key string, defaultValue interface{}) interface{} { 104 if r.Attrs == nil { 105 r.Attrs = make(map[string]interface{}) 106 return nil 107 } 108 v, ok := r.Attrs[key] 109 if !ok { 110 v = defaultValue 111 } 112 return v 113 } 114 115 func (r *RPCResult) String() string { 116 return fmt.Sprintf("&RPCResult{Rest: %v, Attrs: %v, Err: %v}", r.Rest, r.Attrs, r.Err) 117 }