قالب وردپرس درنا توس
Home / IOS Development / # 295: Build an API Client with Sourcery Key / Value Annotations 🔮 – Little Bites of Cocoa

# 295: Build an API Client with Sourcery Key / Value Annotations 🔮 – Little Bites of Cocoa



Subjects

Topics

In Bite # 294, we learned about annotations in Sourcery. They are a great way to create extra metadata on our types when they access them in Sourcerys .stencil painters.

Today we take a look at one of the most powerful features of annotations in Sourcery, Key / Value Annotations .

We will explore these by building a fictional API client powered by a single Swift Enum . Let us begin!

First, we must make our enum . Then we will define some issues to represent each HTTP endpoint that we will be able to call. For us, these are a classic set of "get all" "get a" "create" "change" and "delete"

: ] enum SpaceshipAPI {
case ship
case ship ( shipID : Int )
case ship : shipID : Int ship : Ship )
case case

: Ship ] )
}

Long-term readers can recognize this enum technique from Bites as # 93 or # 1

50. [19659000] ] Here's where magic begins though.

Sourcery Key / Value Annotations Similar to Notes . Instead of a simple tag, they allow us to annotate types, enums, enum cases, etc with a set of names and values.

Key / Value Annotations Work Everywhere As Regularly Notes do. We can annotate types, enums, enum cases and more.

They look like this:

  /// sourcery: key = value 
  /// sourcery: anotherKey = someOtherValue 

They may also be listed in a single line:

  /// sourcery: key = value, anotherKey = someOtherValue 

Values ​​can contain some basic types:

  /// sourcery: maxSpeed ​​= 1500, hasHyperdrive = true 
  / // sourcery: codename = "blacksaber" [19659053] Ok, return to our API client. Now that we know, we can add simple metadata like these to the things in our code, let's use this technique to describe each API endpoint.    enum    RomskipAPI    {
      /// sourcery: method = "GET", path = "/ spaceships" 
      /// sourcery: timeout = 5 
      case    ship 
      ... 
} 

Nice! Normally we can put method and lane into essentially a large switch statement change to itself and returns the correct method or path .

With Sourcery Key / Value Annotations we can still let Sourcery generate all of these statements for us. Let's create a SpaceshipsAPI + Properties.stencil template that expands our enum generates calculated properties :

     SpaceshipsAPI    {19659016] 
    public    var    path :    String      {
      breaks ~~ POS = HEADCOMP       {[19659091] { ]%    to    c    in    type .   SpacehipsAPI .   cases  % } 
        {% [19659082] if    c .   markings .   lane  % } 
        case  .   {{19659018] c [19659092]   name  }}:    return    "{{c.annotations.path}}" 
        {%    ] ] ] 
      {%    end for  % } 
      standard :    return    null 
    } 
  } 
   //. .. etc 
} 

Success! Now we can work at a much higher level. We can update our comments and never need to edit boring swap sentences.

We drive sourcery, which creates / updates RomskipAPI + Properties.generated.swift fills it with all the beautiful switches.

Finally, we can use our new properties as follows:

    print   (  RomskipAPI .   ship .   path  ] 

We can now add rich metadata to just about anything in our Swift code, then use it for metaprogram new Swift code that we can use in our project .

Neat!


Source link