Compare commits

...
2 Commits
Author SHA1 Message Date
Vladimir Kukushkin 92448c359f Add (default:) string interpolation overloads (#471)
Add [Default String
Interpolation](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0477-default-interpolation-values.md)
overloads for `MetadataValue` to restore compatibility with
`\(default:)` interpolation.

### Motivation:

1.13.0 introduced a breaking change to the default string interpolation
compatibility. We do not want to revert the custom string interpolation,
but can fix compatibility with the default string interpolation as much
as possible.

### Modifications:

Add `\(default:)` overloads to `MetadataValue.StringInterpolation`,
similar to the once in `DefaultStringInterpolation`.

### Result:

Compatibility with `logger.info("Log message", metadata: ["value":
"\(value, default: "none")"])` is restored.
2026-06-09 17:01:25 +02:00
Vladimir Kukushkin 2aed77ae5e Match DefaultStringInterpolation appendInterpolation overloads (#469)
Custom `MetadataValue.StringInterpolation` broke compatibility with
`DefaultStringInterpolation`. Rearrange `appendInterpolation` overloads
to match `DefaultStringInterpolation` to maintain backward
compatibility.

### Motivation:

Custom `MetadataValue.StringInterpolation` broke compatibility with
`DefaultStringInterpolation`. Rearrange `appendInterpolation` overloads
to match `DefaultStringInterpolation` to maintain backward
compatibility.

### Modifications:

`MetadataValue.StringInterpolation` `appendInterpolation` overloads
changed.

### Result:

Code relying on matching correct `DefaultStringInterpolation`
`appendInterpolation` overload compiles.
2026-05-28 13:35:14 +01:00
2 changed files with 510 additions and 40 deletions

No files matched your search

+161 -40
View File
@@ -350,35 +350,8 @@ extension Logger.MetadataValue: ExpressibleByStringInterpolation {
self.output.append(literal)
}
/// Interpolation with a custom attributes closure.
///
/// When called, the result will be `.stringConvertible(AttributedStringCarrier(...))`
/// instead of `.string(...)`.
///
/// ```swift
/// "\(userId, attributes: { $0[MyAttribute.self] = .flagged })"
/// ```
@inlinable
public mutating func appendInterpolation<T>(
_ value: T,
attributes: @Sendable (inout Logger.MetadataValueAttributes) -> Void
) where T: CustomStringConvertible & Sendable {
self.output.append(value.description)
attributes(&self.attributes)
self.hasAttributes = true
}
/// Interpolation with pre-built attributes.
///
/// ```swift
/// "\(userId, attributes: [Sensitivity.sensitive])"
/// ```
@inlinable
public mutating func appendInterpolation<T>(
_ value: T,
attributes: Logger.MetadataValueAttributes
) where T: CustomStringConvertible & Sendable {
self.output.append(value.description)
internal mutating func _mergeAttributes(_ attributes: Logger.MetadataValueAttributes) {
if self.hasAttributes {
self.attributes._merge(from: attributes)
} else {
@@ -387,29 +360,177 @@ extension Logger.MetadataValue: ExpressibleByStringInterpolation {
self.hasAttributes = true
}
/// Plain interpolation without attributes.
// MARK: Plain interpolation
// The four overloads below mirror `DefaultStringInterpolation`'s shape.
@inlinable
public mutating func appendInterpolation<T>(
public mutating func appendInterpolation<T: TextOutputStreamable & CustomStringConvertible>(
_ value: T
) where T: CustomStringConvertible & Sendable {
) {
value.write(to: &self.output)
}
@inlinable
public mutating func appendInterpolation<T: TextOutputStreamable>(
_ value: T
) {
value.write(to: &self.output)
}
@inlinable
public mutating func appendInterpolation<T: CustomStringConvertible>(
_ value: T
) {
self.output.append(value.description)
}
/// Fallback interpolation for types that are not `CustomStringConvertible`.
@inlinable
public mutating func appendInterpolation<T>(
_ value: T
) where T: Sendable {
self.output.append(String(describing: value))
}
/// Unconstrained fallback for non-`Sendable` types.
@inlinable
public mutating func appendInterpolation<T>(
_ value: T
) {
self.output.append(String(describing: value))
}
// MARK: Optional interpolation with a default
// The four overloads below mirror `DefaultStringInterpolation`'s `default:`
// shape so that `"\(optional, default: "none")"` keeps working when the
// literal is inferred as a `Logger.MetadataValue`.
@inlinable
public mutating func appendInterpolation<T: TextOutputStreamable & CustomStringConvertible>(
_ value: T?,
default defaultValue: @autoclosure () -> some StringProtocol
) {
if let value {
self.appendInterpolation(value)
} else {
self.output.append(String(defaultValue()))
}
}
@inlinable
public mutating func appendInterpolation<T: TextOutputStreamable>(
_ value: T?,
default defaultValue: @autoclosure () -> some StringProtocol
) {
if let value {
self.appendInterpolation(value)
} else {
self.output.append(String(defaultValue()))
}
}
@inlinable
public mutating func appendInterpolation<T: CustomStringConvertible>(
_ value: T?,
default defaultValue: @autoclosure () -> some StringProtocol
) {
if let value {
self.appendInterpolation(value)
} else {
self.output.append(String(defaultValue()))
}
}
@inlinable
public mutating func appendInterpolation<T>(
_ value: T?,
default defaultValue: @autoclosure () -> some StringProtocol
) {
if let value {
self.appendInterpolation(value)
} else {
self.output.append(String(defaultValue()))
}
}
// MARK: Interpolation with a custom attributes closure
//
// ```swift
// "\(userId, attributes: { $0[MyAttribute.self] = .flagged })"
// ```
@inlinable
public mutating func appendInterpolation<T: TextOutputStreamable & CustomStringConvertible>(
_ value: T,
attributes: @Sendable (inout Logger.MetadataValueAttributes) -> Void
) {
value.write(to: &self.output)
attributes(&self.attributes)
self.hasAttributes = true
}
@inlinable
public mutating func appendInterpolation<T: TextOutputStreamable>(
_ value: T,
attributes: @Sendable (inout Logger.MetadataValueAttributes) -> Void
) {
value.write(to: &self.output)
attributes(&self.attributes)
self.hasAttributes = true
}
@inlinable
public mutating func appendInterpolation<T: CustomStringConvertible>(
_ value: T,
attributes: @Sendable (inout Logger.MetadataValueAttributes) -> Void
) {
self.output.append(value.description)
attributes(&self.attributes)
self.hasAttributes = true
}
@inlinable
public mutating func appendInterpolation<T>(
_ value: T,
attributes: @Sendable (inout Logger.MetadataValueAttributes) -> Void
) {
self.output.append(String(describing: value))
attributes(&self.attributes)
self.hasAttributes = true
}
// MARK: Interpolation with pre-built attributes
//
// ```swift
// "\(userId, attributes: [Sensitivity.sensitive])"
// ```
@inlinable
public mutating func appendInterpolation<T: TextOutputStreamable & CustomStringConvertible>(
_ value: T,
attributes: Logger.MetadataValueAttributes
) {
value.write(to: &self.output)
self._mergeAttributes(attributes)
}
@inlinable
public mutating func appendInterpolation<T: TextOutputStreamable>(
_ value: T,
attributes: Logger.MetadataValueAttributes
) {
value.write(to: &self.output)
self._mergeAttributes(attributes)
}
@inlinable
public mutating func appendInterpolation<T: CustomStringConvertible>(
_ value: T,
attributes: Logger.MetadataValueAttributes
) {
self.output.append(value.description)
self._mergeAttributes(attributes)
}
@inlinable
public mutating func appendInterpolation<T>(
_ value: T,
attributes: Logger.MetadataValueAttributes
) {
self.output.append(String(describing: value))
self._mergeAttributes(attributes)
}
}
/// Creates a metadata value from string interpolation.
@@ -0,0 +1,349 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Logging API open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift Logging API project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import Logging
import Testing
// MARK: - Helper types covering each overload
/// `TextOutputStreamable & CustomStringConvertible` — exercises the most-specific overload.
private struct StreamingAndDescribing: TextOutputStreamable, CustomStringConvertible {
let value: Int
var description: String { "SD(\(self.value))" }
func write<Target: TextOutputStream>(to target: inout Target) {
target.write(self.description)
}
}
/// `TextOutputStreamable` only — exercises the TOS-only overload. (Rare in practice.)
private struct StreamingOnly: TextOutputStreamable {
let value: Int
func write<Target: TextOutputStream>(to target: inout Target) {
target.write("SO(\(self.value))")
}
}
/// `CustomStringConvertible` only — exercises the CSC-only overload.
private struct DescribingOnly: CustomStringConvertible {
let value: Int
var description: String { "DO(\(self.value))" }
}
/// Neither TOS nor CSC — exercises the unconstrained fallback.
private struct PlainStruct {
let value: Int
}
/// Non-`Sendable` reference type — exercises the unconstrained overload and verifies
/// that the constraint relaxation in `appendInterpolation` accepts non-`Sendable` arguments.
private final class NonSendableBox {
var value: Int
init(_ value: Int) { self.value = value }
}
@Suite("MetadataValue String Interpolation")
struct MetadataValueInterpolationTests {
// MARK: - Plain interpolation by overload overload
@Test("String interpolates via TOS & CSC overload")
func plainString() {
let value: Logger.MetadataValue = "hello \("world")"
#expect(value.description == "hello world")
}
@Test("Int interpolates via TOS & CSC overload")
func plainInt() {
let value: Logger.MetadataValue = "n=\(42)"
#expect(value.description == "n=42")
}
@Test("Double interpolates via TOS & CSC overload")
func plainDouble() {
let value: Logger.MetadataValue = "x=\(1.5)"
#expect(value.description == "x=1.5")
}
@Test("Substring interpolates via TOS & CSC overload")
func plainSubstring() {
let s = "hello world"
let value: Logger.MetadataValue = "\(s.dropFirst(6))"
#expect(value.description == "world")
}
@Test("Custom TOS & CSC type uses the most-specific overload")
func plainStreamingAndDescribing() {
let value: Logger.MetadataValue = "\(StreamingAndDescribing(value: 7))"
#expect(value.description == "SD(7)")
}
@Test("TOS-only type interpolates via the TOS overload")
func plainStreamingOnly() {
let value: Logger.MetadataValue = "\(StreamingOnly(value: 9))"
#expect(value.description == "SO(9)")
}
@Test("CSC-only type interpolates via .description")
func plainDescribingOnly() {
let value: Logger.MetadataValue = "\(DescribingOnly(value: 3))"
#expect(value.description == "DO(3)")
}
@Test("Array (CSC, not TOS) interpolates via .description")
func plainArray() {
let value: Logger.MetadataValue = "\([1, 2, 3])"
#expect(value.description == "[1, 2, 3]")
}
@Test("Foundation.Data (CSC) interpolates via .description")
func plainData() {
let data = Data([0x41, 0x42])
let value: Logger.MetadataValue = "\(data)"
#expect(value.description == data.description)
}
@Test("Plain struct (no CSC, no TOS) falls through to String(describing:)")
func plainPlainStruct() {
let value: Logger.MetadataValue = "\(PlainStruct(value: 5))"
#expect(value.description == String(describing: PlainStruct(value: 5)))
}
@Test("Non-Sendable class falls through to String(describing:) and compiles")
func plainNonSendable() {
let box = NonSendableBox(11)
let value: Logger.MetadataValue = "\(box)"
#expect(value.description == String(describing: box))
}
// MARK: - Optional interpolation with a default
@Test("nil optional uses the default string")
func defaultNil() {
let value: Int? = nil
let result: Logger.MetadataValue = "\(value, default: "none")"
#expect(result.description == "none")
}
@Test("non-nil optional interpolates the wrapped value")
func defaultSome() {
let value: Int? = 42
let result: Logger.MetadataValue = "\(value, default: "none")"
#expect(result.description == "42")
}
@Test("nil optional of a TOS-only type uses the default")
func defaultNilStreamingOnly() {
let value: StreamingOnly? = nil
let result: Logger.MetadataValue = "\(value, default: "missing")"
#expect(result.description == "missing")
}
@Test("non-nil optional of a CSC-only type interpolates via .description")
func defaultSomeDescribingOnly() {
let value: DescribingOnly? = DescribingOnly(value: 3)
let result: Logger.MetadataValue = "\(value, default: "missing")"
#expect(result.description == "DO(3)")
}
@Test("nil optional of a non-CSC, non-TOS type uses the default")
func defaultNilUnconstrained() {
let value: PlainStruct? = nil
let result: Logger.MetadataValue = "\(value, default: "absent")"
#expect(result.description == "absent")
}
@Test("default accepts a Substring (some StringProtocol)")
func defaultSubstring() {
let value: Int? = nil
let fallback = "n/a value".dropFirst(4)
let result: Logger.MetadataValue = "\(value, default: fallback)"
#expect(result.description == "value")
}
@Test("default segment mixed with plain segments")
func defaultMultiSegment() {
let name: String? = nil
let result: Logger.MetadataValue = "user=\(name, default: "anon") id=\(42)"
#expect(result.description == "user=anon id=42")
}
@Test("default interpolation yields .string case")
func defaultYieldsStringCase() {
let value: Int? = nil
let result: Logger.MetadataValue = "\(value, default: "none")"
if case .string(let s) = result {
#expect(s == "none")
} else {
Issue.record("Expected .string case, got \(result)")
}
}
// MARK: - Overloaded return types
@Test("Overloaded function returning String/Data/[UInt8] disambiguates to String")
func overloadedReturnTypeAmbiguity() {
func someFunc() -> String { "from-string" }
func someFunc() -> Data { Data([0x00]) }
func someFunc() -> [UInt8] { [0xFF] }
// Without the TOS & CSC overload this would fail with "ambiguous use of 'someFunc()'".
// String wins because it's TOS & CSC; Data and [UInt8] are CSC-only.
let value: Logger.MetadataValue = "\(someFunc())"
#expect(value.description == "from-string")
}
// MARK: - Closure-attribute interpolation
@Test("Closure attributes attach to a TOS & CSC value")
func closureAttrsTOSAndCSC() {
let value: Logger.MetadataValue = "\(42, attributes: { $0[AttrA.self] = .a1 })"
#expect(value.description == "42")
#expect(value.attributes[AttrA.self] == .a1)
}
@Test("Closure attributes attach to a CSC-only value")
func closureAttrsCSC() {
let value: Logger.MetadataValue = "\(DescribingOnly(value: 1), attributes: { $0[AttrA.self] = .a1 })"
#expect(value.description == "DO(1)")
#expect(value.attributes[AttrA.self] == .a1)
}
@Test("Closure attributes attach to a TOS-only value")
func closureAttrsTOS() {
let value: Logger.MetadataValue = "\(StreamingOnly(value: 2), attributes: { $0[AttrA.self] = .a1 })"
#expect(value.description == "SO(2)")
#expect(value.attributes[AttrA.self] == .a1)
}
@Test("Closure attributes attach to a non-CSC, non-TOS value")
func closureAttrsUnconstrained() {
let s = PlainStruct(value: 4)
let value: Logger.MetadataValue = "\(s, attributes: { $0[AttrA.self] = .a1 })"
#expect(value.description == String(describing: s))
#expect(value.attributes[AttrA.self] == .a1)
}
@Test("Closure attributes accept a non-Sendable value")
func closureAttrsNonSendable() {
let box = NonSendableBox(13)
let value: Logger.MetadataValue = "\(box, attributes: { $0[AttrA.self] = .a1 })"
#expect(value.description == String(describing: box))
#expect(value.attributes[AttrA.self] == .a1)
}
// MARK: - Pre-built attribute interpolation
@Test("Pre-built attributes attach to a TOS & CSC value")
func valueAttrsTOSAndCSC() {
let value: Logger.MetadataValue = "\(42, attributes: [AttrA.a1, AttrB.b2])"
#expect(value.description == "42")
#expect(value.attributes[AttrA.self] == .a1)
#expect(value.attributes[AttrB.self] == .b2)
}
@Test("Pre-built attributes attach to a CSC-only value")
func valueAttrsCSC() {
let value: Logger.MetadataValue = "\(DescribingOnly(value: 8), attributes: [AttrA.a1])"
#expect(value.description == "DO(8)")
#expect(value.attributes[AttrA.self] == .a1)
}
@Test("Pre-built attributes attach to a non-CSC value")
func valueAttrsUnconstrained() {
let s = PlainStruct(value: 6)
let value: Logger.MetadataValue = "\(s, attributes: [AttrA.a1])"
#expect(value.description == String(describing: s))
#expect(value.attributes[AttrA.self] == .a1)
}
@Test("Pre-built attributes accept a non-Sendable value")
func valueAttrsNonSendable() {
let box = NonSendableBox(17)
let value: Logger.MetadataValue = "\(box, attributes: [AttrA.a1])"
#expect(value.description == String(describing: box))
#expect(value.attributes[AttrA.self] == .a1)
}
@Test("Empty attributes still produce an attributed result")
func valueAttrsEmpty() {
let value: Logger.MetadataValue = "\(42, attributes: Logger.MetadataValueAttributes())"
#expect(value.description == "42")
// No attribute keys are set, but the carrier exists.
#expect(value.attributes[AttrA.self] == nil)
}
// MARK: - Multi-segment interpolation
@Test("Plain + plain segments concatenate without attributes")
func multiPlain() {
let value: Logger.MetadataValue = "user=\("alice") id=\(42)"
#expect(value.description == "user=alice id=42")
#expect(value.attributes[AttrA.self] == nil)
}
@Test("Plain + attributed segments produce attributed result")
func multiPlainAndAttributed() {
let value: Logger.MetadataValue = "user=\("alice") action=\("login", attributes: [AttrA.a1])"
#expect(value.description == "user=alice action=login")
#expect(value.attributes[AttrA.self] == .a1)
}
@Test("Two attributed segments merge their attributes")
func multiAttributedMerge() {
let value: Logger.MetadataValue = "\("a", attributes: [AttrA.a1])-\("b", attributes: [AttrB.b2])"
#expect(value.description == "a-b")
#expect(value.attributes[AttrA.self] == .a1)
#expect(value.attributes[AttrB.self] == .b2)
}
@Test("Closure-attributes and value-attributes segments merge")
func multiAttributedMixed() {
let value: Logger.MetadataValue =
"\("x", attributes: { $0[AttrA.self] = .a1 })-\("y", attributes: [AttrB.b2])"
#expect(value.description == "x-y")
#expect(value.attributes[AttrA.self] == .a1)
#expect(value.attributes[AttrB.self] == .b2)
}
@Test("Conflicting attribute keys: later segment wins")
func multiAttributedConflict() {
let value: Logger.MetadataValue =
"\("a", attributes: [AttrA.a1])-\("b", attributes: [AttrA.a2])"
#expect(value.description == "a-b")
#expect(value.attributes[AttrA.self] == .a2)
}
// MARK: - Result-case discrimination
@Test("Plain interpolation yields .string case")
func plainYieldsStringCase() {
let value: Logger.MetadataValue = "\(42)"
if case .string(let s) = value {
#expect(s == "42")
} else {
Issue.record("Expected .string case, got \(value)")
}
}
@Test("Attributed interpolation yields .stringConvertible case")
func attributedYieldsStringConvertibleCase() {
let value: Logger.MetadataValue = "\(42, attributes: [AttrA.a1])"
if case .stringConvertible = value {
// ok
} else {
Issue.record("Expected .stringConvertible case, got \(value)")
}
}
}