From 0ad611393ced79c70f2606111086677d1a853c48 Mon Sep 17 00:00:00 2001 From: Mai Mai Date: Thu, 12 Jan 2023 14:13:08 +0200 Subject: [PATCH] Mai/directional and ambient light (#1305) * Add codegen template for Lights * Update codegen - add experimental flags for 3D lights --- .../style-generator/generate-style-code.js | 8 ++++ .../templates/3DLight.swift.ejs | 43 ++++++++++++++++++ .../templates/3DLightTests.swift.ejs | 44 +++++++++++++++++++ codegen/vendor/mapbox-maps-stylegen | 2 +- mapbox-maps-ios-private | 2 +- 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 codegen/style-generator/templates/3DLight.swift.ejs create mode 100644 codegen/style-generator/templates/3DLightTests.swift.ejs diff --git a/codegen/style-generator/generate-style-code.js b/codegen/style-generator/generate-style-code.js index e7f8b2139069..fe36d4f8aea5 100644 --- a/codegen/style-generator/generate-style-code.js +++ b/codegen/style-generator/generate-style-code.js @@ -17,6 +17,14 @@ const baseDirectory = generatePremiumApis ? '../mapbox-maps-ios-private' : '../m const lightSwift = ejs.compile(fs.readFileSync('style-generator/templates/Light.swift.ejs', 'utf8'), { strict: true }); writeIfModified(`${baseDirectory}/Sources/MapboxMaps/Style/Generated/Light/Light.swift`, lightSwift({ properties: style.light.properties })); +// Swift 3D Light +const light3DSwift = ejs.compile(fs.readFileSync('style-generator/templates/3DLight.swift.ejs', 'utf8'), { strict: true }); +const light3DTestsSwift = ejs.compile(fs.readFileSync('style-generator/templates/3DLightTests.swift.ejs', 'utf8'), { strict: true }); +for (const light of style.lights3D) { + writeIfModified(`${baseDirectory}/Sources/MapboxMaps/Style/Generated/Light/${camelizeWithUndercoreRemoved(light.name)}Light.swift`, light3DSwift(light)); + writeIfModified(`${baseDirectory}/Tests/MapboxMapsTests/Style/Generated/${camelizeWithUndercoreRemoved(light.name)}LightTests.swift`, light3DTestsSwift(light)); +} + // Swift Layers const layerSwift = ejs.compile(fs.readFileSync('style-generator/templates/Layer.swift.ejs', 'utf8'), { strict: true }); for (const layer of style.layers) { diff --git a/codegen/style-generator/templates/3DLight.swift.ejs b/codegen/style-generator/templates/3DLight.swift.ejs new file mode 100644 index 000000000000..db0ba7eacc03 --- /dev/null +++ b/codegen/style-generator/templates/3DLight.swift.ejs @@ -0,0 +1,43 @@ +<% + const properties = locals.properties + const lightName = locals.name +-%> +// This file is generated. +import Foundation + +/// Represents 3D <%- camelizeWithLeadingLowercase(lightName) %> light. +/// - SeeAlso: [Mapbox Style Specification](https://www.mapbox.com/mapbox-gl-style-spec/#light) +<% if (locals.experimental) { -%>@_spi(Experimental) <% } -%>public struct <%- camelizeWithUndercoreRemoved(lightName) %>Light: Light3DProtocol { + public let lightType: Light3DType + <%_ for (const property of properties) { -%> + + /// <%- markdownFriendlyDoc(property) %> + <%_ if (property.name === 'id') { _%> + <% if (property.experimental) { -%>@_spi(Experimental) <% } -%>public let <%- camelizeWithLeadingLowercase(property.name) %>: <%- propertySwiftElementType(property) %> + <%_ } else { _%> + <% if (property.experimental) { -%>@_spi(Experimental) <% } -%>public var <%- camelizeWithLeadingLowercase(property.name) %>: <%- propertySwiftValueType(property) %>? + <%_ } _%> + <%_ if (property.transition) { -%> + + /// Transition property for `<%- camelizeWithLeadingLowercase(property.name) %>` + <% if (property.experimental) { -%>@_spi(Experimental) <% } -%>public var <%- camelizeWithLeadingLowercase(property.name) %>Transition: StyleTransition? + <%_ } -%> + <%_ } -%> + + public init(id: String) { + self.id = id + self.lightType = .<%- camelizeWithLeadingLowercase(lightName) %> + } + + enum CodingKeys: String, CodingKey { + case lightType = "type" + <%_ for (const property of properties) { -%> + case <%- camelizeWithLeadingLowercase(property.name) %> = "<%- property.name %>" + <%_ if (property.transition) { -%> + case <%- camelizeWithLeadingLowercase(property.name) %>Transition = "<%- property.name %>-transition" + <%_ } -%> + <%_ } -%> + } +} + +// End of generated file. \ No newline at end of file diff --git a/codegen/style-generator/templates/3DLightTests.swift.ejs b/codegen/style-generator/templates/3DLightTests.swift.ejs new file mode 100644 index 000000000000..8a53e5dfb097 --- /dev/null +++ b/codegen/style-generator/templates/3DLightTests.swift.ejs @@ -0,0 +1,44 @@ +<% + const properties = locals.properties + const lightName = locals.name + const containsExperimentalApi = properties.some((property) => property.experimental === true) || locals.experimental +-%> +// This file is generated +import XCTest +<%if (containsExperimentalApi) { -%>@_spi(Experimental) <% } -%>@testable import MapboxMaps + +final class <%- camelizeWithUndercoreRemoved(lightName) %>LightTests: XCTestCase { + + func testLightEncodingAndDecoding() throws { + let lightID = UUID().uuidString + var light = <%- camelizeWithUndercoreRemoved(lightName) %>Light(id: lightID) + <%_ for (const property of properties) { -%> + <%_ if (property.name !== 'id') { _%> + light.<%- camelizeWithLeadingLowercase(property.name) %> = <%- propertySwiftValueType(property) %>.testConstantValue() + <%_ if (property.transition) { -%> + light.<%- camelizeWithLeadingLowercase(property.name) %>Transition = StyleTransition(duration: 10.0, delay: 10.0) + <%_ } -%> + <%_ } _%> + <%_ } -%> + + let data = try JSONEncoder().encode(light) + XCTAssertFalse(data.isEmpty) + + let decodedLight = try JSONDecoder().decode(<%- camelizeWithUndercoreRemoved(lightName) %>Light.self, from: data) + + XCTAssertEqual(decodedLight.lightType, Light3DType.<%- camelizeWithLeadingLowercase(lightName) %>) + <%_ for (const property of properties) { -%> + <%_ if (property.name === 'id') { _%> + XCTAssertEqual(decodedLight.id, lightID) + <%_ } else { _%> + XCTAssertEqual(decodedLight.<%- camelizeWithLeadingLowercase(property.name) %>, <%- propertySwiftValueType(property) %>.testConstantValue()) + <%_ if (property.transition) { -%> + XCTAssertEqual(decodedLight.<%- camelizeWithLeadingLowercase(property.name) %>Transition?.duration, 10) + XCTAssertEqual(decodedLight.<%- camelizeWithLeadingLowercase(property.name) %>Transition?.delay, 10) + <%_ } -%> + <%_ } -%> + <%_ } -%> + } +} + +// End of generated file diff --git a/codegen/vendor/mapbox-maps-stylegen b/codegen/vendor/mapbox-maps-stylegen index 917e65c399d1..3fad60c26b40 160000 --- a/codegen/vendor/mapbox-maps-stylegen +++ b/codegen/vendor/mapbox-maps-stylegen @@ -1 +1 @@ -Subproject commit 917e65c399d1f6946def458eae1a08c59b6dedc8 +Subproject commit 3fad60c26b403d1b4c6335935a7d5e179bb0d40c diff --git a/mapbox-maps-ios-private b/mapbox-maps-ios-private index 9abe78e87af5..1e2c3c6bdc51 160000 --- a/mapbox-maps-ios-private +++ b/mapbox-maps-ios-private @@ -1 +1 @@ -Subproject commit 9abe78e87af50e75ca05d08874ce5c4a40289cc3 +Subproject commit 1e2c3c6bdc51d7b6651109005968abb41d15314d