Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ go 1.25.9
// We fork this bc the stock version of this library is over 20mb.
replace github.com/hashicorp/go-getter => github.com/viam-labs/go-getter v0.0.0-20251022162721-98d73b852c8a

replace go.viam.com/api => ../api

require (
github.com/AlekSi/gocov-xml v1.0.0
github.com/Masterminds/semver/v3 v3.3.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1158,8 +1158,6 @@ go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
go.viam.com/api v0.1.555 h1:ncBlAwDpEk658aouQLoeq1RLDqdi3spWVFN0y6nsvXM=
go.viam.com/api v0.1.555/go.mod h1:nVe4WXrtc8aupJ8OWXSYx6KhCiOkr3VCbkwxD4D41xQ=
go.viam.com/test v1.2.4 h1:JYgZhsuGAQ8sL9jWkziAXN9VJJiKbjoi9BsO33TW3ug=
go.viam.com/test v1.2.4/go.mod h1:zI2xzosHdqXAJ/kFqcN+OIF78kQuTV2nIhGZ8EzvaJI=
go.viam.com/utils v0.6.1 h1:xJhq+S2ADMTDj9538blmhI0otZCRAZUonRBkb+zHIHo=
Expand Down
12 changes: 12 additions & 0 deletions resource/response_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,36 @@ import (
"time"

commonpb "go.viam.com/api/common/v1"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

// ResponseMetadata contains extra info associated with a Resource's standard response.
type ResponseMetadata struct {
CapturedAt time.Time
Attributes map[string]interface{}
}

// AsProto turns the ResponseMetadata struct into a protobuf message.
func (rm ResponseMetadata) AsProto() *commonpb.ResponseMetadata {
metadata := &commonpb.ResponseMetadata{}
metadata.CapturedAt = timestamppb.New(rm.CapturedAt)
attributes := make(map[string]*structpb.Value)
for k, v := range rm.Attributes {
value, err := structpb.NewValue(v)
if err != nil {
continue
}
attributes[k] = value
}
metadata.Attributes = &structpb.Struct{Fields: attributes}
return metadata
}

// ResponseMetadataFromProto turns the protobuf message into a ResponseMetadata struct.
func ResponseMetadataFromProto(proto *commonpb.ResponseMetadata) ResponseMetadata {
metadata := ResponseMetadata{}
metadata.CapturedAt = proto.CapturedAt.AsTime()
metadata.Attributes = proto.Attributes.AsMap()
return metadata
}
7 changes: 5 additions & 2 deletions resource/response_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import (

commonpb "go.viam.com/api/common/v1"
"go.viam.com/test"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

func TestResponseToProto(t *testing.T) {
ts := time.UnixMilli(12345)
metadata := ResponseMetadata{CapturedAt: ts}
metadata := ResponseMetadata{CapturedAt: ts, Attributes: map[string]interface{}{"key": "value"}}
proto := metadata.AsProto()
test.That(t, proto.CapturedAt.AsTime(), test.ShouldEqual, ts)
test.That(t, proto.Attributes.Fields["key"].GetStringValue(), test.ShouldEqual, "value")
}

func TestResponseFromProto(t *testing.T) {
ts := &timestamppb.Timestamp{Seconds: 12, Nanos: 345000000}
proto := &commonpb.ResponseMetadata{CapturedAt: ts}
proto := &commonpb.ResponseMetadata{CapturedAt: ts, Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{"key": {Kind: &structpb.Value_StringValue{StringValue: "value"}}}}}
metadata := ResponseMetadataFromProto(proto)
test.That(t, metadata.CapturedAt, test.ShouldEqual, time.UnixMilli(12345))
test.That(t, metadata.Attributes["key"], test.ShouldEqual, "value")
}
Loading