Swiftpack.co - banjun/SwiftSparql as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by banjun.
banjun/SwiftSparql 0.12.0
SPARQL 1.1 query generator and results json format decoder (Decodable)
⭐️ 8
🕓 1 year ago
.package(url: "https://github.com/banjun/SwiftSparql.git", from: "0.12.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 SwiftSparqlCLI
cd SwiftSparqlCLI
swift package init --type executable

Edit generated Package.swift:

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

import PackageDescription

let package = Package(
    name: "SwiftSparqlCLI",
    platforms: [.macOS("12.0")],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/banjun/SwiftSparql", from: "0.12.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .executableTarget(
            name: "SwiftSparqlCLI",
            dependencies: ["SwiftSparql"]),
    ]
)

Edit main.swift & execute by swift run.

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
}

Task {
    // request query to the endpoint URL
    // Codable type annotation for being decoded by fetch()
    let idols: [Idol] = try! await Request(endpoint: URL(string: "https://sparql.crssnky.xyz/spql/imas/query")!, select: q).fetch()
    idols.forEach { idol in
        print("\(idol.name) (\(idol.height)cm)")
    }
    exit(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: 8
Last commit: 17 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

0.12.0
1 year ago

async/await

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