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

Suggestion: use quicktype to generate code from data #5

Open
dvdsgl opened this issue Aug 1, 2018 · 0 comments
Open

Suggestion: use quicktype to generate code from data #5

dvdsgl opened this issue Aug 1, 2018 · 0 comments

Comments

@dvdsgl
Copy link

dvdsgl commented Aug 1, 2018

Just wanted to make you aware of quicktype, which can generate code from cli.fyi data for using the data in many programming languages.

Generate Swift for BTC data

~ curl cli.fyi/BTC | quicktype -l swift             
// To parse the JSON, add this file to your project and do:
//
//   let topLevel = try TopLevel(json)

import Foundation

struct TopLevel: Codable {
    let type: String
    let data: [String: Double]
}

// MARK: Convenience initializers

extension TopLevel {
    init(data: Data) throws {
        self = try JSONDecoder().decode(TopLevel.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func jsonData() throws -> Data {
        return try JSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

Generate Go for domain info

~ curl cli.fyi/github.com | quicktype -l go --top-level DomainInfo
// To parse and unparse this JSON data, add this code to your project and do:
//
//    domainInfo, err := UnmarshalDomainInfo(bytes)
//    bytes, err = domainInfo.Marshal()

package main

import "encoding/json"

func UnmarshalDomainInfo(data []byte) (DomainInfo, error) {
	var r DomainInfo
	err := json.Unmarshal(data, &r)
	return r, err
}

func (r *DomainInfo) Marshal() ([]byte, error) {
	return json.Marshal(r)
}

type DomainInfo struct {
	Type string `json:"type"`
	Data Data   `json:"data"`
}

type Data struct {
	DNS   []string `json:"dns"`  
	Whois []string `json:"whois"`
}

Generate C# classes for client info

~ curl cli.fyi/me | \
    quicktype -l csharp --features attributes-only --top-level ClientInfo --density dense \
      --namespace CLI.FYI
namespace CLI.FYI
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using J = Newtonsoft.Json.JsonPropertyAttribute;
    using R = Newtonsoft.Json.Required;
    using N = Newtonsoft.Json.NullValueHandling;

    public partial class ClientInfo
    {
        [J("type")] public string Type { get; set; }
        [J("data")] public Data Data { get; set; }  
    }

    public partial class Data
    {
        [J("iPAddress")]     public string IPAddress { get; set; }           
        [J("userAgent")]     public string UserAgent { get; set; }           
        [J("browser")]       public string Browser { get; set; }             
        [J("iPAddressInfo")] public IPAddressInfo IPAddressInfo { get; set; }
    }

    public partial class IPAddressInfo
    {
        [J("organisation")] public string Organisation { get; set; }
        [J("country")]      public string Country { get; set; }     
        [J("countryCode")]  public string CountryCode { get; set; } 
        [J("city")]         public string City { get; set; }        
        [J("continent")]    public string Continent { get; set; }   
        [J("latitude")]     public string Latitude { get; set; }    
        [J("longitude")]    public string Longitude { get; set; }   
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant