-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
41 lines (33 loc) · 960 Bytes
/
meta.go
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
package ferry
import (
"fmt"
"reflect"
"runtime"
"strings"
)
// meta contains information about service method.
type meta struct {
name string
body map[string]string
query map[string]string
}
// buildMeta uses reflection to determine service name and method name.
func buildMeta(function, request interface{}) (meta, error) {
name := runtime.FuncForPC(reflect.ValueOf(function).Pointer()).Name()
// -fm is a suffix for functions that have receiver.
nameParts := strings.Split(strings.TrimSuffix(name, "-fm"), ".")
if len(nameParts) < 2 {
return meta{}, fmt.Errorf("can not use %q as handler", name)
}
m := meta{
name: nameParts[len(nameParts)-1],
}
var err error
if m.body, err = jsonMapping(request); err != nil {
return meta{}, fmt.Errorf("can not create json mapping: %w", err)
}
if m.query, err = queryMapping(request); err != nil {
return meta{}, fmt.Errorf("can not create query mapping: %w", err)
}
return m, nil
}