Skip to content

Commit d4dda0a

Browse files
committed
Improve name handling for bundled schemas
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 0de5bb7 commit d4dda0a

3 files changed

Lines changed: 71 additions & 27 deletions

File tree

src/ir/ir_symbol.cc

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
11
#include <sourcemeta/codegen/ir.h>
22

3+
#include <sourcemeta/core/uri.h>
4+
35
#include <algorithm> // std::ranges::reverse
46
#include <cassert> // assert
5-
#include <string> // std::string
7+
#include <sstream> // std::istringstream
8+
#include <string> // std::string, std::getline
69
#include <vector> // std::vector
710

11+
namespace {
12+
13+
// If the input looks like an absolute URI, extract its path segments.
14+
// Otherwise, add the input as a single segment.
15+
// Note: segments are added in reverse order because the caller reverses
16+
// the entire result at the end.
17+
auto push_token_segments(std::vector<std::string> &result,
18+
const std::string &value) -> void {
19+
try {
20+
const sourcemeta::core::URI uri{value};
21+
if (uri.is_absolute()) {
22+
const auto path{uri.path()};
23+
if (path.has_value() && !path->empty()) {
24+
std::vector<std::string> segments;
25+
std::istringstream stream{std::string{path.value()}};
26+
std::string segment;
27+
while (std::getline(stream, segment, '/')) {
28+
if (!segment.empty()) {
29+
segments.emplace_back(segment);
30+
}
31+
}
32+
33+
// Reverse segments since the caller will reverse the entire result
34+
std::ranges::reverse(segments);
35+
for (const auto &path_segment : segments) {
36+
result.emplace_back(path_segment);
37+
}
38+
39+
return;
40+
}
41+
}
42+
// NOLINTNEXTLINE(bugprone-empty-catch)
43+
} catch (const sourcemeta::core::URIParseError &) {
44+
// Not a valid URI, fall through to default behavior
45+
}
46+
47+
result.emplace_back(value);
48+
}
49+
50+
} // namespace
51+
852
namespace sourcemeta::codegen {
953

1054
auto symbol(const sourcemeta::core::SchemaFrame &frame,
@@ -29,14 +73,14 @@ auto symbol(const sourcemeta::core::SchemaFrame &frame,
2973
if (segments_skipped >= 2) {
3074
const auto &last_token{current_pointer.back()};
3175
if (last_token.is_property()) {
32-
result.emplace_back(last_token.to_property());
76+
push_token_segments(result, last_token.to_property());
3377
} else {
3478
result.emplace_back(std::to_string(last_token.to_index()));
3579
}
3680
} else {
3781
const auto &token{current_pointer.back()};
3882
if (token.is_property()) {
39-
result.emplace_back(token.to_property());
83+
push_token_segments(result, token.to_property());
4084
} else {
4185
result.emplace_back(std::to_string(token.to_index()));
4286
}

test/e2e/typescript/2020-12/bundled_schema/expected.d.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
export type ApiResponseMeta = ApiResponseHttpsexamplecomschemasmetadata;
1+
export type ApiResponseMeta = ApiResponseSchemasMetadata;
22

3-
export type ApiResponseData = ApiResponseHttpsexamplecomschemasuser;
3+
export type ApiResponseData = ApiResponseSchemasUser;
44

55
export type ApiResponseAdditionalProperties = never;
66

7-
export type ApiResponseHttpsexamplecomschemasuserName = string;
7+
export type ApiResponseSchemasUserName = string;
88

9-
export type ApiResponseHttpsexamplecomschemasuserId = number;
9+
export type ApiResponseSchemasUserId = number;
1010

11-
export type ApiResponseHttpsexamplecomschemasuserEmail = ApiResponseHttpsexamplecomschemasemail;
11+
export type ApiResponseSchemasUserEmail = ApiResponseSchemasEmail;
1212

13-
export type ApiResponseHttpsexamplecomschemasuserAdditionalProperties = never;
13+
export type ApiResponseSchemasUserAdditionalProperties = never;
1414

15-
export interface ApiResponseHttpsexamplecomschemasuser {
16-
"id": ApiResponseHttpsexamplecomschemasuserId;
17-
"name": ApiResponseHttpsexamplecomschemasuserName;
18-
"email"?: ApiResponseHttpsexamplecomschemasuserEmail;
15+
export interface ApiResponseSchemasUser {
16+
"id": ApiResponseSchemasUserId;
17+
"name": ApiResponseSchemasUserName;
18+
"email"?: ApiResponseSchemasUserEmail;
1919
}
2020

21-
export type ApiResponseHttpsexamplecomschemasmetadataVersion = number;
21+
export type ApiResponseSchemasMetadataVersion = number;
2222

23-
export type ApiResponseHttpsexamplecomschemasmetadataTimestamp = string;
23+
export type ApiResponseSchemasMetadataTimestamp = string;
2424

25-
export type ApiResponseHttpsexamplecomschemasmetadataAdditionalProperties = never;
25+
export type ApiResponseSchemasMetadataAdditionalProperties = never;
2626

27-
export interface ApiResponseHttpsexamplecomschemasmetadata {
28-
"timestamp"?: ApiResponseHttpsexamplecomschemasmetadataTimestamp;
29-
"version"?: ApiResponseHttpsexamplecomschemasmetadataVersion;
27+
export interface ApiResponseSchemasMetadata {
28+
"timestamp"?: ApiResponseSchemasMetadataTimestamp;
29+
"version"?: ApiResponseSchemasMetadataVersion;
3030
}
3131

32-
export type ApiResponseHttpsexamplecomschemasemail = string;
32+
export type ApiResponseSchemasEmail = string;
3333

3434
export interface ApiResponse {
3535
"data": ApiResponseData;

test/e2e/typescript/2020-12/bundled_schema/test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
ApiResponse,
3-
ApiResponseHttpsexamplecomschemasuser,
4-
ApiResponseHttpsexamplecomschemasmetadata,
5-
ApiResponseHttpsexamplecomschemasemail
3+
ApiResponseSchemasUser,
4+
ApiResponseSchemasMetadata,
5+
ApiResponseSchemasEmail
66
} from "./expected";
77

88

@@ -29,26 +29,26 @@ const minimalResponse: ApiResponse = {
2929
};
3030

3131
// Valid: user object directly
32-
const user: ApiResponseHttpsexamplecomschemasuser = {
32+
const user: ApiResponseSchemasUser = {
3333
id: 42,
3434
name: "Test User"
3535
};
3636

3737
// Valid: user with email
38-
const userWithEmail: ApiResponseHttpsexamplecomschemasuser = {
38+
const userWithEmail: ApiResponseSchemasUser = {
3939
id: 42,
4040
name: "Test User",
4141
email: "test@example.com"
4242
};
4343

4444
// Valid: metadata object
45-
const metadata: ApiResponseHttpsexamplecomschemasmetadata = {
45+
const metadata: ApiResponseSchemasMetadata = {
4646
timestamp: "2024-01-15",
4747
version: 2
4848
};
4949

5050
// Valid: email is just a string
51-
const email: ApiResponseHttpsexamplecomschemasemail = "user@domain.com";
51+
const email: ApiResponseSchemasEmail = "user@domain.com";
5252

5353
// Invalid: missing required field 'data'
5454
// @ts-expect-error - data is required

0 commit comments

Comments
 (0)