Qapi
API query in Golang, query your api like SQL
Install
go get -u github.com/FerdinaKusumah/qapi
Query from JSON Object
package main
import (
"encoding/json"
"fmt"
"github.com/FerdinaKusumah/qapi"
"log"
)
func main() {
var (
err error
val []byte
records interface{}
query1 = `
select id from "https://jsonplaceholder.typicode.com/comments/1"
`
query2 = `
select id, name, email from "https://jsonplaceholder.typicode.com/comments/1"
`
query3 = `
select name, email from "https://jsonplaceholder.typicode.com/comments/1"
where email = 'Eliseo@gardner.biz'
`
query4 = `
select name, email from "https://jsonplaceholder.typicode.com/comments"
where email like '.biz'
`
query5 = `
select name, email from "https://jsonplaceholder.typicode.com/comments"
where email != 'Eliseo@gardner.biz'
`
)
if records, err = qapi.Fetch(query1); err != nil {
log.Fatalf(`unable to fetch sql: %s`, err)
}
val, _ = json.Marshal(records)
fmt.Println(string(val)) // {"id":1}
if records, err = qapi.Fetch(query2); err != nil {
log.Fatalf(`unable to fetch sql: %s`, err)
}
val, _ = json.Marshal(records)
fmt.Println(string(val)) // {"email":"eliseo@gardner.biz","id":1,"name":"id labore ex et quam laborum"}
if records, err = qapi.Fetch(query3); err != nil {
log.Fatalf(`unable to fetch sql: %s`, err)
}
val, _ = json.Marshal(records)
fmt.Println(string(val)) // {"email":"eliseo@gardner.biz","name":"id labore ex et quam laborum"}
if records, err = qapi.Fetch(query4); err != nil {
log.Fatalf(`unable to fetch sql: %s`, err)
}
val, _ = json.Marshal(records)
fmt.Println(string(val)) // {"email":"eliseo@gardner.biz","name":"id labore ex et quam laborum"}
if records, err = qapi.Fetch(query5); err != nil {
log.Fatalf(`unable to fetch sql: %s`, err)
}
val, _ = json.Marshal(records)
fmt.Println(string(val)) // [{"name":"id labore ex et quam laborum"},{"email":"eliseo@gardner.biz"}, ... ]
}