Skip to content

Commit

Permalink
Wrap namespace utility methods in SavedObjectsUtils class
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Sep 4, 2020
1 parent ba8c72d commit 2c36a31
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 138 deletions.
3 changes: 1 addition & 2 deletions docs/development/core/server/kibana-plugin-core-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [SavedObjectsErrorHelpers](./kibana-plugin-core-server.savedobjectserrorhelpers.md) | |
| [SavedObjectsRepository](./kibana-plugin-core-server.savedobjectsrepository.md) | |
| [SavedObjectsSerializer](./kibana-plugin-core-server.savedobjectsserializer.md) | A serializer that can be used to manually convert [raw](./kibana-plugin-core-server.savedobjectsrawdoc.md) or [sanitized](./kibana-plugin-core-server.savedobjectsanitizeddoc.md) documents to the other kind. |
| [SavedObjectsUtils](./kibana-plugin-core-server.savedobjectsutils.md) | |
| [SavedObjectTypeRegistry](./kibana-plugin-core-server.savedobjecttyperegistry.md) | Registry holding information about all the registered [saved object types](./kibana-plugin-core-server.savedobjectstype.md)<!-- -->. |

## Enumerations
Expand Down Expand Up @@ -224,8 +225,6 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| Variable | Description |
| --- | --- |
| [kibanaResponseFactory](./kibana-plugin-core-server.kibanaresponsefactory.md) | Set of helpers used to create <code>KibanaResponse</code> to form HTTP response on an incoming request. Should be returned as a result of [RequestHandler](./kibana-plugin-core-server.requesthandler.md) execution. |
| [namespaceIdToString](./kibana-plugin-core-server.namespaceidtostring.md) | Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with the exception of the <code>undefined</code> namespace ID (which has a namespace string of <code>'default'</code>). |
| [namespaceStringToId](./kibana-plugin-core-server.namespacestringtoid.md) | Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with the exception of the <code>'default'</code> namespace string (which has a namespace ID of <code>undefined</code>). |
| [ServiceStatusLevels](./kibana-plugin-core-server.servicestatuslevels.md) | The current "level" of availability of a service. |
| [validBodyOutput](./kibana-plugin-core-server.validbodyoutput.md) | The set of valid body.output |

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectsUtils](./kibana-plugin-core-server.savedobjectsutils.md)

## SavedObjectsUtils class


<b>Signature:</b>

