Skip to content

Commit 890a58d

Browse files
authored
Stop using variants storage for HTTP/REST/PropertyList sections (#1104)
* Stop using variants storage for HTTP/REST/PropertyList sections * Fix two new warnings after merging main
1 parent a8bcdde commit 890a58d

17 files changed

+264
-286
lines changed

Sources/SwiftDocC/Infrastructure/DocumentationContext.swift

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,17 +1617,15 @@ public class DocumentationContext {
16171617
}
16181618
}
16191619

1620-
let trait = DocumentationDataVariantsTrait(for: selector)
1621-
16221620
// Merge in all the dictionary keys for each target into their section variants.
16231621
keysByTarget.forEach { targetIdentifier, keys in
16241622
let target = documentationCache[targetIdentifier]
16251623
if let semantic = target?.semantic as? Symbol {
1626-
let keys = keys.sorted { $0.name < $1.name }
1627-
if semantic.dictionaryKeysSectionVariants[trait] == nil {
1628-
semantic.dictionaryKeysSectionVariants[trait] = DictionaryKeysSection(dictionaryKeys: keys)
1624+
let keys = keys.sorted(by: \.name)
1625+
if semantic.dictionaryKeysSection == nil {
1626+
semantic.dictionaryKeysSection = DictionaryKeysSection(dictionaryKeys: keys)
16291627
} else {
1630-
semantic.dictionaryKeysSectionVariants[trait]?.mergeDictionaryKeys(keys)
1628+
semantic.dictionaryKeysSection?.mergeDictionaryKeys(keys)
16311629
}
16321630
}
16331631
}
@@ -1636,11 +1634,11 @@ public class DocumentationContext {
16361634
parametersByTarget.forEach { targetIdentifier, parameters in
16371635
let target = documentationCache[targetIdentifier]
16381636
if let semantic = target?.semantic as? Symbol {
1639-
let parameters = parameters.sorted { $0.name < $1.name }
1640-
if semantic.httpParametersSectionVariants[trait] == nil {
1641-
semantic.httpParametersSectionVariants[trait] = HTTPParametersSection(parameters: parameters)
1637+
let parameters = parameters.sorted(by: \.name)
1638+
if semantic.httpParametersSection == nil {
1639+
semantic.httpParametersSection = HTTPParametersSection(parameters: parameters)
16421640
} else {
1643-
semantic.httpParametersSectionVariants[trait]?.mergeParameters(parameters)
1641+
semantic.httpParametersSection?.mergeParameters(parameters)
16441642
}
16451643
}
16461644
}
@@ -1652,12 +1650,12 @@ public class DocumentationContext {
16521650
// Add any body parameters to existing body record
16531651
var localBody = body
16541652
if let identifier = body.symbol?.preciseIdentifier, let bodyParameters = bodyParametersByTarget[identifier] {
1655-
localBody.parameters = bodyParameters.sorted { $0.name < $1.name }
1653+
localBody.parameters = bodyParameters.sorted(by: \.name)
16561654
}
1657-
if semantic.httpBodySectionVariants[trait] == nil {
1658-
semantic.httpBodySectionVariants[trait] = HTTPBodySection(body: localBody)
1655+
if semantic.httpBodySection == nil {
1656+
semantic.httpBodySection = HTTPBodySection(body: localBody)
16591657
} else {
1660-
semantic.httpBodySectionVariants[trait]?.mergeBody(localBody)
1658+
semantic.httpBodySection?.mergeBody(localBody)
16611659
}
16621660
}
16631661
}
@@ -1666,11 +1664,11 @@ public class DocumentationContext {
16661664
responsesByTarget.forEach { targetIdentifier, responses in
16671665
let target = documentationCache[targetIdentifier]
16681666
if let semantic = target?.semantic as? Symbol {
1669-
let responses = responses.sorted { $0.statusCode < $1.statusCode }
1670-
if semantic.httpResponsesSectionVariants[trait] == nil {
1671-
semantic.httpResponsesSectionVariants[trait] = HTTPResponsesSection(responses: responses)
1667+
let responses = responses.sorted(by: \.statusCode)
1668+
if semantic.httpResponsesSection == nil {
1669+
semantic.httpResponsesSection = HTTPResponsesSection(responses: responses)
16721670
} else {
1673-
semantic.httpResponsesSectionVariants[trait]?.mergeResponses(responses)
1671+
semantic.httpResponsesSection?.mergeResponses(responses)
16741672
}
16751673
}
16761674
}

Sources/SwiftDocC/Model/DocumentationNode.swift

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,8 @@ public struct DocumentationNode {
229229
?? .init(availability: [])
230230
}
231231

232-
let endpointVariants = DocumentationDataVariants(
233-
symbolData: unifiedSymbol.mixins,
234-
platformName: platformName
235-
) { mixins -> HTTPEndpointSection? in
236-
if let endpoint = mixins[SymbolGraph.Symbol.HTTP.Endpoint.mixinKey] as? SymbolGraph.Symbol.HTTP.Endpoint {
237-
return HTTPEndpointSection(endpoint: endpoint)
238-
}
239-
return nil
232+
let endpointSection = unifiedSymbol.defaultSymbol?[mixin: SymbolGraph.Symbol.HTTP.Endpoint.self].map { endpoint in
233+
HTTPEndpointSection(endpoint: endpoint)
240234
}
241235

242236
let overloadVariants = DocumentationDataVariants(
@@ -314,13 +308,13 @@ public struct DocumentationNode {
314308
seeAlsoVariants: .empty,
315309
returnsSectionVariants: .empty,
316310
parametersSectionVariants: .empty,
317-
dictionaryKeysSectionVariants: .empty,
318-
possibleValuesSectionVariants: .empty,
319-
httpEndpointSectionVariants: endpointVariants,
320-
httpBodySectionVariants: .empty,
321-
httpParametersSectionVariants: .empty,
322-
httpResponsesSectionVariants: .empty,
323-
redirectsVariants: .empty,
311+
dictionaryKeysSection: nil,
312+
possibleValuesSection: nil,
313+
httpEndpointSection: endpointSection,
314+
httpBodySection: nil,
315+
httpParametersSection: nil,
316+
httpResponsesSection: nil,
317+
redirects: nil,
324318
crossImportOverlayModule: moduleData.bystanders.map({ (moduleData.name, $0) }),
325319
overloadsVariants: overloadVariants
326320
)
@@ -399,9 +393,7 @@ public struct DocumentationNode {
399393
semantic.deprecatedSummaryVariants = DocumentationDataVariants(
400394
defaultVariantValue: deprecated
401395
)
402-
semantic.redirectsVariants = DocumentationDataVariants(
403-
defaultVariantValue: documentationExtension?.redirects
404-
)
396+
semantic.redirects = documentationExtension?.redirects
405397

406398
let filter = ParametersAndReturnValidator(diagnosticEngine: engine, docChunkSources: docChunks.map(\.source))
407399
let (parametersSectionVariants, returnsSectionVariants) = filter.makeParametersAndReturnsSections(
@@ -415,22 +407,22 @@ public struct DocumentationNode {
415407

416408
if let keys = markupModel.discussionTags?.dictionaryKeys, !keys.isEmpty {
417409
// Record the keys extracted from the markdown
418-
semantic.dictionaryKeysSectionVariants[.fallback] = DictionaryKeysSection(dictionaryKeys:keys)
410+
semantic.dictionaryKeysSection = DictionaryKeysSection(dictionaryKeys:keys)
419411
}
420412

421413
if let parameters = markupModel.discussionTags?.httpParameters, !parameters.isEmpty {
422414
// Record the parameters extracted from the markdown
423-
semantic.httpParametersSectionVariants[.fallback] = HTTPParametersSection(parameters: parameters)
415+
semantic.httpParametersSection = HTTPParametersSection(parameters: parameters)
424416
}
425417

426418
if let body = markupModel.discussionTags?.httpBody {
427419
// Record the body extracted from the markdown
428-
semantic.httpBodySectionVariants[.fallback] = HTTPBodySection(body: body)
420+
semantic.httpBodySection = HTTPBodySection(body: body)
429421
}
430422

431423
if let responses = markupModel.discussionTags?.httpResponses, !responses.isEmpty {
432424
// Record the responses extracted from the markdown
433-
semantic.httpResponsesSectionVariants[.fallback] = HTTPResponsesSection(responses: responses)
425+
semantic.httpResponsesSection = HTTPResponsesSection(responses: responses)
434426
}
435427

436428
// The property list symbol's allowed values.
@@ -467,10 +459,10 @@ public struct DocumentationNode {
467459
}
468460

469461
// Record the possible values extracted from the markdown.
470-
semantic.possibleValuesSectionVariants[.fallback] = PropertyListPossibleValuesSection(possibleValues: knownPossibleValues)
462+
semantic.possibleValuesSection = PropertyListPossibleValuesSection(possibleValues: knownPossibleValues)
471463
} else if let symbolAllowedValues {
472464
// Record the symbol possible values even if none are documented.
473-
semantic.possibleValuesSectionVariants[.fallback] = PropertyListPossibleValuesSection(possibleValues: symbolAllowedValues.value.map {
465+
semantic.possibleValuesSection = PropertyListPossibleValuesSection(possibleValues: symbolAllowedValues.value.map {
474466
PropertyListPossibleValuesSection.PossibleValue(value: String($0), contents: [])
475467
})
476468
}
@@ -778,13 +770,13 @@ public struct DocumentationNode {
778770
seeAlsoVariants: .init(swiftVariant: markupModel.seeAlsoSection),
779771
returnsSectionVariants: .init(swiftVariant: markupModel.discussionTags.flatMap({ $0.returns.isEmpty ? nil : ReturnsSection(content: $0.returns[0].contents) })),
780772
parametersSectionVariants: .init(swiftVariant: markupModel.discussionTags.flatMap({ $0.parameters.isEmpty ? nil : ParametersSection(parameters: $0.parameters) })),
781-
dictionaryKeysSectionVariants: .init(swiftVariant: markupModel.discussionTags.flatMap({ $0.dictionaryKeys.isEmpty ? nil : DictionaryKeysSection(dictionaryKeys: $0.dictionaryKeys) })),
782-
possibleValuesSectionVariants: .init(swiftVariant: markupModel.discussionTags.flatMap({ $0.possiblePropertyListValues.isEmpty ? nil : PropertyListPossibleValuesSection(possibleValues: $0.possiblePropertyListValues) })),
783-
httpEndpointSectionVariants: .empty,
784-
httpBodySectionVariants: .empty,
785-
httpParametersSectionVariants: .empty,
786-
httpResponsesSectionVariants: .empty,
787-
redirectsVariants: .init(swiftVariant: article?.redirects)
773+
dictionaryKeysSection: markupModel.discussionTags.flatMap({ $0.dictionaryKeys.isEmpty ? nil : DictionaryKeysSection(dictionaryKeys: $0.dictionaryKeys) }),
774+
possibleValuesSection: markupModel.discussionTags.flatMap({ $0.possiblePropertyListValues.isEmpty ? nil : PropertyListPossibleValuesSection(possibleValues: $0.possiblePropertyListValues) }),
775+
httpEndpointSection: nil,
776+
httpBodySection: nil,
777+
httpParametersSection: nil,
778+
httpResponsesSection: nil,
779+
redirects: article?.redirects
788780
)
789781

790782
self.isVirtual = symbol.isVirtual
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2023 Apple Inc. and the Swift project authors
4+
Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -17,18 +17,18 @@ struct DictionaryKeysSectionTranslator: RenderSectionTranslator {
1717
renderNode: inout RenderNode,
1818
renderNodeTranslator: inout RenderNodeTranslator
1919
) -> VariantCollection<CodableContentSection?>? {
20-
translateSectionToVariantCollection(
21-
documentationDataVariants: symbol.dictionaryKeysSectionVariants
22-
) { _, dictionaryKeysSection in
23-
guard !dictionaryKeysSection.dictionaryKeys.isEmpty else { return nil }
24-
25-
// Filter out keys that aren't backed by a symbol
26-
let filteredKeys = dictionaryKeysSection.dictionaryKeys.filter { $0.symbol != nil }
27-
28-
return PropertiesRenderSection(
20+
guard let dictionaryKeysSection = symbol.dictionaryKeysSection,
21+
!dictionaryKeysSection.dictionaryKeys.isEmpty
22+
else { return nil }
23+
24+
// Filter out keys that aren't backed by a symbol
25+
let filteredKeys = dictionaryKeysSection.dictionaryKeys.filter { $0.symbol != nil }
26+
27+
return VariantCollection(defaultValue: CodableContentSection(
28+
PropertiesRenderSection(
2929
title: DictionaryKeysSection.title,
3030
items: filteredKeys.map { renderNodeTranslator.createRenderProperty(name: $0.name, contents: $0.contents, required: $0.required, symbol: $0.symbol) }
3131
)
32-
}
32+
))
3333
}
3434
}

Sources/SwiftDocC/Model/Rendering/RenderSectionTranslator/HTTPBodySectionTranslator.swift

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,44 @@ struct HTTPBodySectionTranslator: RenderSectionTranslator {
1717
renderNode: inout RenderNode,
1818
renderNodeTranslator: inout RenderNodeTranslator
1919
) -> VariantCollection<CodableContentSection?>? {
20-
translateSectionToVariantCollection(
21-
documentationDataVariants: symbol.httpBodySectionVariants
22-
) { _, httpBodySection -> RenderSection? in
23-
guard let symbol = httpBodySection.body.symbol, let mediaType = httpBodySection.body.mediaType else { return nil }
20+
guard let httpBodySection = symbol.httpBodySection,
21+
let symbol = httpBodySection.body.symbol,
22+
let mediaType = httpBodySection.body.mediaType
23+
else { return nil }
2424

25-
// Filter out parameters that aren't backed by a symbol or don't have a "body" source.
26-
let filteredParameters = httpBodySection.body.parameters.filter { $0.symbol != nil && $0.source == "body" }
27-
28-
let bodyContent = renderNodeTranslator.visitMarkupContainer(
29-
MarkupContainer(httpBodySection.body.contents)
30-
) as! [RenderBlockContent]
31-
32-
let renderedTokens = symbol.declarationFragments?.map { token -> DeclarationRenderSection.Token in
33-
// Create a reference if one found
34-
let reference: ResolvedTopicReference?
35-
if let preciseIdentifier = token.preciseIdentifier,
36-
let resolved = renderNodeTranslator.context.localOrExternalReference(symbolID: preciseIdentifier)
37-
{
38-
reference = resolved
39-
40-
// Add relationship to render references
41-
renderNodeTranslator.collectedTopicReferences.append(resolved)
42-
} else {
43-
reference = nil
44-
}
25+
// Filter out parameters that aren't backed by a symbol or don't have a "body" source.
26+
let filteredParameters = httpBodySection.body.parameters.filter { $0.symbol != nil && $0.source == "body" }
27+
28+
let bodyContent = renderNodeTranslator.visitMarkupContainer(
29+
MarkupContainer(httpBodySection.body.contents)
30+
) as! [RenderBlockContent]
31+
32+
let renderedTokens = symbol.declarationFragments?.map { token -> DeclarationRenderSection.Token in
33+
// Create a reference if one found
34+
let reference: ResolvedTopicReference?
35+
if let preciseIdentifier = token.preciseIdentifier,
36+
let resolved = renderNodeTranslator.context.localOrExternalReference(symbolID: preciseIdentifier)
37+
{
38+
reference = resolved
4539

46-
// Add the declaration token
47-
return DeclarationRenderSection.Token(fragment: token, identifier: reference?.absoluteString)
40+
// Add relationship to render references
41+
renderNodeTranslator.collectedTopicReferences.append(resolved)
42+
} else {
43+
reference = nil
4844
}
4945

50-
return RESTBodyRenderSection(
46+
// Add the declaration token
47+
return DeclarationRenderSection.Token(fragment: token, identifier: reference?.absoluteString)
48+
}
49+
50+
return VariantCollection(defaultValue: CodableContentSection(
51+
RESTBodyRenderSection(
5152
title: "HTTP Body",
5253
mimeType: mediaType,
5354
bodyContentType: renderedTokens ?? [],
5455
content: bodyContent,
5556
parameters: filteredParameters.map { renderNodeTranslator.createRenderProperty(name: $0.name, contents: $0.contents, required: $0.required, symbol: $0.symbol) }
5657
)
57-
}
58+
))
5859
}
5960
}

Sources/SwiftDocC/Model/Rendering/RenderSectionTranslator/HTTPEndpointSectionTranslator.swift

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2023 Apple Inc. and the Swift project authors
4+
Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -20,30 +20,27 @@ struct HTTPEndpointSectionTranslator: RenderSectionTranslator {
2020
renderNodeTranslator: inout RenderNodeTranslator
2121
) -> VariantCollection<CodableContentSection?>? {
2222
// Check if there is any endpoint available
23-
guard !symbol.httpEndpointSectionVariants.isEmpty else { return nil }
23+
guard let section = symbol.httpEndpointSection else { return nil }
2424

25-
return translateSectionToVariantCollection(
26-
documentationDataVariants: symbol.httpEndpointSectionVariants
27-
) { _, section in
28-
let endpointURL: URL
29-
30-
if endpointType == .production {
31-
endpointURL = section.endpoint.baseURL
32-
} else if let sandboxURL = section.endpoint.sandboxURL {
33-
endpointURL = sandboxURL
34-
} else {
35-
return nil
36-
}
25+
let endpointURL: URL
26+
if endpointType == .production {
27+
endpointURL = section.endpoint.baseURL
28+
} else if let sandboxURL = section.endpoint.sandboxURL {
29+
endpointURL = sandboxURL
30+
} else {
31+
return nil
32+
}
3733

38-
return RESTEndpointRenderSection(
34+
return VariantCollection(defaultValue: CodableContentSection(
35+
RESTEndpointRenderSection(
3936
title: (endpointType == .production ? "URL" : "Sandbox URL"),
4037
tokens: Self.tokensFor(method: section.endpoint.method, baseURL: endpointURL, path: section.endpoint.path)
4138
)
42-
}
39+
))
4340
}
4441

4542
// Generate DeclarationFragments from endpoint data.
46-
static func tokensFor(method: String, baseURL: URL?, path: String) -> [RESTEndpointRenderSection.Token] {
43+
private static func tokensFor(method: String, baseURL: URL?, path: String) -> [RESTEndpointRenderSection.Token] {
4744
var fragments : [RESTEndpointRenderSection.Token] = []
4845
// Operation type
4946

0 commit comments

Comments
 (0)