|
| 1 | +// Copyright 2024, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package wshserver |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "regexp" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/wavetermdev/waveterm/pkg/waveobj" |
| 15 | + "github.com/wavetermdev/waveterm/pkg/wshrpc" |
| 16 | + "github.com/wavetermdev/waveterm/pkg/wstore" |
| 17 | +) |
| 18 | + |
| 19 | +const SimpleId_This = "this" |
| 20 | +const SimpleId_Tab = "tab" |
| 21 | + |
| 22 | +var ( |
| 23 | + simpleTabNumRe = regexp.MustCompile(`^tab:(\d{1,3})$`) |
| 24 | + shortUUIDRe = regexp.MustCompile(`^[0-9a-f]{8}$`) |
| 25 | + SimpleId_BlockNum_Regex = regexp.MustCompile(`^\d+$`) |
| 26 | +) |
| 27 | + |
| 28 | +// Helper function to validate UUIDs or 8-char UUIDs format |
| 29 | +func isValidSimpleUUID(s string) bool { |
| 30 | + // Try parsing as full UUID |
| 31 | + _, err := uuid.Parse(s) |
| 32 | + if err == nil { |
| 33 | + return true |
| 34 | + } |
| 35 | + |
| 36 | + // Check if it's an 8-char hex prefix |
| 37 | + shortUUIDPattern := regexp.MustCompile(`^[0-9a-f]{8}$`) |
| 38 | + return shortUUIDPattern.MatchString(strings.ToLower(s)) |
| 39 | +} |
| 40 | + |
| 41 | +// First function: detect/choose discriminator |
| 42 | +func parseSimpleId(simpleId string) (discriminator string, value string, err error) { |
| 43 | + // Check for explicit discriminator with @ |
| 44 | + if parts := strings.SplitN(simpleId, "@", 2); len(parts) == 2 { |
| 45 | + return parts[0], parts[1], nil |
| 46 | + } |
| 47 | + |
| 48 | + // Handle special keywords |
| 49 | + if simpleId == SimpleId_This || simpleId == SimpleId_Tab { |
| 50 | + return "this", simpleId, nil |
| 51 | + } |
| 52 | + |
| 53 | + // Check if it's a simple ORef (type:uuid) |
| 54 | + if _, err := waveobj.ParseORef(simpleId); err == nil { |
| 55 | + return "oref", simpleId, nil |
| 56 | + } |
| 57 | + |
| 58 | + // Check for tab:N format |
| 59 | + if simpleTabNumRe.MatchString(simpleId) { |
| 60 | + return "tabnum", simpleId, nil |
| 61 | + } |
| 62 | + |
| 63 | + // Check for plain number (block reference) |
| 64 | + if _, err := strconv.Atoi(simpleId); err == nil { |
| 65 | + return "blocknum", simpleId, nil |
| 66 | + } |
| 67 | + |
| 68 | + // Check for UUIDs |
| 69 | + if _, err := uuid.Parse(simpleId); err == nil { |
| 70 | + return "uuid", simpleId, nil |
| 71 | + } |
| 72 | + if shortUUIDRe.MatchString(strings.ToLower(simpleId)) { |
| 73 | + return "uuid8", simpleId, nil |
| 74 | + } |
| 75 | + |
| 76 | + return "", "", fmt.Errorf("invalid simple id format: %s", simpleId) |
| 77 | +} |
| 78 | + |
| 79 | +// Individual resolvers |
| 80 | +func resolveThis(ctx context.Context, data wshrpc.CommandResolveIdsData, value string) (*waveobj.ORef, error) { |
| 81 | + if data.BlockId == "" { |
| 82 | + return nil, fmt.Errorf("no blockid in request") |
| 83 | + } |
| 84 | + |
| 85 | + if value == SimpleId_This { |
| 86 | + return &waveobj.ORef{OType: waveobj.OType_Block, OID: data.BlockId}, nil |
| 87 | + } |
| 88 | + if value == SimpleId_Tab { |
| 89 | + tabId, err := wstore.DBFindTabForBlockId(ctx, data.BlockId) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("error finding tab: %v", err) |
| 92 | + } |
| 93 | + return &waveobj.ORef{OType: waveobj.OType_Tab, OID: tabId}, nil |
| 94 | + } |
| 95 | + return nil, fmt.Errorf("invalid value for 'this' resolver: %s", value) |
| 96 | +} |
| 97 | + |
| 98 | +func resolveORef(ctx context.Context, value string) (*waveobj.ORef, error) { |
| 99 | + parsedORef, err := waveobj.ParseORef(value) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("error parsing oref: %v", err) |
| 102 | + } |
| 103 | + return &parsedORef, nil |
| 104 | +} |
| 105 | + |
| 106 | +func resolveTabNum(ctx context.Context, data wshrpc.CommandResolveIdsData, value string) (*waveobj.ORef, error) { |
| 107 | + m := simpleTabNumRe.FindStringSubmatch(value) |
| 108 | + if m == nil { |
| 109 | + return nil, fmt.Errorf("error parsing simple tab id: %s", value) |
| 110 | + } |
| 111 | + |
| 112 | + tabNum, err := strconv.Atoi(m[1]) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("error parsing simple tab num: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + curTabId, err := wstore.DBFindTabForBlockId(ctx, data.BlockId) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("error finding tab for block: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + wsId, err := wstore.DBFindWorkspaceForTabId(ctx, curTabId) |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("error finding current workspace: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + ws, err := wstore.DBMustGet[*waveobj.Workspace](ctx, wsId) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("error getting workspace: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + if tabNum < 1 || tabNum > len(ws.TabIds) { |
| 133 | + return nil, fmt.Errorf("tab num out of range, workspace has %d tabs", len(ws.TabIds)) |
| 134 | + } |
| 135 | + |
| 136 | + resolvedTabId := ws.TabIds[tabNum-1] |
| 137 | + return &waveobj.ORef{OType: waveobj.OType_Tab, OID: resolvedTabId}, nil |
| 138 | +} |
| 139 | + |
| 140 | +func resolveBlock(ctx context.Context, data wshrpc.CommandResolveIdsData, value string) (*waveobj.ORef, error) { |
| 141 | + blockNum, err := strconv.Atoi(value) |
| 142 | + if err != nil { |
| 143 | + return nil, fmt.Errorf("error parsing block number: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + tabId, err := wstore.DBFindTabForBlockId(ctx, data.BlockId) |
| 147 | + if err != nil { |
| 148 | + return nil, fmt.Errorf("error finding tab for blockid %s: %w", data.BlockId, err) |
| 149 | + } |
| 150 | + |
| 151 | + tab, err := wstore.DBGet[*waveobj.Tab](ctx, tabId) |
| 152 | + if err != nil { |
| 153 | + return nil, fmt.Errorf("error retrieving tab %s: %w", tabId, err) |
| 154 | + } |
| 155 | + |
| 156 | + layout, err := wstore.DBGet[*waveobj.LayoutState](ctx, tab.LayoutState) |
| 157 | + if err != nil { |
| 158 | + return nil, fmt.Errorf("error retrieving layout state %s: %w", tab.LayoutState, err) |
| 159 | + } |
| 160 | + |
| 161 | + if layout.LeafOrder == nil { |
| 162 | + return nil, fmt.Errorf("could not resolve block num %v, leaf order is empty", blockNum) |
| 163 | + } |
| 164 | + |
| 165 | + leafIndex := blockNum - 1 // block nums are 1-indexed |
| 166 | + if len(*layout.LeafOrder) <= leafIndex { |
| 167 | + return nil, fmt.Errorf("could not find a node in the layout matching blockNum %v", blockNum) |
| 168 | + } |
| 169 | + |
| 170 | + leafEntry := (*layout.LeafOrder)[leafIndex] |
| 171 | + return &waveobj.ORef{OType: waveobj.OType_Block, OID: leafEntry.BlockId}, nil |
| 172 | +} |
| 173 | + |
| 174 | +func resolveUUID(ctx context.Context, value string) (*waveobj.ORef, error) { |
| 175 | + return wstore.DBResolveEasyOID(ctx, value) |
| 176 | +} |
| 177 | + |
| 178 | +// Main resolver function |
| 179 | +func resolveSimpleId(ctx context.Context, data wshrpc.CommandResolveIdsData, simpleId string) (*waveobj.ORef, error) { |
| 180 | + discriminator, value, err := parseSimpleId(simpleId) |
| 181 | + if err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + switch discriminator { |
| 185 | + case "this": |
| 186 | + return resolveThis(ctx, data, value) |
| 187 | + case "oref": |
| 188 | + return resolveORef(ctx, value) |
| 189 | + case "tabnum": |
| 190 | + return resolveTabNum(ctx, data, value) |
| 191 | + case "blocknum": |
| 192 | + return resolveBlock(ctx, data, value) |
| 193 | + case "uuid", "uuid8": |
| 194 | + return resolveUUID(ctx, value) |
| 195 | + default: |
| 196 | + return nil, fmt.Errorf("unknown discriminator: %s", discriminator) |
| 197 | + } |
| 198 | +} |
0 commit comments