```typescript
export declare class SavedObjectsUtils
```

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [namespaceIdToString](./kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md) | <code>static</code> | <code>(namespace?: string &#124; undefined) =&gt; string</code> | Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with the exception of the <code>undefined</code> namespace ID (which has a namespace string of <code>'default'</code>). |
| [namespaceStringToId](./kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md) | <code>static</code> | <code>(namespace: string) =&gt; string &#124; undefined</code> | Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with the exception of the <code>'default'</code> namespace string (which has a namespace ID of <code>undefined</code>). |

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [namespaceIdToString](./kibana-plugin-core-server.namespaceidtostring.md)
[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectsUtils](./kibana-plugin-core-server.savedobjectsutils.md) &gt; [namespaceIdToString](./kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md)

## namespaceIdToString variable
## SavedObjectsUtils.namespaceIdToString property

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
static namespaceIdToString: (namespace?: string | undefined) => string;
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [namespaceStringToId](./kibana-plugin-core-server.namespacestringtoid.md)
[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectsUtils](./kibana-plugin-core-server.savedobjectsutils.md) &gt; [namespaceStringToId](./kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md)

## namespaceStringToId variable
## SavedObjectsUtils.namespaceStringToId property

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
static namespaceStringToId: (namespace: string) => string | undefined;
```
3 changes: 1 addition & 2 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,10 @@ export {
SavedObjectsTypeManagementDefinition,
SavedObjectMigrationMap,
SavedObjectMigrationFn,
SavedObjectsUtils,
exportSavedObjectsToStream,
importSavedObjectsFromStream,
resolveSavedObjectsImportErrors,
namespaceIdToString,
namespaceStringToId,
} from './saved_objects';

export {
Expand Down
3 changes: 1 addition & 2 deletions src/core/server/saved_objects/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ export {
SavedObjectsErrorHelpers,
SavedObjectsClientFactory,
SavedObjectsClientFactoryProvider,
namespaceIdToString,
namespaceStringToId,
SavedObjectsUtils,
} from './lib';

export * from './saved_objects_client';
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/service/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export {

export { SavedObjectsErrorHelpers } from './errors';

export { namespaceIdToString, namespaceStringToId } from './namespace';
export { SavedObjectsUtils } from './utils';
53 changes: 0 additions & 53 deletions src/core/server/saved_objects/service/lib/namespace.test.ts

This file was deleted.

48 changes: 0 additions & 48 deletions src/core/server/saved_objects/service/lib/namespace.ts

This file was deleted.

32 changes: 20 additions & 12 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import {
} from '../../types';
import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry';
import { validateConvertFilterToKueryNode } from './filter_utils';
import { namespaceIdToString, namespaceStringToId } from './namespace';
import { SavedObjectsUtils } from './utils';

// BEWARE: The SavedObjectClient depends on the implementation details of the SavedObjectsRepository
// so any breaking changes to this repository are considered breaking changes to the SavedObjectsClient.
Expand Down Expand Up @@ -563,7 +563,7 @@ export class SavedObjectsRepository {
preflightResult = await this.preflightCheckIncludesNamespace(type, id, namespace);
const existingNamespaces = getSavedObjectNamespaces(undefined, preflightResult);
const remainingNamespaces = existingNamespaces?.filter(
(x) => x !== namespaceIdToString(namespace)
(x) => x !== SavedObjectsUtils.namespaceIdToString(namespace)
);

if (remainingNamespaces?.length) {
Expand Down Expand Up @@ -887,7 +887,9 @@ export class SavedObjectsRepository {
const { originId, updated_at: updatedAt } = doc._source;
let namespaces = [];
if (!this._registry.isNamespaceAgnostic(type)) {
namespaces = doc._source.namespaces ?? [namespaceIdToString(doc._source.namespace)];
namespaces = doc._source.namespaces ?? [
SavedObjectsUtils.namespaceIdToString(doc._source.namespace),
];
}

return {
Expand Down Expand Up @@ -944,7 +946,9 @@ export class SavedObjectsRepository {

let namespaces: string[] = [];
if (!this._registry.isNamespaceAgnostic(type)) {
namespaces = body._source.namespaces ?? [namespaceIdToString(body._source.namespace)];
namespaces = body._source.namespaces ?? [
SavedObjectsUtils.namespaceIdToString(body._source.namespace),
];
}

return {
Expand Down Expand Up @@ -1020,7 +1024,9 @@ export class SavedObjectsRepository {
const { originId } = body.get._source;
let namespaces = [];
if (!this._registry.isNamespaceAgnostic(type)) {
namespaces = body.get._source.namespaces ?? [namespaceIdToString(body.get._source.namespace)];
namespaces = body.get._source.namespaces ?? [
SavedObjectsUtils.namespaceIdToString(body.get._source.namespace),
];
}

return {
Expand Down Expand Up @@ -1257,9 +1263,11 @@ export class SavedObjectsRepository {
});

const getNamespaceId = (objectNamespace?: string) =>
objectNamespace !== undefined ? namespaceStringToId(objectNamespace) : namespace;
objectNamespace !== undefined
? SavedObjectsUtils.namespaceStringToId(objectNamespace)
: namespace;
const getNamespaceString = (objectNamespace?: string) =>
objectNamespace ?? namespaceIdToString(namespace);
objectNamespace ?? SavedObjectsUtils.namespaceIdToString(namespace);

const bulkGetDocs = expectedBulkGetResults
.filter(isRight)
Expand Down Expand Up @@ -1319,7 +1327,7 @@ export class SavedObjectsRepository {
};
}
namespaces = actualResult._source.namespaces ?? [
namespaceIdToString(actualResult._source.namespace),
SavedObjectsUtils.namespaceIdToString(actualResult._source.namespace),
];
versionProperties = getExpectedVersionProperties(version, actualResult);
} else {
Expand Down Expand Up @@ -1522,7 +1530,7 @@ export class SavedObjectsRepository {
const savedObject = this._serializer.rawToSavedObject(raw);
const { namespace, type } = savedObject;
if (this._registry.isSingleNamespace(type)) {
savedObject.namespaces = [namespaceIdToString(namespace)];
savedObject.namespaces = [SavedObjectsUtils.namespaceIdToString(namespace)];
}
return omit(savedObject, 'namespace') as SavedObject<T>;
}
Expand All @@ -1545,7 +1553,7 @@ export class SavedObjectsRepository {
}

const namespaces = raw._source.namespaces;
return namespaces?.includes(namespaceIdToString(namespace)) ?? false;
return namespaces?.includes(SavedObjectsUtils.namespaceIdToString(namespace)) ?? false;
}

/**
Expand Down Expand Up @@ -1665,15 +1673,15 @@ function getSavedObjectNamespaces(
if (document) {
return document._source?.namespaces;
}
return [namespaceIdToString(namespace)];
return [SavedObjectsUtils.namespaceIdToString(namespace)];
}

/**
* Ensure that a namespace is always in its namespace ID representation.
* This allows `'default'` to be used interchangeably with `undefined`.
*/
const normalizeNamespace = (namespace?: string) =>
namespace === undefined ? namespace : namespaceStringToId(namespace);
namespace === undefined ? namespace : SavedObjectsUtils.namespaceStringToId(namespace);

/**
* Extracts the contents of a decorated error to return the attributes for bulk operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { esKuery, KueryNode } from '../../../../../../plugins/data/server';

import { getRootPropertiesObjects, IndexMapping } from '../../../mappings';
import { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry';
import { DEFAULT_NAMESPACE_STRING } from '../namespace';
import { DEFAULT_NAMESPACE_STRING } from '../utils';

/**
* Gets the types based on the type. Uses mappings to support
Expand Down
57 changes: 57 additions & 0 deletions src/core/server/saved_objects/service/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 { SavedObjectsUtils } from './utils';

describe('#SavedObjectsUtils', () => {
const { namespaceIdToString, namespaceStringToId } = SavedObjectsUtils;

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('');
});
});
});
Loading

0 comments on commit 2c36a31

Please sign in to comment.