Skip to content

Commit 96062d3

Browse files
authored
Added style guide (#89)
1 parent 825d8c6 commit 96062d3

File tree

1,459 files changed

+7462
-9175
lines changed

Some content is hidden

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

1,459 files changed

+7462
-9175
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Please make sure to follow the steps below when opening a pr:
77
88
- Tag the relative issue (if any) and give a brief explanation on what your changes are doing
99
- If you did a spec change, remember to generate again the outputs, you can do it by running `make generate-spec`
10+
- Make sure your code follows the lint rules, you can fix them by running `npm run lint:fix --prefix specification`
1011
- Sign the CLA https://www.elastic.co/contributor-agreement/
1112
1213
Happy coding!

.github/workflows/code-format.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Code style
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
13+
- name: Use Node.js 14.x
14+
uses: actions/setup-node@v1
15+
with:
16+
node-version: 14.x
17+
18+
- name: Install
19+
run: |
20+
npm install --prefix specification
21+
22+
- name: Code style check
23+
run: |
24+
npm run format:check --prefix specification
25+

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ validation-api-response: ## Validate response of Endpoint with param: api=<api-
2020
@echo ">> validating response of Endpoint: $(api)"
2121
./run-validations.sh --api $(api) --response
2222

23+
format-code: ## Format the spec according to the style guide
24+
@echo ">> validating formatting code"
25+
@npm run format:fix --prefix specification
26+
2327
generate-spec: ## Generate the output spec
2428
@echo ">> generating the spec .."
2529
@npm run compile:canonical-json

README.md

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -184,147 +184,6 @@ Namespaced APIs can be validated in the same way, for example:
184184
./run-validations.sh --api cat.health --request
185185
```
186186

187-
## Custom types
188-
189-
The goal of the specification is to be used by different languages, from dynamically typed to statically typed.
190-
To achieve this goal the specification contains a series of custom types that do not have a meaning
191-
for the target language, but they should be translated to the most approriate construct.
192-
193-
The specification is written in [TypeScript](https://www.typescriptlang.org/), you can find all
194-
the basic types [here](https://www.typescriptlang.org/docs/handbook/basic-types.html), while
195-
in [`docs/special-classes.md`](./docs/special-classes.md) you can find custom classes for
196-
defining set of APIs.
197-
198-
### Dictionary
199-
200-
Represents a dynamic key value map:
201-
202-
```ts
203-
property: Dictionary<string, TypeDefinition>
204-
```
205-
206-
For example:
207-
208-
```json
209-
{
210-
"property1": "type",
211-
"property2": "other-type",
212-
}
213-
```
214-
215-
### SingleKeyDictionary
216-
217-
Represents a dynamic key value map with a single top level key:
218-
219-
```ts
220-
property: SingleKeyDictionary<string, TypeDefinition>
221-
```
222-
223-
For example:
224-
225-
```json
226-
{
227-
"onlyKey": "type"
228-
}
229-
```
230-
231-
### Array
232-
233-
Represents an array of the given value:
234-
235-
```ts
236-
// generics syntax
237-
property: Array<string>
238-
239-
// short syntax
240-
property: string[]
241-
```
242-
243-
### Union
244-
245-
Represents a type that can accept multiple types:
246-
247-
```ts
248-
property: string | long
249-
```
250-
251-
It can be combined with other types:
252-
```ts
253-
// array
254-
property: Array<string | long>
255-
256-
// dictionary
257-
property: Dictionary<string, string | long>
258-
```
259-
260-
### Enum
261-
262-
Represents a set of allowed values:
263-
264-
```ts
265-
enum MyEnum {
266-
first = 0,
267-
second = 1,
268-
third = 2
269-
}
270-
271-
property: MyEnum
272-
```
273-
274-
### User defined value
275-
276-
Represents a value that will be defined by the user and has no specific type.
277-
278-
```ts
279-
property: UserDefinedValue
280-
```
281-
282-
### Numbers
283-
284-
The numeric type in TypeScript is `number`, but given that this specification target mutliple languages,
285-
it offers a bunch of alias that represents the type that should be used if the language supports it:
286-
287-
```ts
288-
type short = number
289-
type byte = number
290-
type integer = number
291-
type long = number
292-
type float = number
293-
type double = number
294-
```
295-
296-
### Strings
297-
298-
The string type in TypeScript is `string`. It's ok to use it in the spec, but to offer a more developer
299-
friendly specification, we do offer a set of aliases based on which string we do expect, for example:
300-
301-
```ts
302-
type ScrollId = string
303-
type ScrollIds = string
304-
type CategoryId = string
305-
type ActionIds = string
306-
type Field = string
307-
type Id = string | number
308-
type Ids = string | number | string[]
309-
type IndexName = string
310-
type Indices = string | string[]
311-
...
312-
```
313-
314-
You can find the full list [here](https://github.com/elastic/elastic-client-generator/blob/update-docs/specification/specs/common.ts),
315-
feel free to add more if it feels appropriate!
316-
317-
### Dates
318-
319-
The `Date` type in TypeScript refers to the JavaScript `Date` object,
320-
since Elasticsearch needs a string or a numeric value, there are aliases also for date types:
321-
322-
```ts
323-
type Timestamp = string
324-
type TimeSpan = string
325-
interface Date {}
326-
```
327-
328187
## FAQ
329188

330189
### A specific property is not always present, how do I define it?

docs/behaviors.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Behaviors
2+
3+
Some APIs needs to be handled differenlty based on the output language, while others share many common parameters.
4+
This document contains the list of this special interfaces and where those should be used.
5+
Behaviors should be used via `implements` in the specification.
6+
7+
You can find all the special classes and aliases in in the [modeling guide](./modeling-guide.md).
8+
9+
## AdditionalProperties
10+
11+
In some places in the specification an object consists of the union of a set of known properties
12+
and a set of runtime injected properties. Meaning that object should theoretically extend Dictionary but expose
13+
a set of known keys and possibly. The object might already be part of an object graph and have a parent class.
14+
This puts it into a bind that needs a client specific solution.
15+
We therefore document the requirement to behave like a dictionary for unknown properties with this interface.
16+
17+
```ts
18+
class IpRangeBucket implements AdditionalProperties<AggregateName, Aggregate> {}
19+
```
20+
21+
## ArrayResponse
22+
23+
A response formatted as an array of records.
24+
Some languages can't represent this easily and need to wrap the
25+
array inside an object.
26+
27+
```ts
28+
class CatAliasesResponse
29+
extends ResponseBase
30+
implements ArrayResponse<CatAliasesRecord> {}
31+
```
32+
33+
## EmptyResponseBase
34+
35+
HEAD APIs can have a different behavior based on the language,
36+
the response body is always empty to it's up to language generators
37+
to define how those should be represented.
38+
39+
```ts
40+
class DocumentExistsResponse
41+
extends ResponseBase
42+
implements EmptyResponseBase {}
43+
```

docs/modeling-guide.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Modeling Guide
2+
3+
The goal of the specification is to be used by different languages, from dynamically typed to statically typed.
4+
To achieve this goal the specification contains a series of custom types that do not have a meaning
5+
for the target language, but they should be translated to the most approriate construct.
6+
7+
The specification is written in [TypeScript](https://www.typescriptlang.org/), you can find all
8+
the basic types [here](https://www.typescriptlang.org/docs/handbook/basic-types.html),
9+
while in [behaviors](./behaviors.md) you can find the list of special interfaces used
10+
for describing APIs that can't be represented in the specification.
11+
12+
### Dictionary
13+
14+
Represents a dynamic key value map:
15+
16+
```ts
17+
property: Dictionary<string, TypeDefinition>
18+
```
19+
20+
For example:
21+
22+
```json
23+
{
24+
"property1": "type",
25+
"property2": "other-type",
26+
}
27+
```
28+
29+
### SingleKeyDictionary
30+
31+
Represents a dynamic key value map with a single top level key:
32+
33+
```ts
34+
property: SingleKeyDictionary<string, TypeDefinition>
35+
```
36+
37+
For example:
38+
39+
```json
40+
{
41+
"onlyKey": "type"
42+
}
43+
```
44+
45+
### Array
46+
47+
Represents an array of the given value:
48+
49+
```ts
50+
// generics syntax
51+
property: Array<string>
52+
53+
// short syntax
54+
property: string[]
55+
```
56+
57+
### Union
58+
59+
Represents a type that can accept multiple types:
60+
61+
```ts
62+
property: string | long
63+
```
64+
65+
It can be combined with other types:
66+
```ts
67+
// array
68+
property: Array<string | long>
69+
70+
// dictionary
71+
property: Dictionary<string, string | long>
72+
```
73+
74+
### Enum
75+
76+
Represents a set of allowed values:
77+
78+
```ts
79+
enum MyEnum {
80+
first = 0,
81+
second = 1,
82+
third = 2
83+
}
84+
85+
property: MyEnum
86+
```
87+
88+
### User defined value
89+
90+
Represents a value that will be defined by the user and has no specific type.
91+
92+
```ts
93+
property: UserDefinedValue
94+
```
95+
96+
### Numbers
97+
98+
The numeric type in TypeScript is `number`, but given that this specification target mutliple languages,
99+
it offers a bunch of alias that represents the type that should be used if the language supports it:
100+
101+
```ts
102+
type short = number
103+
type byte = number
104+
type integer = number
105+
type long = number
106+
type float = number
107+
type double = number
108+
```
109+
110+
### Strings
111+
112+
The string type in TypeScript is `string`. It's ok to use it in the spec, but to offer a more developer
113+
friendly specification, we do offer a set of aliases based on which string we do expect, for example:
114+
115+
```ts
116+
type ScrollId = string
117+
type ScrollIds = string
118+
type CategoryId = string
119+
type ActionIds = string
120+
type Field = string
121+
type Id = string | number
122+
type Ids = string | number | string[]
123+
type IndexName = string
124+
type Indices = string | string[]
125+
...
126+
```
127+
128+
You can find the full list [here](https://github.com/elastic/elastic-client-generator/blob/update-docs/specification/specs/common.ts),
129+
feel free to add more if it feels appropriate!
130+
131+
### Dates
132+
133+
The `Date` type in TypeScript refers to the JavaScript `Date` object,
134+
since Elasticsearch needs a string or a numeric value, there are aliases also for date types:
135+
136+
```ts
137+
type Timestamp = string
138+
type TimeSpan = string
139+
interface Date {}
140+
```

0 commit comments

Comments
 (0)