Skip to content

Commit

Permalink
Mai/directional and ambient light (#1305)
Browse files Browse the repository at this point in the history
* Add codegen template for Lights
* Update codegen - add experimental flags for 3D lights
  • Loading branch information
maios authored Jan 12, 2023
1 parent a4528f8 commit 0ad6113
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
8 changes: 8 additions & 0 deletions codegen/style-generator/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions codegen/style-generator/templates/3DLight.swift.ejs
Original file line number Diff line number Diff line change
@@ -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.
44 changes: 44 additions & 0 deletions codegen/style-generator/templates/3DLightTests.swift.ejs
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion codegen/vendor/mapbox-maps-stylegen
2 changes: 1 addition & 1 deletion mapbox-maps-ios-private

0 comments on commit 0ad6113

Please sign in to comment.