Skip to content
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

fix(storage): allow for Age int64* type and int64 type #6230

Merged
merged 10 commits into from
Jun 22, 2022
33 changes: 31 additions & 2 deletions storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,19 @@ func toCORSFromProto(rc []*storagepb.Bucket_Cors) []CORS {
return out
}

// Used to handle breaking change in Autogen Storage client OLM Age field
// from int64 to *int64 gracefully in the manual client
// Method should be removed once breaking change is made and introduced to this client
func setAgeCondition(age *int64, ageField interface{}) {
codyoss marked this conversation as resolved.
Show resolved Hide resolved
c := reflect.Indirect(reflect.ValueOf(ageField))
codyoss marked this conversation as resolved.
Show resolved Hide resolved
switch c.Kind() {
case reflect.Int64:
c.SetInt(*age)
case reflect.Ptr:
c.Set(reflect.ValueOf(age))
}
}

func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
var rl raw.BucketLifecycle
if len(l.Rules) == 0 {
Expand All @@ -1515,7 +1528,6 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
StorageClass: r.Action.StorageClass,
},
Condition: &raw.BucketLifecycleRuleCondition{
Age: r.Condition.AgeInDays,
DaysSinceCustomTime: r.Condition.DaysSinceCustomTime,
DaysSinceNoncurrentTime: r.Condition.DaysSinceNoncurrentTime,
MatchesPrefix: r.Condition.MatchesPrefix,
Expand All @@ -1525,6 +1537,8 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
},
}

setAgeCondition(&r.Condition.AgeInDays, &rr.Condition.Age)

switch r.Condition.Liveness {
case LiveAndArchived:
rr.Condition.IsLive = nil
Expand Down Expand Up @@ -1595,6 +1609,21 @@ func toProtoLifecycle(l Lifecycle) *storagepb.Bucket_Lifecycle {
return &rl
}

// Used to handle breaking change in Autogen Storage client OLM Age field
// from int64 to *int64 gracefully in the manual client
// Method should be removed once breaking change is made and introduced to this client
func getAgeCondition(ageField interface{}) int64 {
v := reflect.ValueOf(ageField)
if v.Kind() == reflect.Int64 {
return v.Interface().(int64)
} else if v.Kind() == reflect.Ptr {
if v.Interface().(*int64) != nil {
codyoss marked this conversation as resolved.
Show resolved Hide resolved
return *(v.Interface().(*int64))
}
}
return 0
}

func toLifecycle(rl *raw.BucketLifecycle) Lifecycle {
var l Lifecycle
if rl == nil {
Expand All @@ -1607,7 +1636,6 @@ func toLifecycle(rl *raw.BucketLifecycle) Lifecycle {
StorageClass: rr.Action.StorageClass,
},
Condition: LifecycleCondition{
AgeInDays: rr.Condition.Age,
DaysSinceCustomTime: rr.Condition.DaysSinceCustomTime,
DaysSinceNoncurrentTime: rr.Condition.DaysSinceNoncurrentTime,
MatchesPrefix: rr.Condition.MatchesPrefix,
Expand All @@ -1616,6 +1644,7 @@ func toLifecycle(rl *raw.BucketLifecycle) Lifecycle {
NumNewerVersions: rr.Condition.NumNewerVersions,
},
}
r.Condition.AgeInDays = getAgeCondition(rr.Condition.Age)

if rr.Condition.IsLive == nil {
r.Condition.Liveness = LiveAndArchived
Expand Down
17 changes: 17 additions & 0 deletions storage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,23 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) {
}
}

func TestAgeConditionBackwardCompat(t *testing.T) {
var ti int64
var want int64 = 100
setAgeCondition(&want, &ti)
if getAgeCondition(ti) != want {
t.Fatalf("got %v, want %v", getAgeCondition(ti), want)
}

var tp *int64
want = 10
setAgeCondition(&want, &tp)
if getAgeCondition(tp) != want {
t.Fatalf("got %v, want %v", getAgeCondition(tp), want)
}

}

func TestCallBuilders(t *testing.T) {
rc, err := raw.NewService(context.Background())
if err != nil {
Expand Down