-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpact.go
105 lines (89 loc) ยท 1.96 KB
/
pact.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"fmt"
"log"
)
type pactv4 struct {
Consumer application
Provider application
Interactions []interface{} `json:"-"` // This is polymorphic, so we need a custom marshaller
RawInteractions []json.RawMessage `json:"interactions"`
}
func (p *pactv4) UnmarshalJSON(b []byte) error {
type pact pactv4
err := json.Unmarshal(b, (*pact)(p))
if err != nil {
return err
}
for _, raw := range p.RawInteractions {
var v interaction
err = json.Unmarshal(raw, &v)
if err != nil {
return err
}
var i interface{}
switch v.Type {
case "Asynchronous/Messages":
i = &asyncMessageInteraction{}
case "Synchronous/Messages":
i = &syncMessageInteraction{}
case "Synchronous/HTTP":
i = &httpInteraction{}
default:
return fmt.Errorf("unknown interaction type: '%s'", v.Type)
}
log.Println("identified narrow type:", i)
err = json.Unmarshal(raw, i)
if err != nil {
return err
}
log.Println("unmarshalled into narrow type:", i)
p.Interactions = append(p.Interactions, i)
}
return nil
}
type interaction struct {
Type string
Key string
}
type application struct {
Name string
}
type httpInteraction struct {
interaction
Request httpRequest
Response httpResponse
}
type syncMessageInteraction struct {
interaction
Request messageRequest
Response []syncMessageResponse
}
type asyncMessageInteraction struct {
interaction
Contents contents
}
type messageRequest struct {
Contents contents
}
type httpResponse struct {
Body bodyContent
}
// NOTE: only mapping parts of the spec required. Excluding headers, query etc.
// If you need additional fields please update and submit a PR
type httpRequest struct {
Body bodyContent
}
type syncMessageResponse struct {
Contents contents
}
type contents struct {
Content string
}
type bodyContent struct {
Content string // TODO: should be interface{} ?
ContentType string
ContentTypeHint string
Encoded bool
}