go grammes – how to get property from gremlin, not just the value with go grammes

huangapple go评论86阅读模式
英文:

go grammes - how to get property from gremlin, not just the value with go grammes

问题

你好,以下是翻译好的内容:

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/northwesternmutual/grammes"
)

func main() {
	client, err := grammes.DialWithWebSocket("ws://localhost:8182/gremlin")
	if err != nil {
		log.Fatalf("Connection error: %s", err.Error())
	}

	data, err := client.ExecuteQuery(g.V().Has("person", "name", "aksan").Values())
	if err != nil {
		log.Fatalf("Querying error: %s", err.Error())
	}

	var responseStruct []interface{}
	err = json.Unmarshal(data[0], &responseStruct)
	if err != nil {
		log.Fatalf("unmarshal error: %s", err.Error())
	}

	result := make(map[string]interface{})
	for i := 0; i < len(responseStruct); i += 2 {
		field := responseStruct[i].(string)
		value := responseStruct[i+1]
		result[field] = value
	}

	PrettyPrint(result, "result")
}

func PrettyPrint(data interface{}, prefix string) {
	prettyJSON, err := json.MarshalIndent(data, "", "  ")
	if err != nil {
		log.Fatalf("JSON marshaling error: %s", err.Error())
	}
	fmt.Printf("%s:\n%s\n", prefix, string(prettyJSON))
}

响应结果如下:

{
  "height": {
    "@type": "g:Int64",
    "@value": 6
  },
  "code": "p01",
  "weight": {
    "@type": "g:Int64",
    "@value": 10
  },
  "name": "aksan",
  "age": {
    "@type": "g:Int32",
    "@value": 29
  }
}

希望对你有帮助!

英文:

hello i'm using golang package "github.com/northwesternmutual/grammes" and want to get the field not just the value from response client so i can decode it to json

data, err := client.ExecuteQuery(g.V().Has(&quot;person&quot;, &quot;name&quot;, &quot;aksan&quot;).Values())
if err != nil {
	log.Fatalf(&quot;Querying error: %s&quot;, err.Error())
}
var responseStruct map[string]interface{}
err = json.Unmarshal(data[0], &amp;responseStruct)
if err != nil {
	log.Fatalf(&quot;unmarshal error: %s&quot;, err.Error())
}
// Log out the response.
PrettyPrint(responseStruct[&quot;@value&quot;], &quot;result&quot;)

the response is

[
   {
      &quot;@type&quot;: &quot;g:Int64&quot;,
      &quot;@value&quot;: 6
   },
   &quot;p01&quot;,
   {
      &quot;@type&quot;: &quot;g:Int64&quot;,
      &quot;@value&quot;: 10
   },
   &quot;aksan&quot;,
   {
      &quot;@type&quot;: &quot;g:Int32&quot;,
      &quot;@value&quot;: 29
   }
]

what i want is the result will showing field to like @fields or anything maybe something like this

[
   &quot;height&quot;:{
      &quot;@type&quot;: &quot;g:Int64&quot;,
      &quot;@value&quot;: 6
   },
   &quot;code&quot;:&quot;p01&quot;,
   &quot;weight&quot;:{
      &quot;@type&quot;: &quot;g:Int64&quot;,
      &quot;@value&quot;: 10
   },
   &quot;name&quot;:&quot;aksan&quot;,
   &quot;age&quot;:{
      &quot;@type&quot;: &quot;g:Int32&quot;,
      &quot;@value&quot;: 29
   }
]

答案1

得分: 3

使用ValueMap()代替Values()

英文:

Use ValueMap() instead of Values()

huangapple
  • 本文由 发表于 2022年3月17日 18:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/71510865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定