@voxgig/sdkgen 1.3.2 → 1.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/voxgig-sdkgen +1 -1
- package/dist/cmp/ReadmeErrors.js +15 -15
- package/dist/cmp/ReadmeErrors.js.map +1 -1
- package/dist/cmp/ReadmeExplanation.js +37 -35
- package/dist/cmp/ReadmeExplanation.js.map +1 -1
- package/dist/cmp/ReadmeTop.js +24 -2
- package/dist/cmp/ReadmeTop.js.map +1 -1
- package/dist/helpers/naming.js +29 -2
- package/dist/helpers/naming.js.map +1 -1
- package/dist/helpers/opShape.d.ts +6 -1
- package/dist/helpers/opShape.js +78 -0
- package/dist/helpers/opShape.js.map +1 -1
- package/dist/sdkgen.d.ts +2 -2
- package/dist/sdkgen.js +3 -1
- package/dist/sdkgen.js.map +1 -1
- package/package.json +1 -1
- package/project/.sdk/src/cmp/go/EntityOperation_go.ts +8 -1
- package/project/.sdk/src/cmp/go/Entity_go.ts +20 -3
- package/project/.sdk/src/cmp/go/Main_go.ts +2 -1
- package/project/.sdk/src/cmp/go/ReadmeEntity_go.ts +5 -5
- package/project/.sdk/src/cmp/go/ReadmeHowto_go.ts +23 -12
- package/project/.sdk/src/cmp/go/ReadmeModel_go.ts +27 -17
- package/project/.sdk/src/cmp/go/ReadmeRef_go.ts +5 -5
- package/project/.sdk/src/cmp/go/ReadmeTopTest_go.ts +5 -6
- package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +11 -11
- package/project/.sdk/src/cmp/go/fragment/EntityCreateOp.fragment.go +2 -2
- package/project/.sdk/src/cmp/go/fragment/EntityListOp.fragment.go +2 -2
- package/project/.sdk/src/cmp/go/fragment/EntityLoadOp.fragment.go +2 -2
- package/project/.sdk/src/cmp/go/fragment/EntityRemoveOp.fragment.go +2 -2
- package/project/.sdk/src/cmp/go/fragment/EntityUpdateOp.fragment.go +2 -2
- package/project/.sdk/src/cmp/js/Entity_js.ts +18 -2
- package/project/.sdk/src/cmp/js/MainEntity_js.ts +12 -2
- package/project/.sdk/src/cmp/js/Main_js.ts +5 -2
- package/project/.sdk/src/cmp/js/ReadmeEntity_js.ts +5 -5
- package/project/.sdk/src/cmp/js/ReadmeHowto_js.ts +37 -26
- package/project/.sdk/src/cmp/js/ReadmeRef_js.ts +5 -5
- package/project/.sdk/src/cmp/js/ReadmeTopTest_js.ts +5 -6
- package/project/.sdk/src/cmp/js/fragment/Entity.fragment.js +3 -3
- package/project/.sdk/src/cmp/lua/EntityOperation_lua.ts +6 -1
- package/project/.sdk/src/cmp/lua/Entity_lua.ts +20 -2
- package/project/.sdk/src/cmp/lua/ReadmeEntity_lua.ts +8 -5
- package/project/.sdk/src/cmp/lua/ReadmeHowto_lua.ts +14 -7
- package/project/.sdk/src/cmp/lua/ReadmeQuick_lua.ts +9 -6
- package/project/.sdk/src/cmp/lua/ReadmeRef_lua.ts +5 -2
- package/project/.sdk/src/cmp/lua/ReadmeTopQuick_lua.ts +8 -5
- package/project/.sdk/src/cmp/lua/ReadmeTopTest_lua.ts +5 -6
- package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +13 -13
- package/project/.sdk/src/cmp/lua/fragment/EntityCreateOp.fragment.lua +1 -1
- package/project/.sdk/src/cmp/lua/fragment/EntityListOp.fragment.lua +1 -1
- package/project/.sdk/src/cmp/lua/fragment/EntityLoadOp.fragment.lua +1 -1
- package/project/.sdk/src/cmp/lua/fragment/EntityRemoveOp.fragment.lua +1 -1
- package/project/.sdk/src/cmp/lua/fragment/EntityUpdateOp.fragment.lua +1 -1
- package/project/.sdk/src/cmp/php/Entity_php.ts +17 -1
- package/project/.sdk/src/cmp/php/MainEntity_php.ts +12 -3
- package/project/.sdk/src/cmp/php/ReadmeHowto_php.ts +15 -8
- package/project/.sdk/src/cmp/php/ReadmeTopTest_php.ts +5 -6
- package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +2 -2
- package/project/.sdk/src/cmp/py/Entity_py.ts +18 -1
- package/project/.sdk/src/cmp/py/MainEntity_py.ts +14 -4
- package/project/.sdk/src/cmp/py/Main_py.ts +2 -1
- package/project/.sdk/src/cmp/py/ReadmeEntity_py.ts +8 -5
- package/project/.sdk/src/cmp/py/ReadmeHowto_py.ts +18 -8
- package/project/.sdk/src/cmp/py/ReadmeQuick_py.ts +10 -7
- package/project/.sdk/src/cmp/py/ReadmeRef_py.ts +7 -4
- package/project/.sdk/src/cmp/py/ReadmeTopQuick_py.ts +9 -6
- package/project/.sdk/src/cmp/py/ReadmeTopTest_py.ts +6 -7
- package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +2 -2
- package/project/.sdk/src/cmp/rb/Entity_rb.ts +17 -1
- package/project/.sdk/src/cmp/rb/MainEntity_rb.ts +11 -2
- package/project/.sdk/src/cmp/rb/ReadmeEntity_rb.ts +8 -5
- package/project/.sdk/src/cmp/rb/ReadmeHowto_rb.ts +20 -8
- package/project/.sdk/src/cmp/rb/ReadmeQuick_rb.ts +9 -6
- package/project/.sdk/src/cmp/rb/ReadmeRef_rb.ts +5 -2
- package/project/.sdk/src/cmp/rb/ReadmeTopQuick_rb.ts +8 -5
- package/project/.sdk/src/cmp/rb/ReadmeTopTest_rb.ts +10 -8
- package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +2 -2
- package/project/.sdk/src/cmp/ts/Entity_ts.ts +18 -2
- package/project/.sdk/src/cmp/ts/MainEntity_ts.ts +12 -2
- package/project/.sdk/src/cmp/ts/Main_ts.ts +5 -2
- package/project/.sdk/src/cmp/ts/ReadmeEntity_ts.ts +5 -5
- package/project/.sdk/src/cmp/ts/ReadmeHowto_ts.ts +36 -26
- package/project/.sdk/src/cmp/ts/ReadmeRef_ts.ts +4 -5
- package/project/.sdk/src/cmp/ts/ReadmeTopTest_ts.ts +5 -6
- package/project/.sdk/src/cmp/ts/fragment/Entity.fragment.ts +4 -4
- package/project/.sdk/tm/go/utility/prepare_auth.go +6 -1
- package/project/.sdk/tm/js/src/utility/PrepareAuthUtility.js +3 -1
- package/project/.sdk/tm/lua/utility/prepare_auth.lua +6 -1
- package/project/.sdk/tm/php/utility/PrepareAuth.php +3 -1
- package/project/.sdk/tm/py/utility/prepare_auth.py +4 -1
- package/project/.sdk/tm/rb/utility/prepare_auth.rb +3 -1
- package/project/.sdk/tm/ts/src/utility/PrepareAuthUtility.ts +5 -1
- package/src/cmp/ReadmeErrors.ts +16 -17
- package/src/cmp/ReadmeExplanation.ts +37 -35
- package/src/cmp/ReadmeTop.ts +25 -2
- package/src/helpers/naming.ts +32 -2
- package/src/helpers/opShape.ts +89 -0
- package/src/sdkgen.ts +3 -1
|
@@ -38,6 +38,9 @@ def prepare_auth_util(ctx):
|
|
|
38
38
|
apikey_val = ""
|
|
39
39
|
if isinstance(apikey, str):
|
|
40
40
|
apikey_val = apikey
|
|
41
|
-
|
|
41
|
+
# Empty prefix (raw apiKey credential) must not add a leading space.
|
|
42
|
+
headers[HEADER_AUTH] = (
|
|
43
|
+
auth_prefix + " " + apikey_val if auth_prefix else apikey_val
|
|
44
|
+
)
|
|
42
45
|
|
|
43
46
|
return spec, None
|
|
@@ -25,7 +25,9 @@ module ProjectNameUtilities
|
|
|
25
25
|
else
|
|
26
26
|
auth_prefix = VoxgigStruct.getpath(options, "auth.prefix") || ""
|
|
27
27
|
apikey_val = apikey.is_a?(String) ? apikey : ""
|
|
28
|
-
|
|
28
|
+
# Empty prefix (raw apiKey credential) must not add a leading space.
|
|
29
|
+
headers[HEADER_AUTH] =
|
|
30
|
+
auth_prefix.empty? ? apikey_val : "#{auth_prefix} #{apikey_val}"
|
|
29
31
|
end
|
|
30
32
|
|
|
31
33
|
return spec, nil
|
|
@@ -36,13 +36,17 @@ function prepareAuth(ctx: Context): Spec | Error {
|
|
|
36
36
|
return spec
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
const prefix = options.auth.prefix
|
|
40
|
+
|
|
39
41
|
const apikey = getprop(options, OPTION_apikey, NOTFOUND)
|
|
40
42
|
|
|
41
43
|
if (NOTFOUND === apikey || null == apikey || '' === apikey) {
|
|
42
44
|
delprop(headers, HEADER_auth)
|
|
43
45
|
}
|
|
44
46
|
else {
|
|
45
|
-
|
|
47
|
+
// A raw credential (empty prefix, e.g. an apiKey scheme) must go in
|
|
48
|
+
// as-is; only a non-empty prefix (Bearer/Basic/OAuth) is space-joined.
|
|
49
|
+
setprop(headers, HEADER_auth, prefix ? prefix + ' ' + apikey : apikey)
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
return spec
|
package/src/cmp/ReadmeErrors.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
getModelPath
|
|
7
7
|
} from '../types'
|
|
8
8
|
|
|
9
|
-
import { entityIdField,
|
|
9
|
+
import { entityIdField, pickExampleEntity } from '../helpers/opShape'
|
|
10
10
|
import { primaryOpCall } from '../helpers/opExample'
|
|
11
11
|
import type { ExampleLang, PrimaryCall } from '../helpers/opExample'
|
|
12
12
|
import { safeVarName } from '../helpers/naming'
|
|
@@ -224,29 +224,28 @@ const ReadmeErrors = cmp(function ReadmeErrors(props: any) {
|
|
|
224
224
|
|
|
225
225
|
const lang = LANGS[target.name] || DEFAULT_LANG
|
|
226
226
|
|
|
227
|
-
//
|
|
228
|
-
//
|
|
227
|
+
// Pick a real example entity WITH a real op (prefer a read op) so the
|
|
228
|
+
// error-handling snippet never references a phantom entity or fabricates a
|
|
229
|
+
// `.load()` on an op-less one (e.g. Cloudsmith's `Abort`). primaryOp is null
|
|
230
|
+
// only when NO entity exposes any op — then the entity example is skipped
|
|
231
|
+
// and only the direct() error handling (which every SDK has) is shown.
|
|
229
232
|
const entity = getModelPath(model, `main.${KIT}.entity`, { only_active: false, required: false })
|
|
230
|
-
const ex =
|
|
231
|
-
const eName = ex ? (ex.Name || (ex.name[0].toUpperCase() + ex.name.slice(1))) : 'Entity'
|
|
232
|
-
// Sanitise the variable name against the target's reserved words (a `Delete`
|
|
233
|
-
// entity must not bind `const delete = ...`).
|
|
234
|
-
const eLower = safeVarName(eName.toLowerCase(), target.name)
|
|
235
|
-
|
|
236
|
-
// The entity's id-like key field name, or null when it has none.
|
|
237
|
-
const idF = entityIdField(ex)
|
|
238
|
-
// The entity's PRIMARY op — an op it actually exposes (prefer list/load, else
|
|
239
|
-
// create/update/remove). A create-only entity therefore never shows a
|
|
240
|
-
// phantom `.load()`.
|
|
241
|
-
const primaryOp = entityPrimaryOp(ex) || 'load'
|
|
242
|
-
const call = primaryOpCall(target.name as ExampleLang, eName, eLower, primaryOp, idF, ex)
|
|
233
|
+
const { entity: ex, primaryOp } = pickExampleEntity(entity || {})
|
|
243
234
|
|
|
244
235
|
Content(`
|
|
245
236
|
## Error handling
|
|
246
237
|
|
|
247
238
|
`)
|
|
248
239
|
|
|
249
|
-
|
|
240
|
+
if (ex && primaryOp) {
|
|
241
|
+
const eName = ex.Name || (ex.name[0].toUpperCase() + ex.name.slice(1))
|
|
242
|
+
// Sanitise the variable name against the target's reserved words (a
|
|
243
|
+
// `Delete` entity must not bind `const delete = ...`).
|
|
244
|
+
const eLower = safeVarName(eName.toLowerCase(), target.name)
|
|
245
|
+
const idF = entityIdField(ex)
|
|
246
|
+
const call = primaryOpCall(target.name as ExampleLang, eName, eLower, primaryOp, idF, ex)
|
|
247
|
+
Content(lang.entity(call, primaryOp))
|
|
248
|
+
}
|
|
250
249
|
|
|
251
250
|
Content(lang.direct)
|
|
252
251
|
})
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
|
|
9
9
|
import { requirePath } from '../utility'
|
|
10
10
|
|
|
11
|
-
import { entityIdField,
|
|
11
|
+
import { entityIdField, pickExampleEntity } from '../helpers/opShape'
|
|
12
12
|
import { idLiteral, matchArg, dataArg } from '../helpers/opExample'
|
|
13
13
|
import type { ExampleLang } from '../helpers/opExample'
|
|
14
14
|
import { safeVarName } from '../helpers/naming'
|
|
@@ -225,39 +225,40 @@ const ReadmeExplanation = cmp(function ReadmeExplanation(props: any) {
|
|
|
225
225
|
const feature = getModelPath(model, `main.${KIT}.feature`)
|
|
226
226
|
const lang = LANGS[target.name] || DEFAULT_LANG
|
|
227
227
|
|
|
228
|
-
//
|
|
229
|
-
//
|
|
230
|
-
//
|
|
228
|
+
// Pick a real example entity WITH a real op (prefer a read op) so the
|
|
229
|
+
// entity-state example never references a phantom entity or fabricates a
|
|
230
|
+
// `.load()` on an op-less one (e.g. Cloudsmith's `Abort`). primaryOp is null
|
|
231
|
+
// only when NO entity exposes any op — then the entity-state section is
|
|
232
|
+
// skipped (a direct()-only SDK has no entity op to illustrate).
|
|
231
233
|
const entity = getModelPath(model, `main.${KIT}.entity`, { only_active: false, required: false })
|
|
232
|
-
const ex =
|
|
233
|
-
const eName = ex ? (ex.Name || (ex.name[0].toUpperCase() + ex.name.slice(1))) : 'Entity'
|
|
234
|
-
// Sanitise against the target's reserved words (a `Delete` entity must not
|
|
235
|
-
// bind `const delete = ...`).
|
|
236
|
-
const eLower = safeVarName(eName.toLowerCase(), target.name)
|
|
234
|
+
const { entity: ex, primaryOp } = pickExampleEntity(entity || {})
|
|
237
235
|
const lname = target.name as ExampleLang
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
236
|
+
const hasEntityExample = !!(ex && primaryOp)
|
|
237
|
+
|
|
238
|
+
let eName = 'Entity', eLower = 'entity', stateArg = '', matchIdF: string | null = null, idLit = ''
|
|
239
|
+
if (hasEntityExample) {
|
|
240
|
+
eName = ex.Name || (ex.name[0].toUpperCase() + ex.name.slice(1))
|
|
241
|
+
// Sanitise against the target's reserved words (a `Delete` entity must
|
|
242
|
+
// not bind `const delete = ...`).
|
|
243
|
+
eLower = safeVarName(eName.toLowerCase(), target.name)
|
|
244
|
+
const idF = entityIdField(ex)
|
|
245
|
+
const isMatchOp = 'load' === primaryOp || 'remove' === primaryOp
|
|
246
|
+
// Type-correct example id literal (numeric when the id param is integer-
|
|
247
|
+
// typed), derived from the OP's param type so an id carried only in the
|
|
248
|
+
// match compiles.
|
|
249
|
+
idLit = idLiteral(ex, primaryOp as string, idF)
|
|
250
|
+
// Language-correct call argument for the primary op: a match for
|
|
251
|
+
// load/remove, a required-field body for create/update, nothing for list.
|
|
252
|
+
if ('list' === primaryOp) {
|
|
253
|
+
stateArg = 'go' === target.name ? 'nil' : ''
|
|
254
|
+
} else if (isMatchOp) {
|
|
255
|
+
stateArg = matchArg(lname, idF, idLit)
|
|
256
|
+
} else {
|
|
257
|
+
stateArg = dataArg(lname, ex, primaryOp as string, idF)
|
|
258
|
+
}
|
|
259
|
+
// Only a match op keys the `.match()` comment on `{ id: ... }`.
|
|
260
|
+
matchIdF = isMatchOp ? idF : null
|
|
258
261
|
}
|
|
259
|
-
// Only a match op keys the `.match()` comment on `{ id: ... }`.
|
|
260
|
-
const matchIdF = isMatchOp ? idF : null
|
|
261
262
|
|
|
262
263
|
Content(`
|
|
263
264
|
## Advanced
|
|
@@ -327,12 +328,13 @@ were added, so later features can override earlier ones.
|
|
|
327
328
|
}
|
|
328
329
|
|
|
329
330
|
|
|
330
|
-
// Entity state
|
|
331
|
-
|
|
331
|
+
// Entity state — only when the SDK actually has an entity op to show.
|
|
332
|
+
if (hasEntityExample) {
|
|
333
|
+
Content(`### Entity state
|
|
332
334
|
|
|
333
335
|
`)
|
|
334
|
-
|
|
335
|
-
|
|
336
|
+
Content(lang.entityState(eName, eLower, primaryOp as string, stateArg, matchIdF, idLit))
|
|
337
|
+
}
|
|
336
338
|
|
|
337
339
|
|
|
338
340
|
// Direct vs entity access
|
package/src/cmp/ReadmeTop.ts
CHANGED
|
@@ -86,6 +86,17 @@ const ReadmeTop = cmp(function ReadmeTop(props: any) {
|
|
|
86
86
|
const docsUrl = info.docs_url || ''
|
|
87
87
|
const entityDesc = info.entity_desc || {}
|
|
88
88
|
|
|
89
|
+
// Spec-derived (apidef): a short "what this API is" blurb and a link back
|
|
90
|
+
// to the API's own website. Both surface right under the title, before
|
|
91
|
+
// the unofficial-SDK disclosure, so a reader immediately sees what the
|
|
92
|
+
// underlying API is and where it comes from.
|
|
93
|
+
const apiSummary = (info.summary || '').trim()
|
|
94
|
+
const apiWebsite = (info.website || '').trim()
|
|
95
|
+
const websiteLine = apiWebsite
|
|
96
|
+
? `Learn more about ${productName} at ` +
|
|
97
|
+
`[${apiWebsite.replace(/^https?:\/\//, '').replace(/\/$/, '')}](${apiWebsite}).`
|
|
98
|
+
: ''
|
|
99
|
+
|
|
89
100
|
const entity = getModelPath(model, `main.${KIT}.entity`)
|
|
90
101
|
const target = getModelPath(model, `main.${KIT}.target`)
|
|
91
102
|
const feature = getModelPath(model, `main.${KIT}.feature`)
|
|
@@ -123,12 +134,24 @@ const ReadmeTop = cmp(function ReadmeTop(props: any) {
|
|
|
123
134
|
|
|
124
135
|
File({ name: 'README.md' }, () => {
|
|
125
136
|
|
|
126
|
-
// 1. H1 + one-line value prop
|
|
137
|
+
// 1. H1 + one-line value prop, then the API's own purpose + website
|
|
138
|
+
// (spec-derived), then the unofficial / non-affiliation disclosure.
|
|
127
139
|
Content(`# ${model.Name} SDK
|
|
128
140
|
|
|
129
141
|
${tagline}
|
|
130
142
|
|
|
131
|
-
|
|
143
|
+
`)
|
|
144
|
+
if (apiSummary) {
|
|
145
|
+
Content(`${apiSummary}
|
|
146
|
+
|
|
147
|
+
`)
|
|
148
|
+
}
|
|
149
|
+
if (websiteLine) {
|
|
150
|
+
Content(`${websiteLine}
|
|
151
|
+
|
|
152
|
+
`)
|
|
153
|
+
}
|
|
154
|
+
Content(`${nonAffiliation(model)}
|
|
132
155
|
|
|
133
156
|
`)
|
|
134
157
|
|
package/src/helpers/naming.ts
CHANGED
|
@@ -11,8 +11,11 @@
|
|
|
11
11
|
// words a `const` binding trips on). Go lists its 25 keywords — a var named
|
|
12
12
|
// `type`/`func`/`range` fails `go build`; Go builtins like `delete`/`len` are
|
|
13
13
|
// NOT keywords and are legal identifiers, so they are intentionally omitted.
|
|
14
|
-
// py/
|
|
15
|
-
//
|
|
14
|
+
// rb/py/lua list the keywords that are illegal as a local-variable binding
|
|
15
|
+
// (e.g. a `Self` entity -> `self = client.Self` is a Ruby SyntaxError, an
|
|
16
|
+
// `End` entity -> `end = ...` a Lua one). PHP variables are `$`-prefixed so a
|
|
17
|
+
// lowercased entity name never collides (only `$this` is special, and no
|
|
18
|
+
// entity is named `this`), so PHP is not treated.
|
|
16
19
|
|
|
17
20
|
const JS_RESERVED = new Set<string>([
|
|
18
21
|
'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger',
|
|
@@ -32,10 +35,37 @@ const GO_RESERVED = new Set<string>([
|
|
|
32
35
|
'var',
|
|
33
36
|
])
|
|
34
37
|
|
|
38
|
+
// Ruby keywords — illegal as a local-variable name (e.g. `self`, `end`).
|
|
39
|
+
const RB_RESERVED = new Set<string>([
|
|
40
|
+
'alias', 'and', 'begin', 'break', 'case', 'class', 'def', 'defined?',
|
|
41
|
+
'do', 'else', 'elsif', 'end', 'ensure', 'false', 'for', 'if', 'in',
|
|
42
|
+
'module', 'next', 'nil', 'not', 'or', 'redo', 'rescue', 'retry', 'return',
|
|
43
|
+
'self', 'super', 'then', 'true', 'undef', 'unless', 'until', 'when',
|
|
44
|
+
'while', 'yield', '__FILE__', '__LINE__', '__ENCODING__', 'BEGIN', 'END',
|
|
45
|
+
])
|
|
46
|
+
|
|
47
|
+
// Python keywords — illegal as a variable name.
|
|
48
|
+
const PY_RESERVED = new Set<string>([
|
|
49
|
+
'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
|
|
50
|
+
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
|
|
51
|
+
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
|
|
52
|
+
'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield',
|
|
53
|
+
])
|
|
54
|
+
|
|
55
|
+
// Lua keywords — illegal as a variable name. (`self` is NOT reserved in Lua.)
|
|
56
|
+
const LUA_RESERVED = new Set<string>([
|
|
57
|
+
'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function',
|
|
58
|
+
'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return',
|
|
59
|
+
'then', 'true', 'until', 'while',
|
|
60
|
+
])
|
|
61
|
+
|
|
35
62
|
const RESERVED: Record<string, Set<string>> = {
|
|
36
63
|
ts: JS_RESERVED,
|
|
37
64
|
js: JS_RESERVED,
|
|
38
65
|
go: GO_RESERVED,
|
|
66
|
+
rb: RB_RESERVED,
|
|
67
|
+
py: PY_RESERVED,
|
|
68
|
+
lua: LUA_RESERVED,
|
|
39
69
|
}
|
|
40
70
|
|
|
41
71
|
|
package/src/helpers/opShape.ts
CHANGED
|
@@ -238,6 +238,93 @@ function entityPrimaryOp(ent: any): string | null {
|
|
|
238
238
|
}
|
|
239
239
|
|
|
240
240
|
|
|
241
|
+
// Collision-free target-language CLASS name for an entity. The natural
|
|
242
|
+
// class name is `<Name>Entity`, but that can equal another entity's
|
|
243
|
+
// canonical DATA-type name `<Name'>` when Name' === Name + 'Entity' (e.g.
|
|
244
|
+
// GitLab's `project` -> class `ProjectEntity` collides with `project_entity`
|
|
245
|
+
// -> data type `ProjectEntity`), which redeclares a type in Go and merges/
|
|
246
|
+
// shadows it in ts/py/rb. This assigns each entity a class name that is
|
|
247
|
+
// unique across ALL emitted top-level type names (every entity's data type
|
|
248
|
+
// and per-op Match/Data types, plus already-assigned class names): the
|
|
249
|
+
// natural `<Name>Entity` when free, else `<Name>EntityClient`, `...Client2`,
|
|
250
|
+
// … The DATA type keeps its canonical `<Name>` — only the suffixed class
|
|
251
|
+
// yields. Deterministic (sorted-key iteration) and stable across runs.
|
|
252
|
+
//
|
|
253
|
+
// Memoised per entity-collection object so the O(n) assignment runs once.
|
|
254
|
+
const _classNameCache = new WeakMap<object, Record<string, string>>()
|
|
255
|
+
|
|
256
|
+
function entityClassNames(entityColl: any): Record<string, string> {
|
|
257
|
+
const cached = _classNameCache.get(entityColl)
|
|
258
|
+
if (null != cached) {
|
|
259
|
+
return cached
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const ents = each(entityColl).filter((e: any) => e && e.active !== false)
|
|
263
|
+
|
|
264
|
+
// 1. Every top-level DATA-type name the target emits.
|
|
265
|
+
const taken: Record<string, boolean> = {}
|
|
266
|
+
ents.forEach((e: any) => {
|
|
267
|
+
taken[e.Name] = true
|
|
268
|
+
for (const op of ['load', 'list', 'create', 'update', 'remove']) {
|
|
269
|
+
if (e.op && e.op[op]) {
|
|
270
|
+
taken[opTypeName(e.Name, op)] = true
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
// 2. Assign each class name, avoiding all data types and prior classes.
|
|
276
|
+
const out: Record<string, string> = {}
|
|
277
|
+
ents.forEach((e: any) => {
|
|
278
|
+
let name = e.Name + 'Entity'
|
|
279
|
+
if (taken[name]) {
|
|
280
|
+
const base = name + 'Client'
|
|
281
|
+
name = base
|
|
282
|
+
let n = 1
|
|
283
|
+
while (taken[name]) {
|
|
284
|
+
n++
|
|
285
|
+
name = base + n
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
taken[name] = true
|
|
289
|
+
out[e.name] = name
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
_classNameCache.set(entityColl, out)
|
|
293
|
+
return out
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
// The collision-free class name for one entity (see entityClassNames).
|
|
298
|
+
// `entityColl` is main.<KIT>.entity (the collection the entity belongs to).
|
|
299
|
+
function entityClassName(ent: any, entityColl: any): string {
|
|
300
|
+
if (null == ent) {
|
|
301
|
+
return ''
|
|
302
|
+
}
|
|
303
|
+
const map = entityClassNames(entityColl)
|
|
304
|
+
return map[ent.name] || (ent.Name + 'Entity')
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
// Pick a representative entity for a single illustrative snippet (e.g. the
|
|
309
|
+
// README's test-mode example): the first ACTIVE entity that exposes a read
|
|
310
|
+
// op (list/load) so the snippet is meaningful, else the first with ANY
|
|
311
|
+
// active op, else the first active entity. Returns { entity, primaryOp }
|
|
312
|
+
// where primaryOp is null only when NO entity has an op — so callers never
|
|
313
|
+
// fabricate an op the entity lacks (the cause of `.load()` on an op-less
|
|
314
|
+
// entity like Cloudsmith's `Abort`). Entities iterate in sorted-key order
|
|
315
|
+
// for deterministic output.
|
|
316
|
+
function pickExampleEntity(entity: any): { entity: any, primaryOp: string | null } {
|
|
317
|
+
const actives = each(entity).filter((e: any) => e && e.active !== false)
|
|
318
|
+
const readable = actives.find((e: any) => {
|
|
319
|
+
const op = entityPrimaryOp(e)
|
|
320
|
+
return 'list' === op || 'load' === op
|
|
321
|
+
})
|
|
322
|
+
const withOp = actives.find((e: any) => null != entityPrimaryOp(e))
|
|
323
|
+
const chosen = readable || withOp || actives[0] || null
|
|
324
|
+
return { entity: chosen, primaryOp: null == chosen ? null : entityPrimaryOp(chosen) }
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
|
|
241
328
|
// The id field on the entity's DATA type (its fields[]), or null. DISTINCT from
|
|
242
329
|
// entityIdField (the load-MATCH key): an API can model a load match that carries
|
|
243
330
|
// an `id` param while the response entity itself has no `id` field, so `.id`
|
|
@@ -267,6 +354,8 @@ export {
|
|
|
267
354
|
entityDataIdField,
|
|
268
355
|
entityOps,
|
|
269
356
|
entityPrimaryOp,
|
|
357
|
+
pickExampleEntity,
|
|
358
|
+
entityClassName,
|
|
270
359
|
}
|
|
271
360
|
|
|
272
361
|
export type {
|
package/src/sdkgen.ts
CHANGED
|
@@ -48,7 +48,7 @@ import { getMatchEntries } from './helpers/getMatchEntries'
|
|
|
48
48
|
import { collectDeps } from './helpers/collectDeps'
|
|
49
49
|
import type { DepEntry } from './helpers/collectDeps'
|
|
50
50
|
import { canonToType, canonKey } from './helpers/canonType'
|
|
51
|
-
import { OP_SUFFIX, opTypeName, opParams, opRequestShape, entityIdField, entityDataIdField, entityOps, entityPrimaryOp } from './helpers/opShape'
|
|
51
|
+
import { OP_SUFFIX, opTypeName, opParams, opRequestShape, entityIdField, entityDataIdField, entityOps, entityPrimaryOp, pickExampleEntity, entityClassName } from './helpers/opShape'
|
|
52
52
|
import { isReservedName, safeVarName } from './helpers/naming'
|
|
53
53
|
import {
|
|
54
54
|
packageName,
|
|
@@ -448,6 +448,8 @@ export {
|
|
|
448
448
|
entityDataIdField,
|
|
449
449
|
entityOps,
|
|
450
450
|
entityPrimaryOp,
|
|
451
|
+
pickExampleEntity,
|
|
452
|
+
entityClassName,
|
|
451
453
|
isReservedName,
|
|
452
454
|
safeVarName,
|
|
453
455
|
|