internal/mcp: remove omitzero
To be compatible with Go 1.23, we cannot use the encoding/json "omitzero" option. Replace with a custom MarshalJSON method. Change-Id: Ied615ea04fb20e2459f9ed20ef03c81edbc74961 Reviewed-on: https://go-review.googlesource.com/c/tools/+/672417 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
dac5be2c50
commit
aedbba98ee
1 changed files with 73 additions and 3 deletions
|
|
@ -6,6 +6,7 @@ package mcp
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
|
|
@ -23,7 +24,7 @@ type Content struct {
|
|||
Type string `json:"type"`
|
||||
Text string `json:"text,omitempty"`
|
||||
MIMEType string `json:"mimeType,omitempty"`
|
||||
Data []byte `json:"data,omitzero"`
|
||||
Data []byte `json:"data,omitempty"`
|
||||
Resource *ResourceContents `json:"resource,omitempty"`
|
||||
Annotations *Annotations `json:"annotations,omitempty"`
|
||||
}
|
||||
|
|
@ -68,8 +69,34 @@ func NewResourceContent(resource ResourceContents) Content {
|
|||
type ResourceContents struct {
|
||||
URI string `json:"uri,"`
|
||||
MIMEType string `json:"mimeType,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Blob []byte `json:"blob,omitzero"`
|
||||
Text string `json:"text"`
|
||||
Blob []byte `json:"blob,omitempty"`
|
||||
}
|
||||
|
||||
func (r ResourceContents) MarshalJSON() ([]byte, error) {
|
||||
if r.URI == "" {
|
||||
return nil, errors.New("ResourceContents missing URI")
|
||||
}
|
||||
if r.Blob == nil {
|
||||
// Text. Marshal normally.
|
||||
type wireResourceContents ResourceContents
|
||||
return json.Marshal((wireResourceContents)(r))
|
||||
}
|
||||
// Blob.
|
||||
if r.Text != "" {
|
||||
return nil, errors.New("ResourceContents has non-zero Text and Blob fields")
|
||||
}
|
||||
// r.Blob may be the empty slice, so marshal with an alternative definition.
|
||||
br := struct {
|
||||
URI string `json:"uri,omitempty"`
|
||||
MIMEType string `json:"mimeType,omitempty"`
|
||||
Blob []byte `json:"blob"`
|
||||
}{
|
||||
URI: r.URI,
|
||||
MIMEType: r.MIMEType,
|
||||
Blob: r.Blob,
|
||||
}
|
||||
return json.Marshal(br)
|
||||
}
|
||||
|
||||
func NewTextResourceContents(uri, mimeType, text string) ResourceContents {
|
||||
|
|
|
|||
Loading…
Reference in a new issue