client: change Raw fields to be json.RawMessage

These fields store the raw JSON data that we received, and should
never container bytes that are non-JSON (as we'd error out when
failing to unmarshal).

Change the type to a json.RawMessage, which:

- Is more explicit on intent
- Can still be used as a regular []byte in all cases

And, while it's not expected to be marshaled to JSON, doing so will also
print the output in a readable format instead of base64 encoding;

    package main

    import (
        "encoding/json"
        "fmt"
    )

    func main() {
        foo := struct {
            Bytes []byte
            Raw   json.RawMessage
        }{
            Bytes: []byte(`{"hello": "world"}`),
            Raw:   json.RawMessage(`{"hello": "world"}`),
        }

        out, _ := json.MarshalIndent(foo, "", "  ")
        fmt.Println(string(out))
    }

Will print:

    {
      "Bytes": "eyJoZWxsbyI6ICJ3b3JsZCJ9",
      "Raw": {
        "hello": "world"
      }
    }

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-24 19:07:26 +02:00
parent 2bda4a2001
commit ddbb503dc7
18 changed files with 32 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/swarm"
)
@@ -14,7 +15,7 @@ type ConfigInspectOptions struct {
// ConfigInspectResult holds the result from the ConfigInspect method.
type ConfigInspectResult struct {
Config swarm.Config
Raw []byte
Raw json.RawMessage
}
// ConfigInspect returns the config information with raw data

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"net/url"
"github.com/moby/moby/api/types/network"
@@ -10,7 +11,7 @@ import (
// NetworkInspectResult contains the result of a network inspection.
type NetworkInspectResult struct {
Network network.Inspect
Raw []byte
Raw json.RawMessage
}
// NetworkInspect returns the information for a specific network configured in the docker host.

View File

@@ -14,7 +14,7 @@ type NodeInspectOptions struct{}
type NodeInspectResult struct {
Node swarm.Node
Raw []byte
Raw json.RawMessage
}
// NodeInspect returns the node information.

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/plugin"
)
@@ -13,8 +14,8 @@ type PluginInspectOptions struct {
// PluginInspectResult holds the result from the [Client.PluginInspect] method.
type PluginInspectResult struct {
Raw []byte
Plugin plugin.Plugin
Raw json.RawMessage
}
// PluginInspect inspects an existing plugin

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/swarm"
)
@@ -14,7 +15,7 @@ type SecretInspectOptions struct {
// SecretInspectResult holds the result from the [Client.SecretInspect]. method.
type SecretInspectResult struct {
Secret swarm.Secret
Raw []byte
Raw json.RawMessage
}
// SecretInspect returns the secret information with raw data.

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"fmt"
"net/url"
@@ -16,7 +17,7 @@ type ServiceInspectOptions struct {
// ServiceInspectResult represents the result of a service inspect operation.
type ServiceInspectResult struct {
Service swarm.Service
Raw []byte
Raw json.RawMessage
}
// ServiceInspect retrieves detailed information about a specific service by its ID.

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/swarm"
)
@@ -14,7 +15,7 @@ type TaskInspectOptions struct {
// TaskInspectResult contains the result of a task inspection.
type TaskInspectResult struct {
Task swarm.Task
Raw []byte
Raw json.RawMessage
}
// TaskInspect returns the task information and its raw representation.

View File

@@ -70,7 +70,7 @@ func encodePlatform(platform *ocispec.Platform) (string, error) {
return string(p), nil
}
func decodeWithRaw[T any](resp *http.Response, out *T) (raw []byte, _ error) {
func decodeWithRaw[T any](resp *http.Response, out *T) (raw json.RawMessage, _ error) {
if resp == nil || resp.Body == nil {
return nil, errors.New("empty response")
}

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/volume"
)
@@ -13,8 +14,8 @@ type VolumeInspectOptions struct {
// VolumeInspectResult holds the result from the [Client.VolumeInspect] method.
type VolumeInspectResult struct {
Raw []byte
Volume volume.Volume
Raw json.RawMessage
}
// VolumeInspect returns the information about a specific volume in the docker host.

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/swarm"
)
@@ -14,7 +15,7 @@ type ConfigInspectOptions struct {
// ConfigInspectResult holds the result from the ConfigInspect method.
type ConfigInspectResult struct {
Config swarm.Config
Raw []byte
Raw json.RawMessage
}
// ConfigInspect returns the config information with raw data

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"net/url"
"github.com/moby/moby/api/types/network"
@@ -10,7 +11,7 @@ import (
// NetworkInspectResult contains the result of a network inspection.
type NetworkInspectResult struct {
Network network.Inspect
Raw []byte
Raw json.RawMessage
}
// NetworkInspect returns the information for a specific network configured in the docker host.

View File

@@ -14,7 +14,7 @@ type NodeInspectOptions struct{}
type NodeInspectResult struct {
Node swarm.Node
Raw []byte
Raw json.RawMessage
}
// NodeInspect returns the node information.

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/plugin"
)
@@ -13,8 +14,8 @@ type PluginInspectOptions struct {
// PluginInspectResult holds the result from the [Client.PluginInspect] method.
type PluginInspectResult struct {
Raw []byte
Plugin plugin.Plugin
Raw json.RawMessage
}
// PluginInspect inspects an existing plugin

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/swarm"
)
@@ -14,7 +15,7 @@ type SecretInspectOptions struct {
// SecretInspectResult holds the result from the [Client.SecretInspect]. method.
type SecretInspectResult struct {
Secret swarm.Secret
Raw []byte
Raw json.RawMessage
}
// SecretInspect returns the secret information with raw data.

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"fmt"
"net/url"
@@ -16,7 +17,7 @@ type ServiceInspectOptions struct {
// ServiceInspectResult represents the result of a service inspect operation.
type ServiceInspectResult struct {
Service swarm.Service
Raw []byte
Raw json.RawMessage
}
// ServiceInspect retrieves detailed information about a specific service by its ID.

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/swarm"
)
@@ -14,7 +15,7 @@ type TaskInspectOptions struct {
// TaskInspectResult contains the result of a task inspection.
type TaskInspectResult struct {
Task swarm.Task
Raw []byte
Raw json.RawMessage
}
// TaskInspect returns the task information and its raw representation.

View File

@@ -70,7 +70,7 @@ func encodePlatform(platform *ocispec.Platform) (string, error) {
return string(p), nil
}
func decodeWithRaw[T any](resp *http.Response, out *T) (raw []byte, _ error) {
func decodeWithRaw[T any](resp *http.Response, out *T) (raw json.RawMessage, _ error) {
if resp == nil || resp.Body == nil {
return nil, errors.New("empty response")
}

View File

@@ -2,6 +2,7 @@ package client
import (
"context"
"encoding/json"
"github.com/moby/moby/api/types/volume"
)
@@ -13,8 +14,8 @@ type VolumeInspectOptions struct {
// VolumeInspectResult holds the result from the [Client.VolumeInspect] method.
type VolumeInspectResult struct {
Raw []byte
Volume volume.Volume
Raw json.RawMessage
}
// VolumeInspect returns the information about a specific volume in the docker host.