-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRpcFunction.go
More file actions
69 lines (59 loc) · 2.11 KB
/
Copy pathRpcFunction.go
File metadata and controls
69 lines (59 loc) · 2.11 KB
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
package jrm1
import (
"encoding/json"
"fmt"
"reflect"
"runtime"
"strings"
au "github.com/vault-thirteen/auxie/unicode"
)
const AsciiLowLine = '_'
const (
ErrFBadSymbolInFunctionName = "bad symbol in function name: %v"
ErrFUnsupportedFormatOfFunctionName = "unsupported format of function name: %v"
)
// RpcFunction represents a signature for an RPC function (method, procedure).
type RpcFunction func(params *json.RawMessage, metaData *ResponseMetaData) (result any, re *RpcError)
// GetName reads name of the RPC function (method, procedure).
//
// Do note that names of anonymous functions are not usable in practice while
// Go language has an unstable API and ABI.
//
// Go internal ABI specification is described at the following link:
// https://go.googlesource.com/go/+/refs/heads/dev.regabi/src/cmd/compile/internal-abi.md
//
// This document clearly states that ABI of Go language is unstable.
// -----------------------------------------------------------------------------
// This ABI is unstable and will change between Go versions. If you’re writing
// assembly code, please instead refer to Go’s assembly documentation, which
// describes Go’s stable ABI, known as ABI0.
// -----------------------------------------------------------------------------
func (f RpcFunction) GetName() string {
fullName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
parts := strings.Split(fullName, ".")
stageOneName := parts[len(parts)-1]
parts = strings.Split(stageOneName, "-")
switch len(parts) {
case 1:
return stageOneName
case 2:
return parts[0]
default:
err := fmt.Errorf(ErrFUnsupportedFormatOfFunctionName, stageOneName)
panic(err)
}
}
// CheckFunctionName verifies name of a function.
func CheckFunctionName(fn string) (err error) {
runes := []rune(fn)
for _, r := range runes {
if !isValidSymbolForFuncName(r) {
return fmt.Errorf(ErrFBadSymbolInFunctionName, string(r))
}
}
return nil
}
// isValidSymbolForFuncName verifies a symbol for a function name.
func isValidSymbolForFuncName(s rune) bool {
return au.SymbolIsNumber(s) || au.SymbolIsLatLetter(s) || (s == AsciiLowLine)
}