Skip to content

Commit f8d1c2c

Browse files
author
Christy Haragan
committed
Ensure object generation is correct
1 parent 814def5 commit f8d1c2c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+4290
-4206
lines changed

README.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
11
# marklogic-typescript-definitions
2-
TypeScript Definition files for MarkLogic built-in functions, available as npm module
2+
TypeScript definition files for MarkLogic server-side-javascript functions and types, available as npm an module.
3+
4+
For the node.js client definitions try the [marklogic-node-typescript-definitions](https://github.com/christyharagan/marklogic-node-typescript-definitions) package
35

46
## Usage
57

6-
Install these files as nodejs dependency using npm:
8+
Install these files as node.js dependency using npm:
79

810
```
911
npm install marklogic-typescript-definitions --save
1012
```
1113

12-
TypeScript enabled editors should pick up the TypeScript definition files automatically
14+
To use the definitions, modify your ```tsconfig.json``` file, adding the following entry to the ```files``` section:
15+
16+
```
17+
node_modules/marklogic-typescript-definitions/ts/index.d.ts
18+
```
19+
20+
So, for example, your ```tsconfig.json``` file might look like:
21+
22+
```json
23+
{
24+
"compilerOptions": {
25+
"module": "commonjs",
26+
"target": "es5",
27+
},
28+
"files": [
29+
"./lib/myCode.ts",
30+
"./node_modules/marklogic-typescript-definitions/ts/index.d.ts"
31+
]
32+
}
33+
```
34+
35+
## Suggested TypeScript Editors
36+
37+
Below are two suggested TypeScript editors:
1338

14-
## Editor
39+
- (Visual Studio Code)[https://code.visualstudio.com/] runs out of the box with TypeScript support.
40+
- (Atom)[https://atom.io/] also provides good support (if not better), but requires installation of the ```atom-typescript``` plugin.
1541

16-
- (Visual Studio Code)[https://code.visualstudio.com/] runs out of the box with this.
17-
- (Atom)[https://atom.io/] also provides good support (if not better), but requires installation of a TypeScript plugin.
42+
## Building the TypeScript definitions
1843

19-
## Building the typescript definitions
44+
The git repository (and npm package) contains the built definitions. If you'd like to re-build them, perform the following:
2045

2146
- Clone this project
2247
- Create a folder xml/
2348
- Download a copy of the documentation from here: http://docs.marklogic.com/MarkLogic_8_pubs.zip
2449
- Extract the zip, and copy the contents of the folder pubs/raw/apidoc/ into xml/
50+
- Delete the files listed below
2551
- Run `npm install`
2652
- Run `gulp` (may take a few minutes)
2753

@@ -31,7 +57,7 @@ Note: you likely want to override ml-host, ml-user, and such. It runs by default
3157
gulp --ml-host=ml8-ml1 --ml-port=8000 --ml-user=admin --ml-pass=admin
3258
```
3359

34-
#### XML Files to ignore
60+
#### XML files to delete
3561
Before trying to build, remove the following xml files (as some of these are xquery only modules):
3662

3763
- admin

gulpfile.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var fs = require('fs');
99
var request = require('request');
1010
var through = require('through2');
1111
var typescript = require('gulp-tsc');
12+
var concat = require('gulp-concat');
13+
var File = require('vinyl');
1214

1315
var onError = function(err) {
1416
if (err) {
@@ -23,21 +25,17 @@ var options = {
2325
mlPass: argv['ml-pass'] || 'passw0rd'
2426
};
2527

28+
var createAddHeader = function(){
29+
return through.obj(function(file){
30+
file.contents = new Buffer('///<reference path="./types.d.ts" />\n' + file.contents.toString())
31+
this.push(file)
32+
})
33+
}
2634

27-
gulp.task('generate', function(){
28-
var query;
29-
30-
fs.readFile('qconsole/generate-definitions.xqy', null, function(err, data) {
31-
query = data.toString();
32-
});
33-
34-
return gulp.src(['xml/**/*.xml'])
35-
.pipe(through.obj(function (file, enc, cb) {
36-
37-
console.log(file.path);
38-
39-
var outFile = file.path.replace('/xml/', '/ts/').replace('.xml', '.d.ts');
35+
var createProcessXML = function(){
36+
var query = fs.readFileSync('qconsole/generate-definitions.xqy').toString();
4037

38+
return through.obj(function (file, enc, cb) {
4139
var xml = file.contents.toString();
4240
var escapedXml = xml.replace(/\\/gm, '\\\\').replace(/"/gm, '\\"').replace(/(\n\r|\r\n|\n|\r)/gm, '\\n').replace(/\t/gm, '\\t');
4341

@@ -54,25 +52,33 @@ gulp.task('generate', function(){
5452
sendImmediately: false
5553
}
5654
}, function(err, httpResponse, body) {
57-
//console.log(body);
5855
try {
5956
// get rid of multipart response wrapping
6057
body = body.replace(/^([^\r]*\r\n){5}/, '').replace(/\r\n[^\r]*\r\n$/, '');
6158

6259
if (err || httpResponse.statusCode !== 200) {
63-
console.log('FAILED!');
64-
console.log(body);
65-
//console.log(escapedXml);
60+
console.log(file.path)
61+
cb(new Error(body))
6662
} else {
67-
fs.writeFile(outFile, body, onError);
63+
cb(null, new File({
64+
base: file.base,
65+
path: file.path.replace('/xml/', '/ts/').replace('.xml', '.d.ts'),
66+
contents: new Buffer(body)
67+
}));
6868
}
69-
70-
cb(null, file);
7169
} catch (e) {
7270
console.log(e);
7371
}
7472
});
75-
}));
73+
});
74+
}
75+
76+
gulp.task('generate', function(){
77+
return gulp.src(['xml/**/*.xml'])
78+
.pipe(createProcessXML())
79+
.pipe(concat('functions.d.ts'))
80+
.pipe(createAddHeader())
81+
.pipe(gulp.dest('./ts/'));
7682
});
7783

7884
gulp.task('validate', ['generate'], function(){

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "marklogic-typescript-definitions",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "TypeScript Definition files for MarkLogic built-in functions",
55
"author": "Geert Josten <geert.josten@gmail.com>",
66
"contributors": [
7-
{ "name" : "Christy Haragan",
7+
{
8+
"name": "Christy Haragan",
89
"email": "christy.haragan.github@gmail.com",
910
"url": "https://github.com/christyharagan"
1011
}
1112
],
12-
"typings": "./ts/index.d.ts",
1313
"keywords": [
1414
"typescript",
1515
"marklogic"
@@ -19,6 +19,7 @@
1919
"gulp-tsc": "^1.1.1",
2020
"request": "^2.62.0",
2121
"through2": "^2.0.0",
22+
"vinyl": "^1.0.0",
2223
"yargs": "^3.25.0"
2324
},
2425
"engine": "node >= 0.4.1",

qconsole/generate-definitions.xqy

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ let $definition := xdmp:xslt-eval(
3333
<xsl:if test="preceding-sibling::apidoc:param[not(@class) or @class = 'javascript'][@name = current()/@name]">2</xsl:if>
3434
<xsl:if test="not($is-multi) and string(@optional) = 'true'">?</xsl:if>
3535
<xsl:text>: </xsl:text>
36-
<xsl:value-of select="local:fixType(@type)"/>
36+
<xsl:value-of select="local:fixType(@type, true())"/>
3737
<xsl:if test="$is-multi">[]</xsl:if>
3838
<xsl:if test="not(position() = last())">, </xsl:if>
3939
</xsl:for-each>
4040
<xsl:text>): </xsl:text>
41-
<xsl:value-of select="local:fixType(string(apidoc:return[not(@class) or @class = 'javascript']))"/>
41+
<xsl:value-of select="local:fixType(string(apidoc:return[not(@class) or @class = 'javascript']), false())"/>
4242
<xsl:text>;&#10;&#10;</xsl:text>
4343
</xsl:template>
4444

@@ -89,34 +89,82 @@ let $definition := xdmp:xslt-eval(
8989

9090
<xsl:function name="local:fixType">
9191
<xsl:param name="pType"/>
92+
<xsl:param name="isForParameter"/>
9293

93-
<xsl:variable name="preType" select="replace(replace($pType, '^(.+)[?+*](,\.\.\.)?$', '$1'), ' ', '')"/>
94+
<xsl:variable name="preTypeAndCardinality" select="replace(replace($pType, '^([^,]+)(,\.\.\.)?$', '$1'), ' ', '')"/>
95+
<xsl:variable name="lastCharacter" select="substring($preTypeAndCardinality, string-length($preTypeAndCardinality))"/>
96+
<xsl:variable name="preType" select="if (matches($lastCharacter, '^[?*+]$')) then substring($preTypeAndCardinality, 1, string-length($preTypeAndCardinality) - 1) else $preTypeAndCardinality"/>
97+
<xsl:variable name="isArray" select="matches($lastCharacter, '^[*+]$')"/>
9498
<xsl:variable name="type" select="if (substring($preType, 1, 1) = '(') then substring($preType, 2, string-length($preType) - 2) else $preType"/>
9599

96-
<xsl:value-of select="local:fixEachType($type)"/>
100+
<xsl:value-of select="local:fixEachType($type, $isArray, $isForParameter)"/>
97101
</xsl:function>
98102

99103
<xsl:function name="local:fixEachType">
100104
<xsl:param name="typeUnion"/>
105+
<xsl:param name="isArray"/>
106+
<xsl:param name="isForParameter"/>
101107

102108
<xsl:variable name="type" select="substring-before(concat($typeUnion,'|'),'|')"/>
103109

104110
<xsl:choose>
105-
<xsl:when test="$type = ('document-node()')">DocumentNode&lt;any&gt;</xsl:when>
106-
<xsl:when test="$type = ('binary()', 'element()', 'node()')">MLNode&lt;any&gt;</xsl:when>
111+
<xsl:when test="matches($type, '^function\(.*\)as(.+)$')">
112+
<xsl:value-of select="local:fixFunctionType($type, $isArray)"/>
113+
</xsl:when>
114+
<xsl:when test="$isArray">
115+
<xsl:choose>
116+
<xsl:when test="$type = ('document-node()', 'binary()', 'element()', 'node()')">
117+
<xsl:value-of select="local:fixArrayType('any', $isForParameter)"/>
118+
</xsl:when>
119+
<xsl:otherwise>
120+
<xsl:value-of select="local:fixArrayType(string-join(local:fixSingleType($type, $isForParameter), ''), $isForParameter)"/>
121+
</xsl:otherwise>
122+
</xsl:choose>
123+
</xsl:when>
124+
<xsl:otherwise>
125+
<xsl:value-of select="local:fixSingleType($type, $isForParameter)"/>
126+
</xsl:otherwise>
127+
</xsl:choose>
128+
129+
<xsl:if test="contains($typeUnion, '|')">
130+
<xsl:value-of select="concat('|', string-join(local:fixEachType(substring-after($typeUnion, '|'), $isArray, $isForParameter), ''))"/>
131+
</xsl:if>
132+
</xsl:function>
133+
134+
<xsl:function name="local:fixArrayType">
135+
<xsl:param name="type"/>
136+
<xsl:param name="isForParameter"/>
137+
<xsl:value-of select="concat(concat(if($isForParameter) then 'MLArray&lt;' else 'ValueIterator&lt;', $type), '&gt;')"/>
138+
</xsl:function>
139+
140+
<xsl:function name="local:fixSingleType">
141+
<xsl:param name="type"/>
142+
<xsl:param name="isForParameter"/>
143+
<xsl:choose>
144+
<xsl:when test="$type = 'ValueIterator'">ValueIterator&lt;any&gt;</xsl:when>
145+
<xsl:when test="$type = 'document-node()'">DocumentNode&lt;any&gt;</xsl:when>
146+
<xsl:when test="$type = ('binary()')">MLNode&lt;any&gt;</xsl:when>
147+
<xsl:when test="$type = ('element()', 'node()', 'item()')">
148+
<xsl:choose>
149+
<xsl:when test="$isForParameter">MLNodeOrObject&lt;any&gt;</xsl:when>
150+
<xsl:otherwise>MLNode&lt;any&gt;</xsl:otherwise>
151+
</xsl:choose>
152+
</xsl:when>
107153
<xsl:when test="$type = ('empty-sequence()', 'Null')">void</xsl:when>
108-
<xsl:when test="$type = ('String', 'item()', 'xs:anyURI', 'xs:NCName', 'xs:string', 'xs:time', 'xs:unsignedLong', 'xs:duration', 'xs:dayTimeDuration')">string</xsl:when>
154+
<xsl:when test="$type = ('String', 'xs:anyURI', 'xs:NCName', 'xs:string', 'xs:time', 'xs:duration', 'xs:dayTimeDuration')">string</xsl:when>
109155
<xsl:when test="$type = ('json:array', 'Array')">Array&lt;any&gt;</xsl:when>
110-
<xsl:when test="$type = 'ValueIterator'">ValueIterator&lt;any&gt;</xsl:when>
111156
<xsl:when test="$type = ('json:object', 'map:map')">&#123;&#91;key:string&#93;:any&#125;</xsl:when>
112157
<xsl:when test="$type = ('function()', 'xdmp:function', 'function(*', 'function(*)')">() => any</xsl:when>
113158
<xsl:when test="$type = 'xs:boolean'">boolean</xsl:when>
114159
<xsl:when test="$type = 'xs:anyAtomicType'">any</xsl:when>
115160
<xsl:when test="$type = ('xs:dateTime', 'xs:date')">Date</xsl:when>
116-
<xsl:when test="$type = ('Int', 'double', 'xs:numeric', 'numeric', 'xs:decimal', 'xs:double', 'xs:int', 'xs:float', 'xs:integer', 'xs:long', 'xs:nonNegativeInteger', 'xs:positiveInteger', 'xs:unsignedInt')">number</xsl:when>
117-
<xsl:when test="matches($type, '^(schema-)?[Ee]lement\([^)]+\)$')">MLNode&lt;any&gt;</xsl:when>
118-
<xsl:when test="matches($type, '^function\(')">
119-
<xsl:value-of select="local:fixFunctionType($type)"/>
161+
<xsl:when test="$type = ('Int', 'double', 'xs:numeric', 'numeric', 'xs:decimal', 'xs:double', 'xs:unsignedLong', 'xs:int', 'xs:float', 'xs:integer', 'xs:long', 'xs:nonNegativeInteger', 'xs:positiveInteger', 'xs:unsignedInt')">number</xsl:when>
162+
<xsl:when test="matches($type, '^(schema-)?[Ee]lement\([^)]+\)$')">
163+
<xsl:choose>
164+
<!-- TODO: Can we perform generics on this? -->
165+
<xsl:when test="$isForParameter">MLNodeOrObject&lt;any&gt;</xsl:when>
166+
<xsl:otherwise>MLNode&lt;any&gt;</xsl:otherwise>
167+
</xsl:choose>
120168
</xsl:when>
121169
<xsl:otherwise>
122170
<xsl:variable name="local-name" select="replace($type, '^([^:.]+[:.])?([^\(\)]+)\(?\)?$', '$2')"/>
@@ -133,25 +181,33 @@ let $definition := xdmp:xslt-eval(
133181
</xsl:choose>
134182
</xsl:otherwise>
135183
</xsl:choose>
136-
<xsl:if test="contains($typeUnion, '|')">
137-
<xsl:value-of select="concat('|', string-join(local:fixEachType(substring-after($typeUnion, '|')), ''))"/>
138-
</xsl:if>
139184
</xsl:function>
140185

141186
<xsl:function name="local:fixFunctionType">
142187
<xsl:param name="type"/>
188+
<xsl:param name="isArray"/>
143189
<xsl:text>(</xsl:text>
144-
<xsl:value-of select="local:fixParameters(replace($type, '^function\((.+)\)as(.+)?$', '$1'), 1)"/>
190+
<xsl:if test="matches($type, '^function\((.+)\)as(.+)$')">
191+
<xsl:value-of select="local:fixParameters(replace($type, '^function\((.+)\)as(.+)$', '$1'), 1)"/>
192+
</xsl:if>
145193
<xsl:text>)=&gt;</xsl:text>
146-
<xsl:value-of select="local:fixType(replace($type, '^function\((.+)\)as(.+)?$', '$2'))"/>
194+
<xsl:variable name="returnType" select="local:fixType(replace($type, '^function\(.*\)as(.+)$', '$1'), false())"/>
195+
<xsl:choose>
196+
<xsl:when test="$isArray">
197+
<xsl:value-of select="local:fixArrayType($returnType, false())"/>
198+
</xsl:when>
199+
<xsl:otherwise>
200+
<xsl:value-of select="$returnType"/>
201+
</xsl:otherwise>
202+
</xsl:choose>
147203
</xsl:function>
148204

149205
<xsl:function name="local:fixParameters">
150206
<xsl:param name="parameters"/>
151207
<xsl:param name="number"/>
152208
<xsl:value-of select="concat(concat('p', $number), ':')"/>
153209
<xsl:variable name="type" select="substring-before(concat($parameters,','),',')"/>
154-
<xsl:value-of select="local:fixType($type)"/>
210+
<xsl:value-of select="local:fixType($type, true())"/>
155211
<xsl:if test="contains($parameters, ',')">
156212
<xsl:value-of select="concat(',', string-join(local:fixParameters(substring-after($parameters, ','), $number + 1), ''))"/>
157213
</xsl:if>

ts/AccessorBuiltins.d.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)