Question about schema

Hello, I have question with using dgraph.
example:
my biz: have a Person class

type Person struct {
	Name string `json:"name"`
	Age string `json:"age"`
}

my dao will build a dgraph schema by Person:

Person.Name string .
Person.Age int .
type Person {
	Person.Name
	Person.Age
}

my service:
CreatePerson will send a json like:

{
	"name": "foo",
	"age": 18
}

Are there a way to change this json to:

{
	"Person.Name": "foo",
	"Person.Age": 18
}

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
}

but by this way,

{
	"name": "foo",
	"age": 18
}

can not unmarshal to Person.

https://go.dev/play/p/08-ZSZrjFrE

This does not work for you? Maybe I misunderstood your issue

emm, the data my service in is :

{
	"name": "foo",
	"age": 18
}

I wrote a function to change the key, but when I find a node from dgraph, I also neet to change the result key.If a field of struct can have two tags…

func buildContent(content []byte, typ string, uid string) string {
	lexer := &jlexer.Lexer{Data: content}

	var builder strings.Builder
	isTopLevel := lexer.IsStart()
	if lexer.IsNull() {
		if isTopLevel {
			lexer.Consumed()
		}
		lexer.Skip()
		return ""
	}
	lexer.Delim('{')
	builder.WriteString(fmt.Sprintf(`{"dgraph.type":["%s"],"uid":"%s"`, typ, uid))

	//first := true
	for !lexer.IsDelim('}') {
		key := lexer.UnsafeFieldName(false)
		lexer.WantColon()
		if lexer.IsNull() {
			lexer.Skip()
			lexer.WantComma()
			continue
		}
		builder.WriteString(fmt.Sprintf(`,"%s.%s":`, typ, key))
		builder.Write(lexer.Raw())
		lexer.WantComma()
	}
	lexer.Delim('}')
	if isTopLevel {
		lexer.Consumed()
	}

	builder.WriteString(`}`)
	return builder.String()
}

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}}
  }}
}

You could use two different serializers, like in dgraph-lambda-go/dson.go at main · Schartey/dgraph-lambda-go · GitHub

type Person struct {
	Name string `json:"Person.name" dql:"name"`
	Age string `json:"Person.age" dql:"age"`
}
func (p *Person) AsJSON() []byte {
  b, _ := json.Marshal(p)
  return b
}

func (p *Person AsDSON() []byte {
  b, _ := dson.Marshal(p)
  return b
}

Here is @iluminae 's modified example: Go Playground - The Go Programming Language

Thanks!