Swiftpack.co -  banjun/SwiftSparql as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
banjun/SwiftSparql
SPARQL 1.1 query generator and results json format decoder (Decodable)
.package(url: "https://github.com/banjun/SwiftSparql.git", from: "0.11.0")

SwiftSparql

CI verbgen Version License Platform

SwiftSparql includes:

  • SPARQL query generator
  • SPARQL response parser (SPARQL 1.1 Query Results JSON Format (application/sparql-results+json))
  • Swift code generator verbgen for significant verbs in an endpoint

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

CocoaPods

SwiftSparql is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftSparql'

Swift Package Manager

Step by step:

mkdir sparql
cd sparql
swift package init --type executable

Edit generated Package.swift:

// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "sparql",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/banjun/SwiftSparql", from: "0.2.0"),
    ],
    targets: [
        .target(
            name: "sparql",
            dependencies: ["SwiftSparql"])
    ]
)
swift package generate-xcodeproj
open sparql.xcodeproj

Edit main.swift & Run with macOS destination.

import Foundation
import SwiftSparql
:

For example:

// construct query
let q = SelectQuery(
    where: WhereClause(patterns:
        subject(Var("s"))
            .rdfTypeIsImasIdol() // in the followings, verbs for this type can be auto-completed
            .imasType(is: "Cu")
            .foafAge(is: 17)
            .schemaName(is: Var("name"))
            .schemaHeight(is: Var("height"))
            .triples),
    order: [.desc(v: Var("height"))])

// capture results by Codable type
struct Idol: Codable {
    var name: String
    var height: Double
}

// request query to the endpoint URL
Request(endpoint: URL(string: "https://sparql.crssnky.xyz/spql/imas/query")!, select: q)
    .fetch()
    .onSuccess { (idols: [Idol]) in // Codable type annotation for being decoded by fetch()
        idols.forEach { idol in
            print("\(idol.name) (\(idol.height)cm)")
        }
        exit(0)
    }.onFailure {fatalError(String(describing: $0))}

dispatchMain()

Special Thanks

im@sparql dataset & its endpoint: https://github.com/imas/imasparql

License

SwiftSparql is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 4
Last commit: 2 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

0.11.0
13 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API