Skip to content

feat(common): [120044446] support storing temp data in ctx #2884

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
merged 1 commit into from
Oct 12, 2024
Merged
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
77 changes: 77 additions & 0 deletions tencentcloud/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package common

import (
"context"
"sync"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type ctxResourceDataKey struct{}
type ctxProviderMetaKey struct{}
type ctxDataKey struct{}

// NewResourceLifeCycleHandleFuncContext 创建一个资源生命周期处理方法上下文
func NewResourceLifeCycleHandleFuncContext(
Expand All @@ -19,6 +21,7 @@ func NewResourceLifeCycleHandleFuncContext(
ctx := context.WithValue(parent, LogIdKey, logID)
ctx = context.WithValue(ctx, ctxResourceDataKey{}, d)
ctx = context.WithValue(ctx, ctxProviderMetaKey{}, meta)
ctx = context.WithValue(ctx, ctxDataKey{}, &ContextData{})
return ctx
}

Expand All @@ -37,3 +40,77 @@ func ProviderMetaFromContext(ctx context.Context) interface{} {
}
return nil
}

// DataFromContext 从上下文获取 data
func DataFromContext(ctx context.Context) *ContextData {
if data, ok := ctx.Value(ctxDataKey{}).(*ContextData); ok {
return data
}
return nil
}

// ContextData 上下文临时数据
type ContextData struct {
lock sync.RWMutex
data map[string]interface{}
}

// Set 设置值
func (d *ContextData) Set(key string, v interface{}) {
d.lock.Lock()
defer d.lock.Unlock()
if d.data == nil {
d.data = make(map[string]interface{})
}
d.data[key] = v
}

// Delete 删除值
func (d *ContextData) Delete(key string) {
d.lock.Lock()
defer d.lock.Unlock()
delete(d.data, key)
}

// Get 获取键
func (d *ContextData) Get(key string) interface{} {
d.lock.RLock()
defer d.lock.RUnlock()
return d.data[key]
}

// GetInt 获取 int 数据键
func (d *ContextData) GetInt(key string) (ret int, ok bool) {
ret, ok = d.Get(key).(int)
return
}

// GetUInt 获取 uint 数据键
func (d *ContextData) GetUInt(key string) (ret uint, ok bool) {
ret, ok = d.Get(key).(uint)
return
}

// GetInt64 获取 int64 数据键
func (d *ContextData) GetInt64(key string) (ret int64, ok bool) {
ret, ok = d.Get(key).(int64)
return
}

// GetUInt64 获取 uint64 数据键
func (d *ContextData) GetUInt64(key string) (ret uint64, ok bool) {
ret, ok = d.Get(key).(uint64)
return
}

// GetString 获取 string 数据键
func (d *ContextData) GetString(key string) (ret string, ok bool) {
ret, ok = d.Get(key).(string)
return
}

// GetBool 获取 bool 数据键
func (d *ContextData) GetBool(key string) (ret bool, ok bool) {
ret, ok = d.Get(key).(bool)
return
}
Loading