forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
namespaceStringToId
and namespaceIdToString
methods to core
Now that saved objects' `namespaces` are exposed, we should provide a method to compare these strings to namespace IDs. The Spaces plugin already provided utility functions for this; I changed them to be a facade over the new core functions. The reason for this is that other plugins (alerting, actions) depend on the Spaces plugin and will use an `undefined` namespace if the Spaces plugin is not enabled.
- Loading branch information
Showing
17 changed files
with
206 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
docs/development/core/server/kibana-plugin-core-server.namespaceidtostring.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [namespaceIdToString](./kibana-plugin-core-server.namespaceidtostring.md) | ||
|
||
## namespaceIdToString variable | ||
|
||
Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with the exception of the `undefined` namespace ID (which has a namespace string of `'default'`<!-- -->). | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
namespaceIdToString: (namespace?: string | undefined) => string | ||
``` |
13 changes: 13 additions & 0 deletions
13
docs/development/core/server/kibana-plugin-core-server.namespacestringtoid.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [namespaceStringToId](./kibana-plugin-core-server.namespacestringtoid.md) | ||
|
||
## namespaceStringToId variable | ||
|
||
Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with the exception of the `'default'` namespace string (which has a namespace ID of `undefined`<!-- -->). | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
namespaceStringToId: (namespace: string) => string | undefined | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/core/server/saved_objects/service/lib/namespace.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { namespaceIdToString, namespaceStringToId } from './namespace'; | ||
|
||
describe('#namespaceIdToString', () => { | ||
it('converts `undefined` to default namespace string', () => { | ||
expect(namespaceIdToString(undefined)).toEqual('default'); | ||
}); | ||
|
||
it('leaves other namespace IDs as-is', () => { | ||
expect(namespaceIdToString('foo')).toEqual('foo'); | ||
}); | ||
|
||
it('throws an error when a namespace ID is an empty string', () => { | ||
expect(() => namespaceIdToString('')).toThrowError('namespace cannot be an empty string'); | ||
}); | ||
}); | ||
|
||
describe('#namespaceStringToId', () => { | ||
it('converts default namespace string to `undefined`', () => { | ||
expect(namespaceStringToId('default')).toBeUndefined(); | ||
}); | ||
|
||
it('leaves other namespace strings as-is', () => { | ||
expect(namespaceStringToId('foo')).toEqual('foo'); | ||
}); | ||
|
||
it('throws an error when a namespace string is falsy', () => { | ||
const test = (arg: any) => | ||
expect(() => namespaceStringToId(arg)).toThrowError('namespace must be a non-empty string'); | ||
|
||
test(undefined); | ||
test(null); | ||
test(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export const DEFAULT_NAMESPACE_STRING = 'default'; | ||
|
||
/** | ||
* @public | ||
* Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with | ||
* the exception of the `undefined` namespace ID (which has a namespace string of `'default'`). | ||
* | ||
* @param namespace The namespace ID, which must be either a non-empty string or `undefined`. | ||
*/ | ||
export const namespaceIdToString = (namespace?: string) => { | ||
if (namespace === '') { | ||
throw new TypeError('namespace cannot be an empty string'); | ||
} | ||
|
||
return namespace ?? DEFAULT_NAMESPACE_STRING; | ||
}; | ||
|
||
/** | ||
* @public | ||
* Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with | ||
* the exception of the `'default'` namespace string (which has a namespace ID of `undefined`). | ||
* | ||
* @param namespace The namespace string, which must be non-empty. | ||
*/ | ||
export const namespaceStringToId = (namespace: string) => { | ||
if (!namespace) { | ||
throw new TypeError('namespace must be a non-empty string'); | ||
} | ||
|
||
return namespace !== DEFAULT_NAMESPACE_STRING ? namespace : undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
const mockNamespaceIdToString = jest.fn(); | ||
const mockNamespaceStringToId = jest.fn(); | ||
jest.mock('../../../../../../../src/core/server', () => ({ | ||
namespaceIdToString: mockNamespaceIdToString, | ||
namespaceStringToId: mockNamespaceStringToId, | ||
})); | ||
|
||
export { mockNamespaceIdToString, mockNamespaceStringToId }; |
Oops, something went wrong.