<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alejandro Martinez</title>
    <description>The latest articles on DEV Community by Alejandro Martinez (@alexito4).</description>
    <link>https://dev.to/alexito4</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F94733%2F262cc62c-d6d2-4d5c-b1d0-7352b0ef52a5.jpg</url>
      <title>DEV Community: Alejandro Martinez</title>
      <link>https://dev.to/alexito4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexito4"/>
    <language>en</language>
    <item>
      <title>Swift 4.2 CaseIterable enums and UISegmentedControl, a practical example</title>
      <dc:creator>Alejandro Martinez</dc:creator>
      <pubDate>Sat, 18 Aug 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/alexito4/swift-42-caseiterable-enums-and-uisegmentedcontrol-a-practical-example-113k</link>
      <guid>https://dev.to/alexito4/swift-42-caseiterable-enums-and-uisegmentedcontrol-a-practical-example-113k</guid>
      <description>&lt;p&gt;On 2014 I wrote a &lt;a href="http://alejandromp.com/blog/2014/07/24/swift-enums/"&gt;post&lt;/a&gt; about how to drive a UISegmentedControl with the new powerful Swift enums. Back then the major complain was that to get all the cases of the enum you had manually list the cases of the enum, which was tiresome and error prone.&lt;/p&gt;

&lt;p&gt;This post is a new version of the same idea but updated to &lt;strong&gt;Swift 4.2 and the new CaseIterable protocol.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CaseIterable
&lt;/h2&gt;

&lt;p&gt;This new protocol has been introduced by the proposal &lt;a href="https://github.com/apple/swift-evolution/blob/master/proposals/0194-derived-collection-of-enum-cases.md"&gt;SE-0194&lt;/a&gt;. It defines a protocol with a single requirement, a &lt;code&gt;allCases&lt;/code&gt; static variable that returns a collection with all possible values of the type.&lt;/p&gt;

&lt;p&gt;But the best thing is that the compiler &lt;strong&gt;automatically synthesises&lt;/strong&gt; the conformance for some simple &lt;code&gt;enum&lt;/code&gt;s.&lt;/p&gt;

&lt;h2&gt;
  
  
  Drive a UISegmentedControl
&lt;/h2&gt;

&lt;p&gt;Let's first take a look at how the implementation looked back in 2014.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum Size: Int, CustomStringConvertible {
    case S = 0
    case M
    case L
    case XL

    static func allValues() -&amp;gt; [String] {
        return [S, M, L, XL].map({$0.description})
    }

    static func count() -&amp;gt; Int {
        return allValues().count
    }

    public var description: String {
        switch self {
        case .S:
            return "S"
        case .M:
            return "M"
        case .L:
            return "L"
        case .XL:
            return "XL"
        }
    }
}

Size.count()
Size.allValues()

let segmented = UISegmentedControl(items: Size.allValues())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see how we need to implement a custom function and need to conform to &lt;code&gt;CustomStringConvertible&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the CustomStringConvertible it's a tradeoff. We had to pick between implementing a custom &lt;code&gt;rawValue&lt;/code&gt; or custom string.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now with Swift 4.2 we can ged rid of both custom implementations leaving our modernised enum like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum Size: Int, CaseIterable {
    case S = 0
    case M
    case L
    case XL
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With just this conformance we get access to an automatically generated &lt;code&gt;allCases&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;CustomStringConvertible&lt;/code&gt; can also be removed because Swift now provides better runtime information that allows String interpolation to access the name of the case.&lt;/p&gt;

