Skip to content
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

Исправлены ключи для TokenValues #63

Merged
merged 2 commits into from
Feb 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class DefaultBoxShadowTokensContextProvider: BoxShadowTokensContextProvide
return nil
}

guard let nightTokenValue = tokenValues.night.first(where: { $0.name == dayTokenValue.name }) else {
guard let nightTokenValue = tokenValues.hhNight.first(where: { $0.name == dayTokenValue.name }) else {
throw BoxShadowTokensContextProviderError(code: .nightValueNotFound(tokenName: dayTokenValue.name))
}

Expand All @@ -41,7 +41,7 @@ final class DefaultBoxShadowTokensContextProvider: BoxShadowTokensContextProvide
// MARK: -

func fetchBoxShadowTokensContext(from tokenValues: TokenValues) throws -> [BoxShadowToken] {
try tokenValues.day
try tokenValues.hhDay
.compactMap { try makeBoxShadowToken(from: $0, tokenValues: tokenValues) }
.sorted { $0.path.joined() < $1.path.joined() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider {
fallbackValue: String,
tokenValues: TokenValues
) throws -> String {
guard let nightToken = tokenValues.night.first(where: { $0.name == tokenName }) else {
guard let nightToken = tokenValues.hhNight.first(where: { $0.name == tokenName }) else {
fallbackWarning(tokenName: tokenName)
return fallbackValue
}
Expand All @@ -45,7 +45,7 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider {
fallbackRefence: String,
tokenValues: TokenValues
) throws -> String {
guard let nightToken = tokenValues.night.first(where: { $0.name == tokenName }) else {
guard let nightToken = tokenValues.hhNight.first(where: { $0.name == tokenName }) else {
fallbackWarning(tokenName: tokenName)
return fallbackRefence
}
Expand All @@ -55,7 +55,7 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider {
return fallbackRefence
}

return try tokensResolver.resolveBaseReference(nightValue, tokenValues: tokenValues.night)
return try tokensResolver.resolveBaseReference(nightValue, tokenValues: tokenValues.hhNight)
}

private func makeColorToken(
Expand All @@ -72,7 +72,7 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider {

let dayReference = try tokensResolver.resolveBaseReference(
dayValue,
tokenValues: tokenValues.day
tokenValues: tokenValues.hhDay
)

let nightReference = try resolveNightReference(
Expand Down Expand Up @@ -132,7 +132,7 @@ final class DefaultColorTokensContextProvider: ColorTokensContextProvider {
// MARK: -

func fetchColorTokensContext(from tokenValues: TokenValues) throws -> [String: Any] {
let colors: [ColorToken] = try tokenValues.day.compactMap { (token: TokenValue) in
let colors: [ColorToken] = try tokenValues.hhDay.compactMap { (token: TokenValue) in
guard case .color(let dayValue) = token.type else {
return nil
}
Expand Down
33 changes: 27 additions & 6 deletions Sources/FigmaGen/Models/Token/TokenValues.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import Foundation

struct TokenValues: Codable, Hashable {
struct TokenValues: Hashable {

// MARK: - Instance Properties

let core: [TokenValue]
let semantic: [TokenValue]
let colors: [TokenValue]
let typography: [TokenValue]
let day: [TokenValue]
let night: [TokenValue]
let hhDay: [TokenValue]
let hhNight: [TokenValue]
let zpDay: [TokenValue]

// MARK: - Instance Properties

Expand All @@ -18,13 +19,33 @@ struct TokenValues: Codable, Hashable {
func getThemeTokenValues(theme: Theme) -> [TokenValue] {
switch theme {
case .day:
return [day, core, semantic, colors, typography].flatMap { $0 }
return [hhDay, core, semantic, colors, typography].flatMap { $0 }

case .night:
return [night, core, semantic, colors, typography].flatMap { $0 }
return [hhNight, core, semantic, colors, typography].flatMap { $0 }

case .undefined:
return [core, semantic, colors, typography, day, night].flatMap { $0 }
return [core, semantic, colors, typography, hhDay, hhNight].flatMap { $0 }
}
}
}

// MARK: - Codable

extension TokenValues: Codable {

// MARK: - Nested Types

private enum CodingKeys: String, CodingKey {

// MARK: - Enumeration Cases

case core
case semantic
case colors
case typography
case hhDay = "hh-day"
case hhNight = "hh-night"
case zpDay = "zp-day"
}
}
43 changes: 25 additions & 18 deletions Tests/FigmaGenTests/TokensResolverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ final class TokensResolverTests: XCTestCase {
semantic: [],
colors: [],
typography: [],
day: [],
night: []
hhDay: [],
hhNight: [],
zpDay: []
)

let value = "{core.space.1-x} + {core.space.1-x} / 2"
Expand Down Expand Up @@ -64,8 +65,9 @@ final class TokensResolverTests: XCTestCase {
TokenValue(type: .core(value: "#ffffff"), name: "color.base.white")
],
typography: [],
day: [],
night: []
hhDay: [],
hhNight: [],
zpDay: []
)

let value = "rgba({color.base.white}, {semantic.opacity.disabled})"
Expand Down Expand Up @@ -97,8 +99,9 @@ final class TokensResolverTests: XCTestCase {
TokenValue(type: .color(value: "#d64030"), name: "color.base.red.50")
],
typography: [],
day: [],
night: []
hhDay: [],
hhNight: [],
zpDay: []
)

let firstColor = "rgba({color.base.red.50}, {semantic.opacity.transparent})"
Expand Down Expand Up @@ -187,8 +190,9 @@ final class TokensResolverTests: XCTestCase {
TokenValue(type: .color(value: "#111"), name: "color.base.gray.5")
],
typography: [],
day: [],
night: []
hhDay: [],
hhNight: [],
zpDay: []
)

let value1 = "rgba({color.base.white}, {semantic.opacity.disabled})"
Expand Down Expand Up @@ -221,17 +225,18 @@ final class TokensResolverTests: XCTestCase {
TokenValue(type: .color(value: "#000000"), name: "color.base.black")
],
typography: [],
day: [],
night: [
hhDay: [],
hhNight: [
TokenValue(type: .color(value: "{color.base.black}"), name: "color.background.primary"),
TokenValue(type: .color(value: "{color.background.primary}"), name: "color.background.primary.nested")
]
],
zpDay: []
)

let value = "{color.background.primary.nested}"
let expectedBaseReference = "{color.base.black}"

let actualBaseReference = try tokensResolver.resolveBaseReference(value, tokenValues: tokenValues.night)
let actualBaseReference = try tokensResolver.resolveBaseReference(value, tokenValues: tokenValues.hhNight)

XCTAssertEqual(actualBaseReference, expectedBaseReference)
}
Expand All @@ -248,17 +253,18 @@ final class TokensResolverTests: XCTestCase {
TokenValue(type: .color(value: "#000000"), name: "color.base.black")
],
typography: [],
day: [],
night: [
hhDay: [],
hhNight: [
TokenValue(type: .color(value: "{color.base.black}"), name: "color.background.primary"),
TokenValue(type: .color(value: "{color.background.primary}"), name: "color.background.primary.nested")
]
],
zpDay: []
)

let value = "rgba( {color.background.primary.nested}, {semantic.opacity.disabled})"
let expectedBaseReference = "rgba( {color.base.black}, {semantic.opacity.disabled})"

let actualBaseReference = try tokensResolver.resolveBaseReference(value, tokenValues: tokenValues.night)
let actualBaseReference = try tokensResolver.resolveBaseReference(value, tokenValues: tokenValues.hhNight)

XCTAssertEqual(actualBaseReference, expectedBaseReference)
}
Expand All @@ -273,8 +279,9 @@ extension TokenValues {
semantic: [],
colors: [],
typography: [],
day: [],
night: []
hhDay: [],
hhNight: [],
zpDay: []
)
}
#endif
Loading