Giter VIP home page Giter VIP logo

mappingcoder's Introduction

Platform Platform GitHub release (latest by date) GitHub license Swift version GitHub all releases

English | δΈ­ζ–‡

πŸ§‘πŸΌβ€πŸ’» An Xcode Source Editor extension for ObjectMapper user to convert JSON into Swift code.

⏬ Download

Features

  • Convert JSON to Mappable
  • Convert JSON to ImmutableMappable
  • Auto Complete Mapping Methods
    • Custom mapping with attribute @map
  • Support converting nested type
  • Use lower camel case for property names
  • Use Int64 for property named xxx(ID|Id|id)

Install

  • macOS 10.15+
  • Drag MappingCoder.app into Applications folder.
  • Open it. The extension will be installed on Xcode when the app is opened for the first time.
  • In System Preferences > Extensions > Xcode Source Editor, selecting MappingCoder to activate it.

Uninstall

Moving MappingCoder.app to Trash.

Killing Xcode if there is a pop up showing "some of its extensions are in use".

Usage

In Xcode, selecting JSON or Class/Struct Declaration. And choosing Editor > MappingCoder > ... to use.

Examples

Convert JSON to Mappable

Convert JSON to ImmutableMappable

Auto Complete Mapping Methods

Sometimes, there is no need to convert whole JSON to Swift code. So you can simply define properties and use Auto Complete Mapping Methods to generate init(map:) & mapping(map:). If you want to customize the mapping, attribute @map() is provided to determine key & default for each property.

Here is the declaration of @map in Swift Style.

@map(key: String? = nil, default: Any? = nil)

You can use it like this.

// Typing your declaration with @map in line comment

struct Person: ImmutableMappable {

    // @map(key: "all_skills", default: [])
    let skills: [Any]
    // @map(key: "user-name", default: "")
    let name: String
    // @map(default: [:])
    let profile: [String : Any]
    // @map(key: "math score")
    let mathScore: Int
}

// Run Auto Complete Mapping Methods

struct Person: ImmutableMappable {

    // @map(key: "all_skills", default: [])
    let skills: [Any]
    // @map(key: "user-name", default: "")
    let name: String
    // @map(default: [:])
    let profile: [String : Any]
    // @map(key: "math score")
    let mathScore: Int

    init(map: Map) throws {
        skills = (try? map.value("all_skills")) ?? []
        name = (try? map.value("user-name")) ?? ""
        profile = (try? map.value("profile")) ?? [:]
        mathScore = (try? map.value("math score")) ?? <#defaultValue#>
    }

    func mapping(map: Map) {
        skills >>> map["all_skills"]
        name >>> map["user-name"]
        profile >>> map["profile"]
        mathScore >>> map["math score"]
    }
}

This feature is powered by SwiftSyntax.

Use Lower Camel Case & Int64

MappingCoder names property using lower camel case automatically. And also, defines property named xxx(ID|Id|id) as Int64.

//{
//    "user_name": "jack",
//    "user-id": 123456789
//}

class <#name#>: Mappable {

    var userId: Int64 = <#defaultValue#>
    var userName: String = <#defaultValue#>

    required init?(map: Map) {}

    func mapping(map: Map) {
        userId <- map["user-id"]
        userName <- map["user_name"]
    }
}

Settings

In Xcode, choosing Editor > MappingCoder > Settings... to open Settings.

Inspired By

License

MIT

mappingcoder's People

Contributors

wgy6055 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.