sparql

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2021 License: MIT Imports: 7 Imported by: 0

README

GoDoc Go Report Card

sparql

Package sparql provides a series of parsers for turning SPARQL JSON (mime type: application/sparql-results+json only- this package doesn't touch xml) into useful go data types.

Within this package there are two subpackages:

  1. github.com/anglo-korean/sparql/bank - a sparql 'query bank' (though realistically I guess this may work for other well-formed data) with text/template support
  2. github.com/anglo-korean/sparql/repo - a sparql respository client, with various helpers such as auth, and caching

This package is simple to use, stable, and relatively quick. It accepts some json, in string, []byte, or io.Reader form, and returns some rdf terms:

package main

import (
    "fmt"

    "github.com/anglo-korean/sparql"
)

var data = `{"head":{"vars":["item","itemLabel"]},"results":{"bindings":[{"item":{"type":"uri","value":"[http://www.wikidata.org/entity/Q378619](http://www.wikidata.org/entity/Q378619)"},"itemLabel":{"xml:lang":"en","type":"literal","value":"CC"}},{"item":{"type":"uri","value":"[http://www.wikidata.org/entity/Q498787](http://www.wikidata.org/entity/Q498787)"},"itemLabel":{"xml:lang":"en","type":"literal","value":"Muezza"}}]}}`

func main() {
    res, err := sparql.ParseString(data)
    if err != nil {
        return
    }

    fmt.Printf("%!(NOVERB)v\n", res.Solutions())
}

Or, to query data using the provided client:

package main

import (
    "fmt"

    "github.com/anglo-korean/sparql/repo"
)

func main() {
    client, err := repo.New("[https://example.com](https://example.com)")
    if err != nil {
        return
    }

    res, err := client.Query("SELECT * WHERE { ?s ?p ?o } LIMIT 1")
    if err != nil {
        return
    }

    fmt.Printf("%!(NOVERB)v\n", res.Solutions())
}

Note that the client provided in repo parses returned json autiomatically. (Further documentation for both the repo client and the query bank may be found in those specific packages)

Parsed results are parsed into a sparql.Results type, which contains two functions used for accessing data:

  1. res.Bindings() -> map[string][]rdf.Term
  2. res.Solutions() -> []map[string]rdf.Term

An example of working with this data may be found in the examples directory

Sub Packages

  • bank: Package bank provides a query bank for sparql queries.

  • repo: Package repo provides a simple http client for interacting with sparql endpoints.


Readme created from Go doc with goreadme

Documentation

Overview

Package sparql provides a series of parsers for turning SPARQL JSON (mime type: application/sparql-results+json only- this package doesn't touch xml) into useful go data types.

Within this package there are two subpackages:

1. github.com/anglo-korean/sparql/bank - a sparql 'query bank' (though realistically I guess this may work for other well-formed data) with `text/template` support 2. github.com/anglo-korean/sparql/repo - a sparql respository client, with various helpers such as auth, and caching

This package is simple to use, stable, and relatively quick. It accepts some json, in `string`, `[]byte`, or `io.Reader` form, and returns some rdf terms:

package main

import (
    "fmt"

    "github.com/anglo-korean/sparql"
)

var data = `{"head":{"vars":["item","itemLabel"]},"results":{"bindings":[{"item":{"type":"uri","value":"http://www.wikidata.org/entity/Q378619"},"itemLabel":{"xml:lang":"en","type":"literal","value":"CC"}},{"item":{"type":"uri","value":"http://www.wikidata.org/entity/Q498787"},"itemLabel":{"xml:lang":"en","type":"literal","value":"Muezza"}}]}}`

func main() {
    res, err := sparql.ParseString(data)
    if err != nil {
        return
    }

    fmt.Printf("%v\n", res.Solutions())
}

Or, to query data using the provided client:

package main

import (
    "fmt"

    "github.com/anglo-korean/sparql/repo"
)

func main() {
    client, err := repo.New("https://example.com")
    if err != nil {
        return
    }

    res, err := client.Query("SELECT * WHERE { ?s ?p ?o } LIMIT 1")
    if err != nil {
        return
    }

    fmt.Printf("%v\n", res.Solutions())
}

Note that the client provided in repo parses returned json autiomatically. (Further documentation for both the repo client and the query bank may be found in those specific packages)

Parsed results are parsed into a sparql.Results type, which contains two functions used for accessing data:

1. `res.Bindings()` -> `map[string][]rdf.Term` 2. `res.Solutions()` -> `[]map[string]rdf.Term`

An example of working with this data may be found in the examples directory

Index

Constants

This section is empty.

Variables

View Source
var DateFormat = time.RFC3339

DateFormat is the expected layout of the xsd:DateTime values. You can override it if your triple store uses a different layout.

Functions

This section is empty.

Types

type Results

type Results struct {
	Head    header
	Results results
}

Results holds the parsed results of a application/sparql-results+json response.

func Parse

func Parse(r io.Reader) (res *Results, err error)

Parse takes an application/sparql-results+json response as an io.Reader (like from an http.Response.Body) and parses it into a Results struct

func ParseBytes

func ParseBytes(b []byte) (*Results, error)

ParseBytes takes a bytes containing valid sparql-results+json and returns a set of Results

func ParseString

func ParseString(s string) (*Results, error)

ParseString takes a string containing valid sparql-results+json and returns a set of Results

func (*Results) Bindings

func (r *Results) Bindings() map[string][]rdf.Term

Bindings returns a map of the bound variables in the SPARQL response, where each variable points to one or more RDF terms.

func (*Results) Solutions

func (r *Results) Solutions() []map[string]rdf.Term

Solutions returns a slice of the query solutions, each containing a map of all bindings to RDF terms.

Directories

Path Synopsis
Package bank provides a query bank for sparql queries.
Package bank provides a query bank for sparql queries.
Package repo provides a simple http client for interacting with sparql endpoints.
Package repo provides a simple http client for interacting with sparql endpoints.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL