Are you saying you want to render json to match your schema? Then just change the json tags:
type Person struct {
Name string `json:"Person.Name"`
Age int64 `json:"Person.Age"` //this would be int to match dgraph schema, but was string in your example go struct
}
You mean you have a response coming in age,name and you need to convert it to Person.Age,Person.Name before sending it to dgraph? If so not really a dgraph question at all but:
type(
inputPerson struct {
Age int `json:"age"`
Name string `json:"name"`
}
outputPerson struct {
Age int `json:"Person.Age"`
Name string `json:"Person.Name"`
}
)
func formatForDgraph(input []byte) []byte {
it:=inputPerson{}
if err:= json.Unmarshal(input, &it); err!= nil {
panic(err)
}
buf, err :=json.Marshal(outputPerson{Age:it.Age,Name:it.Name})
if err != nil {
panic(err)
}
return buf
}
Or you could use the binary API:
type inputPerson struct {
Age int `json:"age"`
Name string `json:"name"`
}
func formatForDgraph(input []byte) []*api.NQuad {
it:=inputPerson{}
if err:= json.Unmarshal(input, &it); err!= nil {
panic(err)
}
return []*api.NQuad{{
Subject: "_:newPersonEntry",
Predicate: "Person.Name",
ObjectValue: &api.Value{Val:&api.Value_StrVal{it.Name}},
},{
Subject: "_:newPersonEntry",
Predicate: "Person.Age",
ObjectValue: &api.Value{Val:&api.Value_IntVal{it.Age}}
}}
}