Skip to content

Commit

Permalink
Merge pull request #27 from superfaceai/feat/document-id-interfaces
Browse files Browse the repository at this point in the history
Feat/document id interfaces
  • Loading branch information
TheEdward162 authored Jan 20, 2021
2 parents 1523241 + a60ec65 commit 20ed549
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 381 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
## [Unreleased]

### Added
* Public `parseDocumentId` for partial parsing

### Changed
* Interfaces for map and profile id
* Improved document id version parsing
* Rename `isLowercaseIdentifier` to `isValidDocumentIdentifier`

### Fixed
* Document id `parseProfileId` not returning version label

## [0.0.11] - 2021-01-19

### Added
Expand Down Expand Up @@ -100,7 +111,7 @@
* Documentation extraction from doc strings
* Usecase result parsing as optional

[Unreleased]: https://github.com/superfaceai/parser/compare/v0.0.10...HEAD
[Unreleased]: https://github.com/superfaceai/parser/compare/v0.0.11...HEAD
[0.0.11]: https://github.com/superfaceai/parser/compare/v0.0.10...v0.0.11
[0.0.10]: https://github.com/superfaceai/parser/compare/v0.0.9...v0.0.10
[0.0.9]: https://github.com/superfaceai/parser/compare/v0.0.8...v0.0.9
Expand Down
45 changes: 45 additions & 0 deletions src/common/document/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Structure representing semver version tag.
*/
export type DocumentVersion = {
major: number;
minor?: number;
/** Patch version cannot appear without minor version. */
patch?: number;
/** Label can appear even without major and minor version. */
label?: string;
};
/**
* Generic structure that covers both partial profile and partial map ids.
*/
export type DocumentId = {
/** The optional leading scope before `/`. */
scope?: string;
/** The middle portion between of the `[<scope>/]middle[@<version>]`, split by `.`. */
middle: string[];
/** The optional trailing version after `@`. */
version?: DocumentVersion;
};

/** Information encoded in the profile id string. */
export type ProfileDocumentId = {
/** Scope of the profile, if any. */
scope?: string;
/** Name of the profile. */
name: string;
/** Version of the profile. */
version: DocumentVersion;
};

/** Information encoded in the map id string. */
export type MapDocumentId = {
scope?: string;
name: string;
provider: string;
variant?: string;
version: {
major: number;
minor?: number;
revision?: number;
};
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import { parseMapId, parseProfileId, parseVersion } from './document_id';
import {
parseDocumentId,
parseMapId,
parseProfileId,
parseVersion,
} from './parser';

describe('document name parsing', () => {
it('parses minimal profile name', () => {
it('parses partial profile name', () => {
const id = 'my-profile';

expect(parseDocumentId(id)).toEqual({
kind: 'parsed',
value: {
middle: ['my-profile'],
},
});
});

it('parses minimal profile name', () => {
const id = 'my_profile@17';

expect(parseProfileId(id)).toEqual({
kind: 'parsed',
name: 'my-profile',
value: {
name: 'my_profile',
version: {
major: 17,
},
},
});
});

Expand All @@ -15,47 +36,70 @@ describe('document name parsing', () => {

expect(parseProfileId(id)).toEqual({
kind: 'parsed',
scope: 'my-sc0pe',
name: 'my-profile',
version: {
major: 1,
minor: 2,
patch: 34,
value: {
scope: 'my-sc0pe',
name: 'my-profile',
version: {
major: 1,
minor: 2,
patch: 34,
},
},
});
});

it('parses minimal map name', () => {
it('parses partial map name', () => {
const id = 'my-profile.x_prov1der';

expect(parseDocumentId(id)).toEqual({
kind: 'parsed',
value: {
middle: ['my-profile', 'x_prov1der'],
},
});
});

it('parses minimal map name', () => {
const id = 'prof.prov@32';

expect(parseMapId(id)).toEqual({
kind: 'parsed',
name: 'my-profile',
provider: 'x_prov1der',
value: {
name: 'prof',
provider: 'prov',
version: {
major: 32,
},
},
});
});

it('parses full map name', () => {
const id = 'our_scope/[email protected].34-rev567';
const id = 'our_scope/[email protected]';

expect(parseMapId(id)).toEqual({
kind: 'parsed',
scope: 'our_scope',
name: 'my-profile',
provider: 'x_prov1der',
variant: 'v4riant',
version: {
major: 1,
minor: 2,
patch: 34,
revision: 567,
value: {
scope: 'our_scope',
name: 'my-profile',
provider: 'x_prov1der',
variant: 'v4riant',
version: {
major: 1,
minor: 2,
revision: 567,
},
},
});
});

it('returns an error for uppercase', () => {
const id = 'SCOPE/profile';

expect(parseDocumentId(id)).toStrictEqual({
kind: 'error',
message: 'scope is not a valid lowercase identifier',
});
expect(parseProfileId(id)).toStrictEqual({
kind: 'error',
message: 'scope is not a valid lowercase identifier',
Expand All @@ -67,12 +111,12 @@ describe('document name parsing', () => {

expect(parseMapId(id)).toStrictEqual({
kind: 'error',
message: 'provider is not a valid lowercase identifier',
message: '"" is not a valid lowercase identifier',
});

expect(parseProfileId(id)).toStrictEqual({
kind: 'error',
message: 'name is not a valid lowercase identifier',
message: '"" is not a valid lowercase identifier',
});
});

Expand All @@ -81,12 +125,12 @@ describe('document name parsing', () => {

expect(parseMapId(id)).toStrictEqual({
kind: 'error',
message: 'variant is not a valid lowercase identifier',
message: '"" is not a valid lowercase identifier',
});

expect(parseProfileId(id)).toStrictEqual({
kind: 'error',
message: 'name is not a valid lowercase identifier',
message: '"" is not a valid lowercase identifier',
});
});

Expand All @@ -96,7 +140,7 @@ describe('document name parsing', () => {
expect(parseMapId(id)).toStrictEqual({
kind: 'error',
message:
'could not parse revision: label must be in format `revN` where N is a non-negative integer',
'revision label must be in format `revN` where N is a non-negative integer',
});
});

Expand Down
Loading

0 comments on commit 20ed549

Please sign in to comment.