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

Add RequestModel Macro #9020

Closed
brunogama opened this issue Feb 1, 2025 · 2 comments
Closed

Add RequestModel Macro #9020

brunogama opened this issue Feb 1, 2025 · 2 comments
Labels
Add Package [Automatic PR]

Comments

@brunogama
Copy link

New Packages

https://github.com/brunogama/RequestModelMacro

Features

This library includes various Swift macros that can be used in your projects to improve code generation and reduce repetitive tasks:

  • Header and Body Macros: Use HeaderMacro.swift and BodyMacro.swift to generate consistent request headers and body structures for API calls.

  • Request Model Macro: RequestModelMacro.swift and RequestModelMacroInterface.swift facilitate the creation of standardized request models for APIs. This macro helps ensure that your request models are well-formed, consistent, and easy to maintain.

  • Diagnostics Support: Diagnostics.swift helps provide useful information about the health and correctness of the generated code, making debugging simpler and more effective.

  • Property Data Macro: PropertyData.swift offers an easy way to handle property generation for request models, ensuring consistency and reducing repetitive definitions.

Usage

Input

import RequestModelMacro

@RequestModel
struct ComplexRequest {
    @Header("Auth")
    var token: String
    
    @Header("Content-Type")
    var contentType: String
    
    @Body("data")
    var payload: String
    
    @Body("metadata")
    var info: String
}

Generated Code&

struct ComplexRequest {
    var token: String {
        get {
            headers.token
        }
    }

    var contentType: String {
        get {
            headers.contentType
        }
    }

    var payload: String {
        get {
            body.payload
        }
    }

    var info: String {
        get {
            body.info
        }
    }

    private struct Headers: Codable {
        let token: String
        let contentType: String

        enum CodingKeys: String, CodingKey {
            case token = "Auth"
            case contentType = "Content-Type"
        }
    }

    private struct Body: Codable {
        let payload: String
        let info: String

        enum CodingKeys: String, CodingKey {
            case payload = "data"
            case info = "metadata"
        }
    }

    var headersDictionary: [String: String] {
        do {
            let encoder = JSONEncoder()
            let data = try encoder.encode(headers)
            let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])

            if let dictionary = jsonObject as? [String: String] {
                return dictionary
            } else {
                print("Warning: Failed to cast headers dictionary to [String: String]")
                return [:]
            }
        } catch {
            assertionFailure("Warning: Failed to encode headers: \\(error)")
            return [:]
        }
    }

    var bodyDictionary: [String: Any] {
        do {
            let encoder = JSONEncoder()
            let data = try encoder.encode(body)
            let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])

            if let dictionary = jsonObject as? [String: Any] {
                return dictionary
            } else {
                print("Warning: Failed to cast body dictionary to [String: Any]")
                return [:]
            }
        } catch {
            assertionFailure("Warning: Failed to encode body: \\(error)")
            return [:]
        }
    }

    private var headers: Headers

    private var body: Body

    init(token: String, contentType: String, payload: String, info: String) {
        self.headers = Headers(token: token, contentType: contentType)
        self.body = Body(payload: payload, info: info)
    }
}
@brunogama brunogama added the Add Package [Automatic PR] label Feb 1, 2025
Copy link
Contributor

github-actions bot commented Feb 1, 2025

Thank you! We will approve and add these packages with #9021.

@finestructure
Copy link
Member

Added via #9027

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Add Package [Automatic PR]
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants