|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package wstore |
| 5 | + |
| 6 | +import ( |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "github.com/wavetermdev/waveterm/pkg/waveobj" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + blockRTInfoStore = make(map[waveobj.ORef]*waveobj.ObjRTInfo) |
| 16 | + blockRTInfoMutex sync.RWMutex |
| 17 | +) |
| 18 | + |
| 19 | +// SetRTInfo merges the provided info map into the BlockRTInfo for the given ORef. |
| 20 | +// Only updates fields that exist in the BlockRTInfo struct. |
| 21 | +// Removes fields that have nil values. |
| 22 | +func SetRTInfo(oref waveobj.ORef, info map[string]any) { |
| 23 | + blockRTInfoMutex.Lock() |
| 24 | + defer blockRTInfoMutex.Unlock() |
| 25 | + |
| 26 | + rtInfo, exists := blockRTInfoStore[oref] |
| 27 | + if !exists { |
| 28 | + rtInfo = &waveobj.ObjRTInfo{} |
| 29 | + blockRTInfoStore[oref] = rtInfo |
| 30 | + } |
| 31 | + |
| 32 | + rtInfoValue := reflect.ValueOf(rtInfo).Elem() |
| 33 | + rtInfoType := rtInfoValue.Type() |
| 34 | + |
| 35 | + // Build a map of json tags to field indices for quick lookup |
| 36 | + jsonTagToField := make(map[string]int) |
| 37 | + for i := 0; i < rtInfoType.NumField(); i++ { |
| 38 | + field := rtInfoType.Field(i) |
| 39 | + jsonTag := field.Tag.Get("json") |
| 40 | + if jsonTag != "" { |
| 41 | + // Remove omitempty and other options |
| 42 | + tagParts := strings.Split(jsonTag, ",") |
| 43 | + if len(tagParts) > 0 && tagParts[0] != "" { |
| 44 | + jsonTagToField[tagParts[0]] = i |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Merge the info map into the struct |
| 50 | + for key, value := range info { |
| 51 | + fieldIndex, exists := jsonTagToField[key] |
| 52 | + if !exists { |
| 53 | + continue // Skip keys that don't exist in the struct |
| 54 | + } |
| 55 | + |
| 56 | + fieldValue := rtInfoValue.Field(fieldIndex) |
| 57 | + if !fieldValue.CanSet() { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + if value == nil { |
| 62 | + // Set to zero value (empty string for string fields) |
| 63 | + fieldValue.Set(reflect.Zero(fieldValue.Type())) |
| 64 | + } else { |
| 65 | + // Convert and set the value |
| 66 | + if valueStr, ok := value.(string); ok && fieldValue.Kind() == reflect.String { |
| 67 | + fieldValue.SetString(valueStr) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// GetRTInfo returns the BlockRTInfo for the given ORef, or nil if not found |
| 74 | +func GetRTInfo(oref waveobj.ORef) *waveobj.ObjRTInfo { |
| 75 | + blockRTInfoMutex.RLock() |
| 76 | + defer blockRTInfoMutex.RUnlock() |
| 77 | + |
| 78 | + if rtInfo, exists := blockRTInfoStore[oref]; exists { |
| 79 | + // Return a copy to avoid external modification |
| 80 | + copy := *rtInfo |
| 81 | + return © |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// DeleteRTInfo removes the BlockRTInfo for the given ORef |
| 87 | +func DeleteRTInfo(oref waveobj.ORef) { |
| 88 | + blockRTInfoMutex.Lock() |
| 89 | + defer blockRTInfoMutex.Unlock() |
| 90 | + |
| 91 | + delete(blockRTInfoStore, oref) |
| 92 | +} |
0 commit comments