Skip to content

Commit

Permalink
Support !reset and !override tags for compose
Browse files Browse the repository at this point in the history
  • Loading branch information
bwateratmsft committed Dec 12, 2024
1 parent ff78035 commit 2c0aaf7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/service/ComposeDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { ErrorCodes, Position, Range, ResponseError, TextDocumentIdentifier, TextDocumentsConfiguration } from 'vscode-languageserver';
import { DocumentUri, TextDocument } from 'vscode-languageserver-textdocument';
import { Document as YamlDocument, isDocument, Node as YamlNode, parseDocument } from 'yaml';
import { Document as YamlDocument, isDocument, Node as YamlNode, parseDocument, ScalarTag, Tags } from 'yaml';
import { CRLF, DocumentSettings, DocumentSettingsParams, DocumentSettingsRequest, LF } from '../client/DocumentSettings';
import { ExtendedPositionParams, PositionInfo } from './ExtendedParams';
import { getCurrentContext } from './utils/ActionContext';
Expand Down Expand Up @@ -152,7 +152,7 @@ export class ComposeDocument {
};

private buildYamlDocument(): YamlDocument<YamlNode> {
const yamlDocument = parseDocument(this.textDocument.getText(), { merge: true, prettyErrors: true });
const yamlDocument = parseDocument(this.textDocument.getText(), { merge: true, prettyErrors: true, customTags: ComposeCustomTags });

if (!isDocument(yamlDocument)) {
throw new ResponseError(ErrorCodes.ParseError, 'Malformed YAML document');
Expand Down Expand Up @@ -346,3 +346,13 @@ const Value = '<value>';
const Item = '<item>';
const Sep = '<sep>';
const Comment = '<comment>';

// Custom YAML tags that Compose supports
const ComposeCustomTags: Tags = [
{ tag: '!reset', collection: 'seq', resolve: v => v },
{ tag: '!reset', collection: 'map', resolve: v => v },
{ tag: '!reset', resolve: v => v} satisfies ScalarTag,
{ tag: '!override', collection: 'seq', resolve: v => v },
{ tag: '!override', collection: 'map', resolve: v => v },
{ tag: '!override', resolve: v => v} satisfies ScalarTag,
];
40 changes: 40 additions & 0 deletions src/test/providers/DiagnosticProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,46 @@ services:
});
});

describe('Custom Tags', async () => {
it('Should provide nothing for valid compose documents with known custom tags', async () => {
const validComposeWithTags = `version: '123'
services:
foo:
image: !reset bar
ports: !override
- 1234
environment: !override
foo: bar
`;

const expected = undefined;
await awaitDiagnosticsAndCompare(testConnection, validComposeWithTags, expected);
});

it('Should provide something for valid compose documents with unknown custom tags', async () => {
const validComposeWithUnknownTags = `version: '123'
services:
foo:
image: bar
ports:
- 1234
environment: !unknowntag
foo: bar
`;

const expected: ExpectedDiagnostic[] = [
{
range: Range.create(7, 17, 7, 28),
contentCanary: 'Unresolved tag',
}
];

await awaitDiagnosticsAndCompare(testConnection, validComposeWithUnknownTags, expected);
});
});

after('Cleanup', () => {
testConnection.dispose();
noDiagnosticsTestConnection.dispose();
Expand Down

0 comments on commit 2c0aaf7

Please sign in to comment.