Skip to content

Commit

Permalink
improving source builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hactar committed Jan 18, 2024
1 parent 922f306 commit 77a5e07
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions Sources/MapLibreSwiftDSL/Data Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,33 @@ public struct ShapeSource: Source {

@resultBuilder
public enum ShapeDataBuilder {
public static func buildBlock(_ components: MLNShape...) -> ShapeData {
let features = components.compactMap({ $0 as? MLNShape & MLNFeature })

// Handle a single MLNShape element
public static func buildExpression(_ expression: MLNShape) -> [MLNShape] {
return [expression]
}

public static func buildExpression(_ expression: [MLNShape]) -> [MLNShape] {
return expression
}

// Combine elements into an array
public static func buildBlock(_ components: [MLNShape]...) -> [MLNShape] {
return components.flatMap { $0 }
}

// Handle an array of MLNShape (if you want to directly pass arrays)
public static func buildArray(_ components: [MLNShape]) -> [MLNShape] {
return components
}

// Handle for in of MLNShape
public static func buildArray(_ components: [[MLNShape]]) -> [MLNShape] {
return components.flatMap { $0 }
}

// Convert the collected MLNShape array to ShapeData
public static func buildFinalResult(_ components: [MLNShape]) -> ShapeData {
let features = components.compactMap { $0 as? MLNShape & MLNFeature }
if features.count == components.count {
return .features(features)
} else {
Expand Down

0 comments on commit 77a5e07

Please sign in to comment.