Skip to content

Add Binary data type #703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ export function modelType (node: Node): model.ValueOf {
return type
}

case 'ArrayBuffer': {
const type: model.InstanceOf = {
kind: 'instance_of',
type: {
name: 'binary',
namespace: 'internal'
}
}
return type
}

case 'Dictionary':
case 'AdditionalProperties': {
assert(node, node.getTypeArguments().length === 2, 'A Dictionary must have two arguments')
Expand Down
32 changes: 32 additions & 0 deletions docs/modeling-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,38 @@ type TimeSpan = string
type DateString = string
```

### Binary

Some APIs return a Binary stream of data instead of JSON.
Create an alias of the `ArrayBuffer` type for the appropriate name.

```ts
export type MapboxVectorTiles = ArrayBuffer

export class Response {
body: MapboxVectorTiles
}
```

In the output schema.json `MapboxVectorTiles` will be defined as:

```json
{
"kind": "type_alias",
"name": {
"name": "MapboxVectorTiles",
"namespace": "_types"
},
"type": {
"kind": "instance_of",
"type": {
"name": "binary",
"namespace": "internal"
}
}
}
```

### Literal values

The compiler supports literal values as well. This can be useful if a
Expand Down
20 changes: 19 additions & 1 deletion output/schema/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions output/schema/validation-errors.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion output/typescript/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions specification/_global/search_mvt/SearchMvtResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import { UserDefinedValue } from '@spec_utils/UserDefinedValue'
import { MapboxVectorTiles } from '@_types/Binary'

export class Response {
body: UserDefinedValue
body: MapboxVectorTiles
}
21 changes: 21 additions & 0 deletions specification/_types/Binary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// Vector tile response
export type MapboxVectorTiles = ArrayBuffer
4 changes: 4 additions & 0 deletions typescript-generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ function buildValue (type: M.ValueOf, openGenerics?: string[], origin?: M.TypeNa
}
}

if (type.type.name === 'binary' && type.type.namespace === 'internal') {
return 'ArrayBuffer'
}

return `${createName(type.type)}${buildGenerics(type.generics, openGenerics)}`
case 'array_of':
return type.value.kind === 'union_of' || getShortcutType(type.value) != null
Expand Down