&lt;p&gt;With these improvements we have way less code to maintain and we can still drive our &lt;code&gt;UISegmentedControl&lt;/code&gt; as before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UISegmentedControl(items: Size.allCases.map({ "\($0)" }))
...
let index = sender.selectedSegmentIndex
Size(rawValue: index)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As usual you can find the code for this post in a &lt;a href="http://alejandromp.com/static/SwiftCaseIterable.playground-f80eab7fe171a4b3be19052556223af6.zip"&gt;Playground&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you want to know more about &lt;code&gt;CaseIterable&lt;/code&gt; checkout my video on the topic: &lt;a href="https://youtu.be/c3W8-57Bdx8"&gt;Swift 4.2 - CaseIterable - Following Swift Evolution&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
    </item>
    <item>
      <title>Programming the Cloud with Pulumi and Typescript</title>
      <dc:creator>Alejandro Martinez</dc:creator>
      <pubDate>Sat, 04 Aug 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/alexito4/programming-the-cloud-with-pulumi-and-typescript-4hkf</link>
      <guid>https://dev.to/alexito4/programming-the-cloud-with-pulumi-and-typescript-4hkf</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was &lt;a href="http://alejandromp.com/blog/2018/08/04/programming-the-cloud-with-pulumi-and-typescript/"&gt;originally posted on my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Server side development and the managing of the infrastructure has gone a long way in the recent years. From scalable virtual machines easy to manage to containerisation with Docker and even &lt;em&gt;serverless&lt;/em&gt; functions with Amazon Lambda. But all of this advancements still require you to change your mindset, from programming your application to configuring the infrastructure.&lt;/p&gt;

&lt;p&gt;Even if it's easier than ever before it's still not frictionless, and when you just want to try a quick idea or work on a weekend project the last thing that you want is friction. That's why I've been so curious about &lt;a href="https://www.pulumi.com"&gt;Pulumi&lt;/a&gt;. I've been following &lt;a href="https://twitter.com/funcOfJoe"&gt;Joe Duffy&lt;/a&gt; for a while because of his interesting work in programming languages and his most recent work on &lt;strong&gt;Programming the Cloud&lt;/strong&gt; was more than promising.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pulumi provides a cloud native programming model to create containers, serverless functions, and cloud infrastructure, with all the benefits of immutable infrastructure, and real programming languages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is great. I'm a huge believer that some configuration should be done with real programming languages, Pulumi brings this idea to the cloud infrastructure.&lt;/p&gt;

&lt;p&gt;So this weekend I decided to give a try to an idea that I've been wanting to implement for a while, and using Pulumi seemed the quickest way to work on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Serverless
&lt;/h2&gt;

&lt;p&gt;For this idea I just need a couple of endpoints and a small database. I've recently worked with Amazon Lambda and DynamoDB in my work and I knew that Amazon provided a free tier of those services, so it seemed ideal for a weekend project.&lt;/p&gt;

&lt;p&gt;Even if serverless has many advantages you still need to setup quite a bit of stuff. Lambda functions, dynamo tables, user roles, policies, API gateways... it's not something you can just learn in 5 minutes. And luckly for me I had a couple of great mentors that helped me quickstart it at work, but still I'm not confident enough on all that setup to do it myself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pulumi
&lt;/h2&gt;

&lt;p&gt;Here's where Pulumi comes in. After installing the CLI and setting up your AWS credentials (this is the only part where you need to deal with AWS directly, and you will get tired of it just creating an account 😂 ) you can create a project and start working on your application.&lt;/p&gt;

&lt;p&gt;Pulumi programming model treats infrastructure resources as just objects in your code. So if you want an API endpoint you just create one in your code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const endpoint = new cloud.API("langs");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! That's everything you have to do to have a working API Gateway with Lambda. The best thing is that you can treat that object as the router of your webservice, like in any other web framework.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;endpoint.get("/test", async (req, res) =&amp;gt; {
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this you are configuring a "/test" endpoint in the API Gateway that you run the code that you add in the closure from a lambda function.&lt;/p&gt;

&lt;p&gt;And this is one other advantage of Pulumi. Because this code creates the infrastructure and has your application logic, you can easily communicate between different parts of your infrastructure just by accessing their variables and even capturing them in closures!&lt;/p&gt;

