Skip to content

Commit

Permalink
fix: allow for pointer based types
Browse files Browse the repository at this point in the history
  • Loading branch information
frankyn committed Jun 22, 2022
1 parent 001d08b commit 5df45dc
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion storage/bucket.go
Expand Up @@ -1503,6 +1503,17 @@ func toCORSFromProto(rc []*storagepb.Bucket_Cors) []CORS {
return out
}

// Handle ptr or int64
func setAgeCondition(age int64, cond *raw.BucketLifecycleRuleCondition) {
c := reflect.ValueOf(cond).Elem().FieldByName("Age")
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 @@ -1525,6 +1536,8 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
},
}

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

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

// Handle ptr or int64
func getAgeCondition(age interface{}) int64 {
v := reflect.ValueOf(age)
var out int64 = 0
switch v.Kind() {
case reflect.Int64:
out = v.Interface().(int64)
case reflect.Ptr:
if v.Interface() == nil {
out = 0
} else {
out = *(v.Interface().(*int64))
}
}
return out
}

func toLifecycle(rl *raw.BucketLifecycle) Lifecycle {
var l Lifecycle
if rl == nil {
Expand All @@ -1607,7 +1637,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 +1645,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

0 comments on commit 5df45dc

Please sign in to comment.