diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-02-05 17:24:34 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-05 17:24:34 -0500 |
commit | 288fb688964cb7fc7086d0728daa1f5f6b726dd6 (patch) | |
tree | e2246f42cb5241cc243fbec178324482c211d4a3 /pkg/api/handlers/utils/handler.go | |
parent | 9e2cdc4a849091ba7eb1c5440b8970c819c46419 (diff) | |
parent | c5c946b18fda59ffba826603b7784dc6e7cafda0 (diff) | |
download | podman-288fb688964cb7fc7086d0728daa1f5f6b726dd6.tar.gz podman-288fb688964cb7fc7086d0728daa1f5f6b726dd6.tar.bz2 podman-288fb688964cb7fc7086d0728daa1f5f6b726dd6.zip |
Merge pull request #9237 from mheon/backports_300_RC3
Backports for v3.0,0-RC3
Diffstat (limited to 'pkg/api/handlers/utils/handler.go')
-rw-r--r-- | pkg/api/handlers/utils/handler.go | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/pkg/api/handlers/utils/handler.go b/pkg/api/handlers/utils/handler.go index 517dccad0..b3c674788 100644 --- a/pkg/api/handlers/utils/handler.go +++ b/pkg/api/handlers/utils/handler.go @@ -1,16 +1,17 @@ package utils import ( - "encoding/json" "fmt" "io" "net/http" "net/url" "os" "strings" + "unsafe" "github.com/blang/semver" "github.com/gorilla/mux" + jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -43,8 +44,8 @@ var ( // clients to shop for the Version they wish to support APIVersion = map[VersionTree]map[VersionLevel]semver.Version{ LibpodTree: { - CurrentAPIVersion: semver.MustParse("2.0.0"), - MinimalAPIVersion: semver.MustParse("2.0.0"), + CurrentAPIVersion: semver.MustParse("3.0.0"), + MinimalAPIVersion: semver.MustParse("3.0.0"), }, CompatTree: { CurrentAPIVersion: semver.MustParse("1.40.0"), @@ -144,6 +145,50 @@ func WriteResponse(w http.ResponseWriter, code int, value interface{}) { } } +func init() { + jsoniter.RegisterTypeEncoderFunc("error", MarshalErrorJSON, MarshalErrorJSONIsEmpty) + jsoniter.RegisterTypeEncoderFunc("[]error", MarshalErrorSliceJSON, MarshalErrorSliceJSONIsEmpty) +} + +var json = jsoniter.ConfigCompatibleWithStandardLibrary + +// MarshalErrorJSON writes error to stream as string +func MarshalErrorJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { + p := *((*error)(ptr)) + if p == nil { + stream.WriteNil() + } else { + stream.WriteString(p.Error()) + } +} + +// MarshalErrorSliceJSON writes []error to stream as []string JSON blob +func MarshalErrorSliceJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) { + a := *((*[]error)(ptr)) + switch { + case len(a) == 0: + stream.WriteNil() + default: + stream.WriteArrayStart() + for i, e := range a { + if i > 0 { + stream.WriteMore() + } + stream.WriteString(e.Error()) + } + stream.WriteArrayEnd() + } +} + +func MarshalErrorJSONIsEmpty(_ unsafe.Pointer) bool { + return false +} + +func MarshalErrorSliceJSONIsEmpty(_ unsafe.Pointer) bool { + return false +} + +// WriteJSON writes an interface value encoded as JSON to w func WriteJSON(w http.ResponseWriter, code int, value interface{}) { // FIXME: we don't need to write the header in all/some circumstances. w.Header().Set("Content-Type", "application/json") |