Comments (15)
I took your code from above and made it work on my local machine: https://gist.github.com/garrison-henkle/0687a49b1e2e910bbd0c16816a164e10
I created one record as a test:
create apps:test1 set host = "google.com"
[{"time":"4.254208ms","status":"OK","result":[{"host":"google.com","id":"apps:test1"}]}]
And got:
[{0 google.com 0 []}]
Just add the json tags and export the rest of the fields of Application and it will work.
from surrealdb.go.
Thank you 🥳 your tag is very helpful, ran into the same issues with Select; the read me at a bare minimum needs to be updated ASAP to fix minor acid traps so people can get started quickly. Nothing exhaustive, just "hey do this, that, this works, this doesn't, generally do this, this common error means this" as opposed to taking apart the Node and Python libraries and docs by hand to figure out whats going on
from surrealdb.go.
the data db.Select
returns is already the result
property that you are looking for.
from surrealdb.go.
the data
db.Select
returns is already theresult
property that you are looking for.
How do I access the result
property.
from surrealdb.go.
Try:
data, d_err := db.Select("apps")
if d_err != nil {
fmt.Print(d_err)
}
var apps Results
err := surrealdb.Unmarshal(data, &apps)
The surrealdb.Unmarshal()
function takes the interface{}
return of db.Select()
, db.Update()
, etc. db.Query()
works the same way, just replace surrealdb.Unmarshal()
with surrealdb.UnmarshalRaw()
.
data
is the result
property from the the jsons you have in the terminal, it is just a nested mess of []interface{}
and map[string]interface{}
in Go that the unmarshal functions will unravel for you.
from surrealdb.go.
The
surrealdb.Unmarshal()
function takes theinterface{}
return ofdb.Select()
,db.Update()
, etc.db.Query()
works the same way, just replacesurrealdb.Unmarshal()
withsurrealdb.UnmarshalRaw()
.
module proxybox
go 1.19
require github.com/surrealdb/surrealdb.go v0.1.1
require github.com/gorilla/websocket v1.5.0 // indirect
import (
surreal "github.com/surrealdb/surrealdb.go"
)
.\main.go:85:17: undefined: surrealdb.Unmarshal
from surrealdb.go.
Oh I just checked and that v0.1.1 tag is several weeks old, while the unmarshal changes were made a few days ago. Someone with access to the repo needs to make a new tag.
To get the new version you'll have to make a new fork, tag it, and then import that version instead. If you don't want to do that yourself, the newest tag on my repo contains the changes. It does have an additional workaround for a non-driver bug, though, so you'll have to do it yourself if you don't want that. The fix just disables encoding of <
and >
to utf8 to allow for <
, >
, <-
and ->
operators to be used, so it is actually better than the official version if anything.
Or I guess as an alternative, you can just copy and paste the unmarshal code from this repo into your code and replace the map[string]interface{}
everywhere with interface{}
from surrealdb.go.
Oh I just checked and that v0.1.1 tag is several weeks old, while the unmarshal changes were made a few days ago. Someone with access to the repo needs to make a new tag.
package main
import (
"fmt"
"net/http"
surreal "github.com/garrison-henkle/surrealdb.go"
)
type Config struct {
ns string
db string
user string
scheme string
pass string
host string
port int
}
type Application struct {
external_port int16
host string
id string
internal_port int
name string
scheme string
server_names []string
}
type Results struct {
ID interface{} `json:"id" msgpack:"id"`
Result []interface{} `json:"result" msgpack:"result"`
}
func get_configs() Config {
configs := Config{
user: "root",
pass: "root",
db: "test",
ns: "test",
host: "127.0.0.1",
scheme: "ws",
port: 8000,
}
return configs
}
func restart(w http.ResponseWriter, req *http.Request) {
configs := get_configs()
url := configs.scheme + "://" + configs.host + ":" + fmt.Sprint(configs.port)
db, db_err := surreal.New(url + "/rpc")
if db_err != nil {
fmt.Print(db_err)
}
_, use_err := db.Use(configs.ns, configs.db)
if use_err != nil {
fmt.Print(use_err)
}
data, d_err := db.Select("apps")
if d_err != nil {
fmt.Print(d_err)
}
fmt.Println(data)
var apps Results
err := surreal.Unmarshal(data, &apps)
if err != nil {
fmt.Print(d_err)
}
fmt.Println(apps.Result)
for i, s := range apps.Result {
fmt.Println(i, s)
}
}
func main() {
http.HandleFunc("/restart", restart)
http.ListenAndServe(":1000", nil)
}
I used your repo. same response, same results.
from surrealdb.go.
You need to call db.Signin
before db.Use
. var apps Results
should also be var apps Application
and Application
needs both its fields exported and json
tags.
from surrealdb.go.
You need to call
db.Signin
beforedb.Use
.var apps Results
should also bevar apps Application
andApplication
needs both its fields exported andjson
tags.
What json tags does it need(This is my first golang project)?
from surrealdb.go.
Basically Application
needs to be something like this:
type Application struct {
External_port int16 `json:"external_port"`
Host string `json:"host"`
Id string `json:"id"`
Internal_port int `json:"internal_port"`
Name string `json:"name"`
Scheme string `json:"scheme"`
Server_names []string `json:"server_names"`
}
The field names need to be capitalized so they can be exported (the json marshaller can't use non-exported fields), so the json meta data tags allow you to change the name to whatever your actual data looks like. I just used whatever my IDE autofilled above, so you may need to change them to match the case and format of the field names. The tags aren't necessary if you are fine with the first letter being capitalized when Application
is serialized.
from surrealdb.go.
Basically
Application
needs to be something like this:type Application struct { External_port int16 `json:"external_port"` Host string `json:"host"` Id string `json:"id"` Internal_port int `json:"internal_port"` Name string `json:"name"` Scheme string `json:"scheme"` Server_names []string `json:"server_names"` }
Doesn't work, still same results
from surrealdb.go.
I took your code from above and made it work on my local machine: https://gist.github.com/garrison-henkle/0687a49b1e2e910bbd0c16816a164e10
I created one record as a test:
create apps:test1 set host = "google.com" [{"time":"4.254208ms","status":"OK","result":[{"host":"google.com","id":"apps:test1"}]}]
And got:
[{0 google.com 0 []}]
Just add the json tags and export the rest of the fields of Application and it will work.
I'm waiting for the official Golang docs to come out.
from surrealdb.go.
In case anyone wants to use the latest changes in the repo (like surrealdb.Unmarchal) you can also download module as
go get github.com/surrealdb/surrealdb.go@main
.
Select doesn't work for me, I can create stuff, but for some reason select returns empty array. I don't even think it's an issue with unmarchaling because during debugging response from 'select' seems to be empty.
from surrealdb.go.
Nevermind, select does work but I was using it as
db.Select("select ticker, name from stock")
instead of
db.Select("stock")
Still figuring out how to filter results, but it should be easy now :P
from surrealdb.go.
Related Issues (20)
- Feature: context.Context Support HOT 6
- Documentation: working with record links HOT 4
- Feature: Move and test `unmarshalMapToStruct(data map[string]interface{}, outStruct interface{})` HOT 2
- Bug: Re-connection to the DB HOT 2
- Bug: Broken Tests for Fetch HOT 5
- Bug: Unhandled errors in websockets can lead to panics HOT 4
- Bug: `Question or idea` Button HOT 2
- surrealdb Auth undefined HOT 1
- Bug: unexpected EOF on TestSurrealDBSuite/gorilla/TestUpdate HOT 1
- Documentation: The README file example is not usable with the latest main branch, neither with the latest version HOT 1
- Feature: Replace Gorilla WS with Nhooyr WS HOT 7
- Bug: The session has expired error when connecting to version 1.3.0 HOT 4
- Bug: stack overflow error in gorilla websocket with db version 1.3.0 HOT 4
- Feature: surrealML for golang
- Documentation: Add Documentation for .Live() func & other stuff related to LIVE queries HOT 6
- Change function does not exist HOT 3
- Feature: Add CBOR support HOT 2
- Bug: UnmarshalRaw panics when unmarshalling object HOT 3
- Feature: UPSERT
- Example fails in Beta 11 - panic: sending request failed for method 'change': Method not found HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from surrealdb.go.