Skip to content

Commit

Permalink
When outputting capabilities, make order of all fields deterministic,…
Browse files Browse the repository at this point in the history
… not just resourceTypeCapabilities.
  • Loading branch information
ggreer committed Jan 31, 2024
1 parent b839493 commit 5f50d9f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 14 additions & 2 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/spf13/cobra"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"

"github.com/conductorone/baton-sdk/internal/connector"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
Expand Down Expand Up @@ -288,7 +289,18 @@ func NewCmd[T any, PtrT *T](
return fmt.Errorf("connector does not support capabilities")
}

outBytes, err := json.MarshalIndent(md.Metadata.Capabilities, "", " ")
protoMarshaller := protojson.MarshalOptions{
Multiline: true,
Indent: " ",
}

a := &anypb.Any{}
err = anypb.MarshalFrom(a, md.Metadata.Capabilities, proto.MarshalOptions{Deterministic: true})
if err != nil {
return err
}

outBytes, err := protoMarshaller.Marshal(a)
if err != nil {
return err
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/connectorbuilder/connectorbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package connectorbuilder
import (
"context"
"fmt"
"sort"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
Expand Down Expand Up @@ -266,9 +265,7 @@ func getCapabilities(ctx context.Context, b *builderImpl) *v2.ConnectorCapabilit
}
resourceTypeCapabilities = append(resourceTypeCapabilities, resourceTypeCapability)
}
sort.Slice(resourceTypeCapabilities, func(i, j int) bool {
return resourceTypeCapabilities[i].ResourceType.GetId() < resourceTypeCapabilities[j].ResourceType.GetId()
})

return &v2.ConnectorCapabilities{ResourceTypeCapabilities: resourceTypeCapabilities}
}

Expand Down

0 comments on commit 5f50d9f

Please sign in to comment.