Skip to content

Commit 19f47b0

Browse files
persidskiygithub-actions[bot]
authored andcommitted
Rework PromoteId to accomodate expression variant
GitOrigin-RevId: 9649fb299e6a138f4f43e857f1241fe488eb36d6
1 parent e798f9e commit 19f47b0

File tree

17 files changed

+350
-32
lines changed

17 files changed

+350
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone.
77
* The Interactions and Featuresets API is promoted from experimental. The new API allows you to add interaction handlers to layers, Standard Style featuresets (POI, Buildings and Place Labels), and the map itself in the consistent way. You can control the propagation of events, tappable area, and the order of event handling.
88
* The experimental style `MapStyle.standardExperimental` is removed. Use `MapStyle.standard` instead.
99
* Methods `GestureManager.onMapTap`, `GestureManager.onMapLongPress`, `GestureManager.onLayerTap`, `GestureManager.onLayerLongPress` and their SwiftUI counterparts are deprecated. Use `TapInteraction` and `LongPressInteraction` instead.
10+
* Add new `VectorSource.promoteId2` and `GeoJSONSource.promoteId2`. Deprecate `VectorSource.promoteId` and `GeoJSONSource.promoteId`. The newer version support the expression variant of promoteId, which can be used to dynamically nominate IDs to the features.
1011

1112
```swift
1213
// Before (SwiftUI)

Sources/MapboxMaps/Documentation.docc/API Catalogs/Sources.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- ``RasterArraySource``
1616
- ``VectorSource``
1717
- ``PromoteId``
18+
- ``VectorSourcePromoteId``
1819
- ``Scheme``
1920
- ``Encoding``
2021
- ``CustomGeometrySource``

Sources/MapboxMaps/Style/Generated/Sources/GeoJSONSource.swift

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/MapboxMaps/Style/Generated/Sources/RasterArraySource.swift

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/MapboxMaps/Style/Generated/Sources/VectorSource.swift

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/MapboxMaps/Style/Types/PromoteId.swift

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
import Foundation
22

3+
/// Denotes promote-id setting for ``VectorSource``.
4+
///
5+
/// In vector sources, a promote-id setting can be applied globally for all layers, or individually for each vector source layer.
6+
public enum VectorSourcePromoteId: Equatable, Codable, Sendable {
7+
// The promoteId will be applied to all layers in the vector source.
8+
case global(Value<String>)
9+
10+
// The promoteId will be applied per each layer in the vector source.
11+
case byLayer([String: Value<String>])
12+
13+
public init(from decoder: Decoder) throws {
14+
let container = try decoder.singleValueContainer()
15+
16+
if let decodedSingle = try? container.decode(Value<String>.self) {
17+
self = .global(decodedSingle)
18+
return
19+
}
20+
21+
if let decodedDict = try? container.decode([String: Value<String>].self) {
22+
self = .byLayer(decodedDict)
23+
return
24+
}
25+
26+
let context = DecodingError.Context(codingPath: decoder.codingPath,
27+
debugDescription: "Failed to decode VectorSourcePromoteId")
28+
throw DecodingError.dataCorrupted(context)
29+
}
30+
31+
public func encode(to encoder: Encoder) throws {
32+
var container = encoder.singleValueContainer()
33+
34+
switch self {
35+
case .global(let string):
36+
try container.encode(string)
37+
case .byLayer(let object):
38+
try container.encode(object)
39+
}
40+
}
41+
}
42+
43+
@available(*, deprecated, message: "Use VectorSourcePromoteId instead")
344
public enum PromoteId: Equatable, Codable, Sendable {
445
case string(String)
546
case object([String: String])
@@ -31,4 +72,45 @@ public enum PromoteId: Equatable, Codable, Sendable {
3172
try container.encode(object)
3273
}
3374
}
75+
76+
init?(from: VectorSourcePromoteId?) {
77+
guard let from else { return nil }
78+
switch from {
79+
case let .global(val):
80+
guard let constant = val.asConstant else { return nil }
81+
self = .string(constant)
82+
case let .byLayer(dict):
83+
self = .object(dict.compactMapValues { $0.asConstant })
84+
}
85+
}
86+
87+
init?(from: Value<String>?) {
88+
guard let from, let const = from.asConstant else { return nil }
89+
self = .string(const)
90+
}
91+
}
92+
93+
@available(*, deprecated)
94+
extension VectorSourcePromoteId {
95+
init?(from: PromoteId?) {
96+
guard let from else { return nil }
97+
switch from {
98+
case .string(let string):
99+
self = .global(.constant(string))
100+
case .object(let dict):
101+
self = .byLayer(dict.mapValues { .constant($0) })
102+
}
103+
}
104+
}
105+
106+
@available(*, deprecated)
107+
extension Value where T == String {
108+
init?(from: PromoteId?) {
109+
if let from,
110+
case let .string(string) = from {
111+
self = .init(constant: string)
112+
} else {
113+
return nil
114+
}
115+
}
34116
}

Tests/MapboxMapsTests/Style/Fixtures/Fixtures.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ internal extension PromoteId {
127127
}
128128
}
129129

130+
internal extension VectorSourcePromoteId {
131+
static func testSourceValue() -> VectorSourcePromoteId {
132+
return .byLayer([
133+
"layer-1": .constant("test-promote-id-1"),
134+
"layer-2": .expression(Exp(.get, "name")),
135+
])
136+
}
137+
}
138+
139+
extension Value where T == String {
140+
static func testSourceValue() -> Value {
141+
.expression(Exp(.get, "name"))
142+
}
143+
}
144+
130145
internal extension Array where Element == RasterArraySource.RasterDataLayer {
131146
static func testSourceValue() -> [RasterArraySource.RasterDataLayer] {
132147
return [.init(layerId: "test-layer-id", bands: ["band_0", "band_1"])]

Tests/MapboxMapsTests/Style/Generated/IntegrationTests/Sources/GeoJSONSourceIntegrationTests.swift

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/MapboxMapsTests/Style/Generated/IntegrationTests/Sources/ImageSourceIntegrationTests.swift

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/MapboxMapsTests/Style/Generated/IntegrationTests/Sources/RasterArraySourceIntegrationTests.swift

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)