-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(storage): add object contexts in Go GCS SDK #13390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+713
−5
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e951fd
feat: add object contexts in Go GCS SDK
krishnamd-jkp a996350
resolve comments and fix tests
krishnamd-jkp 61773b5
resolve comments
krishnamd-jkp 17d736a
resolve comments
krishnamd-jkp 1425a0b
remove custom context in list query
krishnamd-jkp 03ae72f
resolve comments
krishnamd-jkp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
krishnamd-jkp marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package storage | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "cloud.google.com/go/storage/internal/apiv2/storagepb" | ||
| raw "google.golang.org/api/storage/v1" | ||
| ) | ||
|
|
||
| // ObjectContexts is a container for object contexts. | ||
| type ObjectContexts struct { | ||
| Custom map[string]ObjectContextValue | ||
| } | ||
|
|
||
| // ObjectContextValue holds the value of a user-defined object context. | ||
| type ObjectContextValue struct { | ||
krishnamd-jkp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Value string | ||
| Delete bool | ||
| // Read-only fields. Any updates to CreateTime and UpdateTime will be ignored. | ||
| // These fields are handled by the server. | ||
| CreateTime time.Time | ||
| UpdateTime time.Time | ||
| } | ||
|
|
||
| // toContexts converts the raw library's ObjectContexts type to the object contexts. | ||
| func toObjectContexts(c *raw.ObjectContexts) *ObjectContexts { | ||
| if c == nil { | ||
| return nil | ||
| } | ||
| customContexts := make(map[string]ObjectContextValue) | ||
| for k, v := range c.Custom { | ||
| customContexts[k] = ObjectContextValue{ | ||
| Value: v.Value, | ||
| CreateTime: convertTime(v.CreateTime), | ||
| UpdateTime: convertTime(v.UpdateTime), | ||
| } | ||
| } | ||
| return &ObjectContexts{ | ||
| Custom: customContexts, | ||
| } | ||
| } | ||
|
|
||
| // toRawObjectContexts converts the object contexts to the raw library's ObjectContexts type. | ||
| func toRawObjectContexts(c *ObjectContexts) *raw.ObjectContexts { | ||
| if c == nil { | ||
| return nil | ||
| } | ||
krishnamd-jkp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| customContexts := make(map[string]raw.ObjectCustomContextPayload) | ||
| for k, v := range c.Custom { | ||
| if v.Delete { | ||
| // If Delete is true, populate null fields to signify deletion. | ||
| customContexts[k] = raw.ObjectCustomContextPayload{NullFields: []string{k}} | ||
| } else { | ||
| customContexts[k] = raw.ObjectCustomContextPayload{ | ||
| Value: v.Value, | ||
| ForceSendFields: []string{k}, | ||
| } | ||
| } | ||
| } | ||
| return &raw.ObjectContexts{ | ||
| Custom: customContexts, | ||
| } | ||
| } | ||
|
|
||
| func toObjectContextsFromProto(c *storagepb.ObjectContexts) *ObjectContexts { | ||
| if c == nil { | ||
| return nil | ||
| } | ||
| customContexts := make(map[string]ObjectContextValue) | ||
| for k, v := range c.GetCustom() { | ||
| customContexts[k] = ObjectContextValue{ | ||
| Value: v.GetValue(), | ||
| CreateTime: v.GetCreateTime().AsTime(), | ||
| UpdateTime: v.GetUpdateTime().AsTime(), | ||
| } | ||
| } | ||
| return &ObjectContexts{ | ||
| Custom: customContexts, | ||
| } | ||
| } | ||
|
|
||
| func toProtoObjectContexts(c *ObjectContexts) *storagepb.ObjectContexts { | ||
| if c == nil { | ||
krishnamd-jkp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
| customContexts := make(map[string]*storagepb.ObjectCustomContextPayload) | ||
| for k, v := range c.Custom { | ||
| if !v.Delete { | ||
cpriti-os marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| customContexts[k] = &storagepb.ObjectCustomContextPayload{ | ||
| Value: v.Value, | ||
| } | ||
| } | ||
| } | ||
| return &storagepb.ObjectContexts{ | ||
| Custom: customContexts, | ||
| } | ||
| } | ||
|
|
||
| func toStringCustomContext(cc *CustomContext) string { | ||
krishnamd-jkp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if cc == nil || cc.Key == "" { | ||
| return "" | ||
| } | ||
| var filter string | ||
| if cc.Value != "" { | ||
| filter = fmt.Sprintf(`contexts."%s"="%s"`, cc.Key, cc.Value) | ||
| } else { | ||
| filter = fmt.Sprintf(`contexts."%s":*`, cc.Key) | ||
| } | ||
| if cc.Absence { | ||
| filter = fmt.Sprintf(`-%s`, filter) | ||
| } | ||
| return filter | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.