Skip to content

Commit 4b5f781

Browse files
committed
refactor(materials): optimize UTF-8 check and add binary round-trip test
Check UTF-8 validity before allocating string to avoid wasted allocation for binary content. Add round-trip test verifying binary data survives the structpb.Struct serialization path. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent 51517b3 commit 4b5f781

2 files changed

Lines changed: 5 additions & 7 deletions

File tree

app/cli/pkg/action/workflow_run_describe.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,16 @@ func materialPBToAction(in *pb.AttestationItem_Material) *Material {
318318
// Fall back to deprecated string value field for compatibility with
319319
// older control plane versions that don't populate raw_value.
320320
var value string
321-
if len(in.GetRawValue()) > 0 {
322-
value = string(in.GetRawValue())
323-
if !utf8.Valid(in.GetRawValue()) {
324-
value = ""
321+
if raw := in.GetRawValue(); len(raw) > 0 {
322+
if utf8.Valid(raw) {
323+
value = string(raw)
325324
}
326325
} else {
327326
value = in.GetValue() //nolint:staticcheck // fallback for older servers
328327
}
329328

330329
m := &Material{
331-
Name: in.Name,
332-
// kept for compatibility in case we have users that are still using the string value field
330+
Name: in.Name,
333331
Value: value,
334332
RawValue: in.GetRawValue(),
335333
Type: in.Type,

pkg/attestation/renderer/chainloop/v02_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func TestBinaryContentStructRoundTrip(t *testing.T) {
351351
Materials: []*intoto.ResourceDescriptor{
352352
{
353353
Name: "binary.bin",
354-
Annotations: mapToStruct(t, map[string]interface{}{
354+
Annotations: mapToStruct(t, map[string]any{
355355
"chainloop.material.name": "test-binary",
356356
"chainloop.material.type": "ARTIFACT",
357357
"chainloop.material.cas.inline": true,

0 commit comments

Comments
 (0)