Git Product home page Git Product logo

Comments (15)

garrison-henkle avatar garrison-henkle commented on August 22, 2024 1

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.

spit4520 avatar spit4520 commented on August 22, 2024 1

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.

ikaio avatar ikaio commented on August 22, 2024

the data db.Select returns is already the result property that you are looking for.

from surrealdb.go.

jub0t avatar jub0t commented on August 22, 2024

the data db.Select returns is already the result property that you are looking for.

How do I access the result property.

from surrealdb.go.

garrison-henkle avatar garrison-henkle commented on August 22, 2024

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.

jub0t avatar jub0t commented on August 22, 2024

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().

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.

garrison-henkle avatar garrison-henkle commented on August 22, 2024

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.

jub0t avatar jub0t commented on August 22, 2024

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.

garrison-henkle avatar garrison-henkle commented on August 22, 2024

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.

jub0t avatar jub0t commented on August 22, 2024

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.

What json tags does it need(This is my first golang project)?

from surrealdb.go.

garrison-henkle avatar garrison-henkle commented on August 22, 2024

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.

jub0t avatar jub0t commented on August 22, 2024

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.

jub0t avatar jub0t commented on August 22, 2024

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.

nxy7 avatar nxy7 commented on August 22, 2024

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.
image

from surrealdb.go.

nxy7 avatar nxy7 commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.