isPyCompatFunc checks if function signature is a python-compatible function. Returns nil if function is compatible, err message if not. Also returns the return type of the function haserr is true if 2nd arg is an error type, which is only supported form of multi-return-value functions hasfun is true
(sig *types.Signature)
| 151 | // supported form of multi-return-value functions |
| 152 | // hasfun is true if one of the args is a function signature |
| 153 | func isPyCompatFunc(sig *types.Signature) (ret types.Type, haserr, hasfun bool, err error) { |
| 154 | res := sig.Results() |
| 155 | |
| 156 | switch res.Len() { |
| 157 | case 2: |
| 158 | if !isErrorType(res.At(1).Type()) { |
| 159 | err = fmt.Errorf("gopy: second result value must be of type error: %s", sig.String()) |
| 160 | return |
| 161 | } |
| 162 | haserr = true |
| 163 | ret = res.At(0).Type() |
| 164 | case 1: |
| 165 | if isErrorType(res.At(0).Type()) { |
| 166 | haserr = true |
| 167 | ret = nil |
| 168 | } else { |
| 169 | ret = res.At(0).Type() |
| 170 | } |
| 171 | case 0: |
| 172 | ret = nil |
| 173 | default: |
| 174 | err = fmt.Errorf("gopy: too many results to return: %s", sig.String()) |
| 175 | return |
| 176 | } |
| 177 | |
| 178 | if ret != nil { |
| 179 | if err = isPyCompatType(ret); err != nil { |
| 180 | return |
| 181 | } |
| 182 | if _, isSig := ret.Underlying().(*types.Signature); isSig { |
| 183 | err = fmt.Errorf("gopy: return type is signature") |
| 184 | return |
| 185 | } |
| 186 | if ret.Underlying().String() == "interface{}" { |
| 187 | err = fmt.Errorf("gopy: return type is interface{}") |
| 188 | return |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | args := sig.Params() |
| 193 | nargs := args.Len() |
| 194 | for i := 0; i < nargs; i++ { |
| 195 | arg := args.At(i) |
| 196 | argt := arg.Type() |
| 197 | if err = isPyCompatType(argt); err != nil { |
| 198 | return |
| 199 | } |
| 200 | if _, isSig := argt.Underlying().(*types.Signature); isSig { |
| 201 | if !hasfun { |
| 202 | hasfun = true |
| 203 | } else { |
| 204 | err = fmt.Errorf("gopy: only one function signature arg allowed: %s", sig.String()) |
| 205 | return |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | return |
| 210 | } |
no test coverage detected