-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
45 lines (38 loc) · 870 Bytes
/
handler.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
42
43
44
45
package jschema
import (
"encoding/json"
"math/big"
"reflect"
"time"
)
// Hijack is the custom handler for a special type.
// scm is the parsed schema without this handler, modify it to produce the final schema.
type Hijack func(scm *Schema)
func (s Schemas) Hijack(v interface{}, h Hijack) {
r := s.RefT(reflect.TypeOf(v))
s.handlers[r] = h
}
func (s Schemas) getHijack(r Ref) Hijack {
if h, has := s.handlers[r]; has {
return h
}
return nil
}
func (s Schemas) HijackTime() {
s.Hijack(time.Time{}, func(scm *Schema) {
scm.Type = TypeString
scm.AdditionalProperties = nil
})
}
func (s Schemas) HijackBigInt() {
s.Hijack(big.Int{}, func(scm *Schema) {
scm.Type = TypeNumber
scm.AdditionalProperties = nil
})
}
func (s Schemas) HijackJSONRawMessage() {
s.Hijack(json.RawMessage{}, func(scm *Schema) {
scm.Type = ""
scm.Items = nil
})
}