&lt;p&gt;This means that you can easily access DynamoDB tables from Lambdas just by calling a method on the table objects from inside the endpoint closure!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const endpoint = new cloud.API("langs");
const table = new cloud.Table("projectsTable", "project");
endpoint.get("/test", (req, res) =&amp;gt; {
    table.get({ 'project': "1" }).then(value =&amp;gt; {
        res.status(200).json(value)
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pulumi pulls thus off by serialising the objects captured in the lambda closure and passing them transparently for you at runtime.&lt;/p&gt;

&lt;p&gt;It is great that you don't have to worry about setting up the services you need or even how to pass data between them. You just focus on the product you want to create.&lt;/p&gt;

&lt;h2&gt;
  
  
  Powerful infrastructure configuration
&lt;/h2&gt;

&lt;p&gt;By now you have seen how cool being able to access infrastructure just by creating object is, specially for somebody like me that is not an expert in devops. But for the experts in setting up infrastructure Pulumi is also really awesome.&lt;/p&gt;

&lt;p&gt;First of all, you have all the power of a real programming language at your disposal. Conditionals, &lt;a href="http://blog.pulumi.com/program-the-cloud-with-12-pulumi-pearls#pearl-1"&gt;loops&lt;/a&gt;, functions... anything.&lt;/p&gt;

&lt;p&gt;Having all this power brings the reusing of infrastructure to the next level. You can easily &lt;a href="http://blog.pulumi.com/program-the-cloud-with-12-pulumi-pearls#pearl-2"&gt;create and distribute components&lt;/a&gt;, at the end of the day is just a library!&lt;/p&gt;

&lt;p&gt;To see how amazing this is I have a real world example. One of the things I'm still not convinced about Lambda is the cold starts. Lambda functions instances can be shut down and reused after some minutes of inactivity. This can add quite a bit of latency specially if you're hitting those endpoints in a mobile network. Usually it's not something that you need to worry about but sometimes is useful to have a system that keeps pinging the lambda to keep it warm.&lt;/p&gt;

&lt;p&gt;This setup can be quite convoluted but check out this post to see how you can build a &lt;a href="https://mikhail.io/2018/08/aws-lambda-warmer-as-pulumi-component/"&gt;reusable component to keep an AWS Lambda warm&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Just by using the component Pulumi will setup all the infra needed to keep it warm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const lambda = new mylibrary.WarmLambda("my-warm-function", { /* options */ }, handler);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Typescript
&lt;/h2&gt;

&lt;p&gt;The other thing that I didn't want to deal with was, you guessed it, Javascript. I'm well aware that the language is not that bad if you use all the modern tooling, but I'm still a Swift lover and I need my compiler 😘&lt;/p&gt;

&lt;p&gt;Luckily Pulumi supports Typescript which is one of the best languages out there in my opinion. The fact that you can write a mix of safe checked language with crazy javascript dynamism is a pretty cool accomplishment.&lt;/p&gt;

&lt;p&gt;And even if I love my types I don't miss them that much when dealing with a JSON API that I just need to grab a couple of values from, specially in a side project. In a more serious programming I would probably add typing and more safety to it but it's great that I don't have to do it just now.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Pulumi is great. I'm not an expert in infrastructure so this opinion goes in both ways. The fact that it allowed me to deploy all the services without having to check how to configure them is awesome. But because I'm not an expert about it there are probably some things that you could say are not ideal. Maybe. Probably.&lt;/p&gt;

&lt;p&gt;In any case the project is evolving quite quickly. They support different languages like Javascript/Typescript, Python and Go (I would love to see Swift included someday) and different cloud providers like AWS, Google Cloud, Azure, etc. And even if I focused in Serverless because that's what I'm working with, you can use Containers or any other type of infrastructure.&lt;/p&gt;

&lt;p&gt;If you're curious about any of this stuff don't doubt on checking it out, with a Free AWS account you can learn all of it for free ^^&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
