Files
moby/client/service_inspect.go
Sebastiaan van Stijn ddbb503dc7 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>
2025-10-24 19:07:26 +02:00

42 lines
1.1 KiB
Go

package client
import (
"context"
"encoding/json"
"fmt"
"net/url"
"github.com/moby/moby/api/types/swarm"
)
// ServiceInspectOptions holds parameters related to the service inspect operation.
type ServiceInspectOptions struct {
InsertDefaults bool
}
// ServiceInspectResult represents the result of a service inspect operation.
type ServiceInspectResult struct {
Service swarm.Service
Raw json.RawMessage
}
// ServiceInspect retrieves detailed information about a specific service by its ID.
func (cli *Client) ServiceInspect(ctx context.Context, serviceID string, options ServiceInspectOptions) (ServiceInspectResult, error) {
serviceID, err := trimID("service", serviceID)
if err != nil {
return ServiceInspectResult{}, err
}
query := url.Values{}
query.Set("insertDefaults", fmt.Sprintf("%v", options.InsertDefaults))
resp, err := cli.get(ctx, "/services/"+serviceID, query, nil)
defer ensureReaderClosed(resp)
if err != nil {
return ServiceInspectResult{}, err
}
var out ServiceInspectResult
out.Raw, err = decodeWithRaw(resp, &out.Service)
return out, err
}