Skip to content

Add Map API #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Sources/OpenGraph/Attribute/Attribute/External.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public struct External<Value> {

extension External: _AttributeBody {
public static var comparisonMode: OGComparisonMode { ._3 }

public static var flags: OGAttributeTypeFlags { [] }

public static func _update(_: UnsafeMutableRawPointer, attribute _: AnyAttribute) {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,19 @@
// Audited for RELEASE_2021
// Status: Complete

public struct Focus<Root, Value> {
@frozen
public struct Focus<Root, Value>: Rule, CustomStringConvertible {
public var root: Attribute<Root>
public var keyPath: KeyPath<Root, Value>

public init(root: Attribute<Root>, keyPath: KeyPath<Root, Value>) {
self.root = root
self.keyPath = keyPath
}
}

// MARK: - Rule

extension Focus: Rule {
public var value: Value { root.value[keyPath: keyPath] }
public static var flags: OGAttributeTypeFlags { [] }
}

// MARK: CustomStringConvertible
public static var flags: OGAttributeTypeFlags { [] }

Check warning on line 20 in Sources/OpenGraph/Attribute/Rule/Focus.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenGraph/Attribute/Rule/Focus.swift#L20

Added line #L20 was not covered by tests

extension Focus: CustomStringConvertible {
public var description: String { "• \(Metadata(Value.self).description)" }
}
25 changes: 25 additions & 0 deletions Sources/OpenGraph/Attribute/Rule/Map.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Map.swift
// OpenGraph
//
// Audited for RELEASE_2021
// Status: Complete

// MARK: - Map

@frozen
public struct Map<Source, Value>: Rule, CustomStringConvertible {
public var arg: Attribute<Source>
public let body: (Source) -> Value

public init(_ arg: Attribute<Source>, _ body: @escaping (Source) -> Value) {
self.arg = arg
self.body = body

Check warning on line 17 in Sources/OpenGraph/Attribute/Rule/Map.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenGraph/Attribute/Rule/Map.swift#L15-L17

Added lines #L15 - L17 were not covered by tests
}

public var value: Value { body(arg.value) }

Check warning on line 20 in Sources/OpenGraph/Attribute/Rule/Map.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenGraph/Attribute/Rule/Map.swift#L20

Added line #L20 was not covered by tests

public static var flags: OGAttributeTypeFlags { [] }

Check warning on line 22 in Sources/OpenGraph/Attribute/Rule/Map.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenGraph/Attribute/Rule/Map.swift#L22

Added line #L22 was not covered by tests

public var description: String { "λ \(Value.self)" }

Check warning on line 24 in Sources/OpenGraph/Attribute/Rule/Map.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenGraph/Attribute/Rule/Map.swift#L24

Added line #L24 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// AttributeTests.swift
//
//
//
// OpenGraphCompatibilityTests

import Testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// ExternalTests.swift
//
//
//
// OpenGraphCompatibilityTests

import Testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// FocusTests.swift
//
//
//
// OpenGraphCompatibilityTests

import Testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// IndirectAttributeTests.swift
//
//
// Created by Kyle on 2024/3/3.
//
// OpenGraphCompatibilityTests

import Testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// AnyOptionalAttributeTests.swift
//
//
// Created by Kyle on 2024/3/3.
//
// OpenGraphCompatibilityTests

import Testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// OptionalAttributeTests.swift
//
//
// Created by Kyle on 2024/3/3.
//
// OpenGraphCompatibilityTests

import Testing

Expand Down
26 changes: 26 additions & 0 deletions Tests/OpenGraphCompatibilityTests/Attribute/Rule/MapTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// MapTests.swift
// OpenGraphCompatibilityTests

import Testing

@Suite(.disabled(if: !compatibilityTestEnabled))
final class MapTests: AttributeTestBase {
@Test
func description() throws {
let map = Map(.init(value: 2)) { $0.description }
let map2 = Map(.init(value: 1)) { $0 + 2 }

#expect(map.description == "λ String")
#expect(map2.description == "λ Int")
}

@Test
func value() throws {
let map = Map(.init(value: 2)) { $0.description }
let map2 = Map(.init(value: 1)) { $0 + 2 }

#expect(map.value == "2")
#expect(map2.value == 3)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// RuleTests.swift
//
//
//
// OpenGraphCompatibilityTests

import Testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// AnyWeakAttributeTests.swift
//
//
// Created by Kyle on 2024/3/4.
//
// OpenGraphCompatibilityTests

import Testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// WeakAttributeTests.swift
//
//
// Created by Kyle on 2024/3/3.
//
// OpenGraphCompatibilityTests

import Testing

Expand Down