Skip to content

Commit 5530e97

Browse files
committed
Modified schema modification to be inside of yamlSchemaService
1 parent 2b96210 commit 5530e97

7 files changed

Lines changed: 98 additions & 136 deletions

File tree

src/languageservice/apis/schemaModification.ts

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

src/languageservice/services/yamlSchemaService.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ const localize = nls.loadMessageBundle();
1616

1717
export declare type CustomSchemaProvider = (uri: string) => Thenable<string>;
1818

19+
export enum MODIFICATION_ACTIONS {
20+
'delete',
21+
'add'
22+
}
23+
24+
export interface SchemaAdditions {
25+
schema: string,
26+
action: MODIFICATION_ACTIONS.add,
27+
path: string,
28+
key: string,
29+
// tslint:disable-next-line: no-any
30+
content: any
31+
}
32+
33+
export interface SchemaDeletions {
34+
schema: string,
35+
action: MODIFICATION_ACTIONS.delete,
36+
path: string,
37+
key: string
38+
}
39+
1940
export class FilePatternAssociation {
2041

2142
private schemas: string[];
@@ -238,6 +259,7 @@ export class YAMLSchemaService extends JSONSchemaService {
238259
return resolveSchema();
239260
}
240261
}
262+
241263
/**
242264
* Save a schema with schema ID and schema content.
243265
* Overrides previous schemas set for that schema ID.
@@ -259,6 +281,68 @@ export class YAMLSchemaService extends JSONSchemaService {
259281
return Promise.resolve(undefined);
260282
}
261283

284+
/**
285+
* Add content to a specified schema at a specified path
286+
*/
287+
public async addContent(additions: SchemaAdditions) {
288+
const schema = await this.getResolvedSchema(additions.schema);
289+
if (schema) {
290+
const resolvedSchemaLocation = this.resolveJSONSchemaToSection(schema.schema, additions.path);
291+
292+
if (typeof resolvedSchemaLocation === 'object') {
293+
resolvedSchemaLocation[additions.key] = additions.content;
294+
}
295+
await this.saveSchema(additions.schema, schema.schema);
296+
}
297+
}
298+
299+
/**
300+
* Delete content in a specified schema at a specified path
301+
*/
302+
public async deleteContent(deletions: SchemaDeletions) {
303+
const schema = await this.getResolvedSchema(deletions.schema);
304+
if (schema) {
305+
const resolvedSchemaLocation = this.resolveJSONSchemaToSection(schema.schema, deletions.path);
306+
307+
if (typeof resolvedSchemaLocation === 'object') {
308+
delete resolvedSchemaLocation[deletions.key];
309+
}
310+
await this.saveSchema(deletions.schema, schema.schema);
311+
}
312+
}
313+
314+
/**
315+
* Take a JSON Schema and the path that you would like to get to
316+
* @returns the JSON Schema resolved at that specific path
317+
*/
318+
private resolveJSONSchemaToSection(schema: JSONSchema, paths: string): JSONSchema {
319+
const splitPathway = paths.split('/');
320+
let resolvedSchemaLocation = schema;
321+
for (const path of splitPathway) {
322+
if (path === '') {
323+
continue;
324+
}
325+
this.resolveNext(resolvedSchemaLocation, path);
326+
resolvedSchemaLocation = resolvedSchemaLocation[path];
327+
}
328+
return resolvedSchemaLocation;
329+
}
330+
331+
/**
332+
* Resolve the next Object if they have compatible types
333+
* @param object a location in the JSON Schema
334+
* @param token the next token that you want to search for
335+
*/
336+
// tslint:disable-next-line: no-any
337+
private resolveNext(object: any, token: any) {
338+
// tslint:disable-next-line: no-any
339+
if (Array.isArray(object) && isNaN(token)) {
340+
throw new Error('Expected a number after the array object');
341+
} else if (typeof object === 'object' && typeof token !== 'string') {
342+
throw new Error('Expected a string after the object');
343+
}
344+
}
345+
262346
/**
263347
* Everything below here is needed because we're importing from vscode-json-languageservice umd and we need
264348
* to provide a wrapper around the javascript methods we are calling since they have no type

src/languageservice/utils/kubernetesResolver.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const KUBERNETES_SCHEMA_URL = 'https://raw.githubusercontent.com/instrumenta/kubernetes-json-schema/master/v1.14.0-standalone-strict/all.json';
2+
export const JSON_SCHEMASTORE_URL = 'http://schemastore.org/api/json/catalog.json';

src/languageservice/yamlLanguageService.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66

7-
import { YAMLSchemaService, CustomSchemaProvider } from './services/yamlSchemaService';
7+
import { YAMLSchemaService, CustomSchemaProvider, SchemaAdditions, SchemaDeletions } from './services/yamlSchemaService';
88
import { TextDocument, Position, CompletionList, Diagnostic, Hover, SymbolInformation, DocumentSymbol, CompletionItem, TextEdit } from 'vscode-languageserver-types';
99
import { JSONSchema } from './jsonSchema07';
1010
import { YAMLDocumentSymbols } from './services/documentSymbols';
@@ -13,7 +13,6 @@ import { YAMLHover } from './services/yamlHover';
1313
import { YAMLValidation } from './services/yamlValidation';
1414
import { YAMLFormatter } from './services/yamlFormatter';
1515
import { getLanguageService as getJSONLanguageService, JSONWorkerContribution } from 'vscode-json-languageservice';
16-
import { SchemaModification, SchemaAdditions, SchemaDeletions } from './apis/schemaModification';
1716

1817
export interface LanguageSettings {
1918
validate?: boolean; //Setting for whether we want to validate the schema
@@ -137,7 +136,6 @@ export function getLanguageService(schemaRequestService: SchemaRequestService,
137136
const yamlDocumentSymbols = new YAMLDocumentSymbols(schemaService);
138137
const yamlValidation = new YAMLValidation(schemaService, promise);
139138
const formatter = new YAMLFormatter();
140-
const schemaModifier = new SchemaModification();
141139

142140
return {
143141
configure: settings => {
@@ -166,7 +164,7 @@ export function getLanguageService(schemaRequestService: SchemaRequestService,
166164
doFormat: formatter.format.bind(formatter),
167165
addSchema: (schemaID: string, schema: JSONSchema) => schemaService.saveSchema(schemaID, schema),
168166
deleteSchema: (schemaID: string) => schemaService.deleteSchema(schemaID),
169-
modifySchemaContent: (schemaAdditions: SchemaAdditions) => schemaModifier.addContent(schemaService, schemaAdditions),
170-
deleteSchemaContent: (schemaDeletions: SchemaDeletions) => schemaModifier.deleteContent(schemaService, schemaDeletions)
167+
modifySchemaContent: (schemaAdditions: SchemaAdditions) => schemaService.addContent(schemaAdditions),
168+
deleteSchemaContent: (schemaDeletions: SchemaDeletions) => schemaService.deleteContent(schemaDeletions)
171169
};
172170
}

src/server.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { SchemaAssociationNotification, DynamicCustomSchemaRequestRegistration,
2222
import { schemaRequestHandler } from './languageservice/services/schemaRequestHandler';
2323
import { isRelativePath, relativeToAbsolutePath } from './languageservice/utils/paths';
2424
import { URI } from 'vscode-uri';
25-
import { resolveURL } from './languageservice/utils/kubernetesResolver';
25+
import { KUBERNETES_SCHEMA_URL, JSON_SCHEMASTORE_URL } from './languageservice/utils/schemaUrls';
2626
// tslint:disable-next-line: no-any
2727
nls.config(process.env['VSCODE_NLS_CONFIG'] as any);
2828

@@ -236,23 +236,9 @@ function updateConfiguration() {
236236
* @param schema schema id
237237
* @param languageSettings current server settings
238238
*/
239-
function configureSchemas(uri, fileMatch, schema, languageSettings){
239+
function configureSchemas(uri: string, fileMatch: string[], schema: any, languageSettings: LanguageSettings) {
240240

241-
uri = resolveURL(uri);
242-
243-
if (schema === null){
244-
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
245-
}else {
246-
languageSettings.schemas.push({ uri, fileMatch: fileMatch, schema: schema });
247-
}
248-
249-
if (fileMatch.constructor === Array && uri === KUBERNETES_SCHEMA_URL){
250-
fileMatch.forEach(url => {
251-
specificValidatorPaths.push(url);
252-
});
253-
}else if (uri === KUBERNETES_SCHEMA_URL) {
254-
specificValidatorPaths.push(fileMatch);
255-
}
241+
uri = checkSchemaURI(uri);
256242

257243
if (schema === null) {
258244
languageSettings.schemas.push({ uri, fileMatch: fileMatch });

test/schema.test.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import * as JsonSchema from '../src/languageservice/jsonSchema07';
66
import fs = require('fs');
77
import url = require('url');
88
import path = require('path');
9-
import { SchemaModification, MODIFICATION_ACTIONS, SchemaDeletions } from '../src/languageservice/apis/schemaModification';
10-
import { KUBERNETES_SCHEMA_URL } from '../src/languageservice/utils/kubernetesResolver';
119
import { XHRResponse, xhr } from 'request-light';
10+
import { MODIFICATION_ACTIONS, SchemaDeletions } from '../src/languageservice/services/yamlSchemaService';
11+
import { KUBERNETES_SCHEMA_URL } from '../src/languageservice/utils/schemaUrls';
1212

1313
const fixtureDocuments = {
1414
'http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json': 'deploymentTemplate.json',
@@ -354,8 +354,7 @@ suite('JSON Schema', () => {
354354
}
355355
});
356356

357-
const schemaModification = new SchemaModification();
358-
await schemaModification.addContent(service, {
357+
await service.addContent({
359358
action: MODIFICATION_ACTIONS.add,
360359
path: 'properties/apiVersion',
361360
key: 'enum',
@@ -401,8 +400,7 @@ suite('JSON Schema', () => {
401400
}
402401
});
403402

404-
const schemaModification = new SchemaModification();
405-
await schemaModification.deleteContent(service, {
403+
await service.deleteContent({
406404
action: MODIFICATION_ACTIONS.delete,
407405
path: 'properties',
408406
key: 'apiVersion',
@@ -425,8 +423,7 @@ suite('JSON Schema', () => {
425423
const service = new SchemaService.YAMLSchemaService(schemaRequestServiceForURL, workspaceContext);
426424
service.registerExternalSchema(KUBERNETES_SCHEMA_URL);
427425

428-
const schemaModification = new SchemaModification();
429-
await schemaModification.addContent(service, {
426+
await service.addContent({
430427
action: MODIFICATION_ACTIONS.add,
431428
path: 'oneOf/0/properties/kind',
432429
key: 'enum',
@@ -445,8 +442,7 @@ suite('JSON Schema', () => {
445442
const service = new SchemaService.YAMLSchemaService(schemaRequestServiceForURL, workspaceContext);
446443
service.registerExternalSchema(KUBERNETES_SCHEMA_URL);
447444

448-
const schemaModification = new SchemaModification();
449-
await schemaModification.deleteContent(service, {
445+
await service.deleteContent({
450446
action: MODIFICATION_ACTIONS.delete,
451447
path: 'oneOf/0/properties/kind',
452448
key: 'enum',

0 commit comments

Comments
 (0)