@@ -15,6 +15,7 @@ import (
15
15
//
16
16
// ToWire converts content to its jsonrpc2 wire format.
17
17
type Content interface {
18
+ // TODO: unexport this, and move the tests that use it to this package.
18
19
ToWire () protocol.Content
19
20
}
20
21
@@ -29,64 +30,69 @@ func (c TextContent) ToWire() protocol.Content {
29
30
30
31
// ImageContent contains base64-encoded image data.
31
32
type ImageContent struct {
32
- Data string
33
- MimeType string
33
+ Data [] byte // base64-encoded
34
+ MIMEType string
34
35
}
35
36
36
37
func (c ImageContent ) ToWire () protocol.Content {
37
- return protocol.Content {Type : "image" , MIMEType : c .MimeType , Data : c .Data }
38
+ return protocol.Content {Type : "image" , MIMEType : c .MIMEType , Data : c .Data }
38
39
}
39
40
40
41
// AudioContent contains base64-encoded audio data.
41
42
type AudioContent struct {
42
- Data string
43
- MimeType string
43
+ Data [] byte
44
+ MIMEType string
44
45
}
45
46
46
47
func (c AudioContent ) ToWire () protocol.Content {
47
- return protocol.Content {Type : "audio" , MIMEType : c .MimeType , Data : c .Data }
48
+ return protocol.Content {Type : "audio" , MIMEType : c .MIMEType , Data : c .Data }
48
49
}
49
50
50
51
// ResourceContent contains embedded resources.
51
52
type ResourceContent struct {
52
- Resource Resource
53
+ Resource EmbeddedResource
53
54
}
54
55
55
56
func (r ResourceContent ) ToWire () protocol.Content {
56
- res := r .Resource .ToWire ()
57
+ res := r .Resource .toWire ()
57
58
return protocol.Content {Type : "resource" , Resource : & res }
58
59
}
59
60
60
- type Resource interface {
61
- ToWire () protocol.Resource
61
+ type EmbeddedResource interface {
62
+ toWire () protocol.ResourceContents
62
63
}
63
64
64
- type TextResource struct {
65
+ // The {Text,Blob}ResourceContents types match the protocol definitions,
66
+ // but we represent both as a single type on the wire.
67
+
68
+ // A TextResourceContents is the contents of a text resource.
69
+ type TextResourceContents struct {
65
70
URI string
66
- MimeType string
71
+ MIMEType string
67
72
Text string
68
73
}
69
74
70
- func (r TextResource ) ToWire () protocol.Resource {
71
- return protocol.Resource {
75
+ func (r TextResourceContents ) toWire () protocol.ResourceContents {
76
+ return protocol.ResourceContents {
72
77
URI : r .URI ,
73
- MIMEType : r .MimeType ,
78
+ MIMEType : r .MIMEType ,
74
79
Text : r .Text ,
80
+ // Blob is nil, indicating this is a TextResourceContents.
75
81
}
76
82
}
77
83
78
- type BlobResource struct {
84
+ // A BlobResourceContents is the contents of a blob resource.
85
+ type BlobResourceContents struct {
79
86
URI string
80
- MimeType string
81
- Blob string
87
+ MIMEType string
88
+ Blob [] byte
82
89
}
83
90
84
- func (r BlobResource ) ToWire () protocol.Resource {
85
- blob := r .Blob
86
- return protocol.Resource {
91
+ func (r BlobResourceContents ) toWire () protocol.ResourceContents {
92
+ return protocol.ResourceContents {
87
93
URI : r .URI ,
88
- MIMEType : r .MimeType ,
89
- Blob : & blob ,
94
+ MIMEType : r .MIMEType ,
95
+ Blob : r . Blob ,
90
96
}
91
97
}
92
98
@@ -97,22 +103,22 @@ func ContentFromWireContent(c protocol.Content) Content {
97
103
case "text" :
98
104
return TextContent {Text : c .Text }
99
105
case "image" :
100
- return ImageContent {Data : c .Data , MimeType : c .MIMEType }
106
+ return ImageContent {Data : c .Data , MIMEType : c .MIMEType }
101
107
case "audio" :
102
- return AudioContent {Data : c .Data , MimeType : c .MIMEType }
108
+ return AudioContent {Data : c .Data , MIMEType : c .MIMEType }
103
109
case "resource" :
104
110
r := ResourceContent {}
105
111
if c .Resource != nil {
106
112
if c .Resource .Blob != nil {
107
- r .Resource = BlobResource {
113
+ r .Resource = BlobResourceContents {
108
114
URI : c .Resource .URI ,
109
- MimeType : c .Resource .MIMEType ,
110
- Blob : * c .Resource .Blob ,
115
+ MIMEType : c .Resource .MIMEType ,
116
+ Blob : c .Resource .Blob ,
111
117
}
112
118
} else {
113
- r .Resource = TextResource {
119
+ r .Resource = TextResourceContents {
114
120
URI : c .Resource .URI ,
115
- MimeType : c .Resource .MIMEType ,
121
+ MIMEType : c .Resource .MIMEType ,
116
122
Text : c .Resource .Text ,
117
123
}
118
124
}
0 commit comments