-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathschema-context-utils.ts
More file actions
169 lines (149 loc) · 5.6 KB
/
schema-context-utils.ts
File metadata and controls
169 lines (149 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Copyright 2026 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/
import type { SchemaRegistryContextResponse } from '../../../react-query/api/schema-registry';
import type { SchemaRegistrySubject } from '../../../state/rest-interfaces';
export const CONTEXT_PREFIX_RE = /^:\.([^:]+):(.+)$/;
export type ParsedSubject = {
context: string;
displayName: string;
qualifiedName: string;
};
// Extract context and display name from a subject.
// E.g. ":.staging:my-topic" → { context: "staging", … }
export function parseSubjectContext(name: string): ParsedSubject {
const match = CONTEXT_PREFIX_RE.exec(name);
if (match) {
return {
context: match[1],
displayName: match[2],
qualifiedName: name,
};
}
return {
context: 'default',
displayName: name,
qualifiedName: name,
};
}
export const ALL_CONTEXT_ID = '__all__';
export const DEFAULT_CONTEXT_ID = '__default__';
export const DEFAULT_CONTEXT_LABEL = 'Default';
export type DerivedContext = {
id: string;
label: string;
subjectCount: number;
mode: string;
compatibility: string;
};
// Merge backend context list with subject counts for
// the dropdown. Order: Default, named (sorted), All.
export function deriveContexts(
apiContexts: SchemaRegistryContextResponse[],
subjects: SchemaRegistrySubject[]
): DerivedContext[] {
const activeSubjects = subjects.filter((s) => !s.isSoftDeleted);
const countByContext = new Map<string, number>();
for (const subject of activeSubjects) {
const { context } = parseSubjectContext(subject.name);
const key = context === 'default' ? DEFAULT_CONTEXT_ID : context;
countByContext.set(key, (countByContext.get(key) ?? 0) + 1);
}
const contexts: DerivedContext[] = [];
// Default first, then named contexts alphabetically, then All at the bottom
const namedApiContexts: SchemaRegistryContextResponse[] = [];
let defaultContext: SchemaRegistryContextResponse | undefined;
for (const ctx of apiContexts) {
if (ctx.name === '.') {
defaultContext = ctx;
contexts.push({
id: DEFAULT_CONTEXT_ID,
label: DEFAULT_CONTEXT_LABEL,
subjectCount: countByContext.get(DEFAULT_CONTEXT_ID) ?? 0,
mode: ctx.mode,
compatibility: ctx.compatibility,
});
} else {
namedApiContexts.push(ctx);
}
}
namedApiContexts.sort((a, b) => a.name.localeCompare(b.name));
for (const ctx of namedApiContexts) {
const contextKey = ctx.name.startsWith('.') ? ctx.name.slice(1) : ctx.name;
contexts.push({
id: ctx.name,
label: ctx.name,
subjectCount: countByContext.get(contextKey) ?? 0,
mode: ctx.mode,
compatibility: ctx.compatibility,
});
}
// "All" uses the default context's mode/compat, falling back to empty strings
contexts.push({
id: ALL_CONTEXT_ID,
label: 'All',
subjectCount: activeSubjects.length,
mode: defaultContext?.mode ?? '',
compatibility: defaultContext?.compatibility ?? '',
});
return contexts;
}
// True for actual SR contexts (not the synthetic
// "All" or "Default" entries).
export function isNamedContext(contextId: string): boolean {
return contextId != '' && contextId !== ALL_CONTEXT_ID && contextId !== DEFAULT_CONTEXT_ID;
}
// Convert a raw context name (e.g. from a URL param or parseSubjectContext)
// into the internal context ID used by the editor state.
// ".staging" → ".staging", "default" → DEFAULT_CONTEXT_ID, "prod" → ".prod"
export function contextNameToId(name: string): string {
if (name === 'default') return DEFAULT_CONTEXT_ID;
if (name.startsWith('.')) return name;
return `.${name}`;
}
// Build a qualified subject name from context + subject.
// Named contexts (e.g. ".staging") → ":.staging:subject"
// Default context → plain "subject"
export function buildQualifiedSubjectName(contextId: string, subjectName: string): string {
if (!subjectName) return '';
if (isNamedContext(contextId)) return `:${contextId}:${subjectName}`;
return subjectName;
}
// Map between internal context IDs and display labels.
// DEFAULT_CONTEXT_ID ('__default__') ↔ 'Default'; all others pass through.
export function contextIdToLabel(contextId: string): string {
return contextId === DEFAULT_CONTEXT_ID ? DEFAULT_CONTEXT_LABEL : contextId;
}
export function contextLabelToId(label: string): string {
return label === DEFAULT_CONTEXT_LABEL ? DEFAULT_CONTEXT_ID : label;
}
// Build qualified references for API calls (create/validate).
// When the parent subject is in a named context and a reference targets the
// default context, explicitly qualify it as `:.:subject` so the SR doesn't
// auto-prefix with the parent's context.
export function buildQualifiedReferences(
refs: { name: string; subject: string; version: number; context: string }[],
parentContext: string
): { name: string; subject: string; version: number }[] {
return refs
.filter((x) => x.name && x.subject)
.map((r) => ({
name: r.name,
subject:
isNamedContext(parentContext) && !isNamedContext(r.context)
? buildQualifiedSubjectName('.', r.subject)
: buildQualifiedSubjectName(r.context, r.subject),
version: r.version,
}));
}
// Simple English pluralization: 1 subject / 3 subjects.
export function pluralize(count: number, singular: string) {
return `${count} ${singular}${count === 1 ? '' : 's'}`;
}