shared/generate/db/method.go
func (m *Method) fillSliceReferenceField(buf *file.Buffer, nk []*Field, field *Field) error {
	objectsVar := fmt.Sprintf("%sObjects", lex.Minuscule(field.Name))
	methodName := fmt.Sprintf("%s%sRef", lex.Capital(m.entity), field.Name)

	buf.L("// Fill field %s.", field.Name)
	buf.L("%s, err := c.%s(filter)", objectsVar, methodName)
	buf.L("if err != nil {")
	buf.L("        return nil, errors.Wrap(err, \"Failed to fetch field %s\")", field.Name)
	buf.L("}")
	buf.N()
	buf.L("for i := range objects {")
	needle := ""
	for i, key := range nk[:len(nk)-1] {
		needle += fmt.Sprintf("[objects[i].%s]", key.Name)
		subIndexTyp := indexType(nk[i+1:], field.Type.Name)
		buf.L("        _, ok := %s%s", objectsVar, needle)
		buf.L("        if !ok {")
		buf.L("                subIndex := %s{}", subIndexTyp)
		buf.L("                %s%s = subIndex", objectsVar, needle)
		buf.L("        }")
		buf.N()
	}

	needle += fmt.Sprintf("[objects[i].%s]", nk[len(nk)-1].Name)
	buf.L("        value := %s%s", objectsVar, needle)
	buf.L("        if value == nil {")
	buf.L("                value = %s{}", field.Type.Name)
	buf.L("        }")
	buf.L("        objects[i].%s = value", field.Name)
	buf.L("}")
	buf.N()

	return nil
}
func (m *Method) fillSliceReferenceField(buf *file.Buffer, nk []*Field, field *Field) error {
	objectsVar := fmt.Sprintf("%sObjects", lex.Minuscule(field.Name))
	methodName := fmt.Sprintf("%s%sRef", lex.Capital(m.entity), field.Name)

	buf.L("// Fill field %s.", field.Name)
	buf.L("%s, err := c.%s(filter)", objectsVar, methodName)
	buf.L("if err != nil {")
	buf.L("        return nil, errors.Wrap(err, \"Failed to fetch field %s\")", field.Name)
	buf.L("}")
	buf.N()
	buf.L("for i := range objects {")
	needle := ""
	for i, key := range nk[:len(nk)-1] {
		needle += fmt.Sprintf("[objects[i].%s]", key.Name)
		subIndexTyp := indexType(nk[i+1:], field.Type.Name)
		buf.L("        _, ok := %s%s", objectsVar, needle)
		buf.L("        if !ok {")
		buf.L("                subIndex := %s{}", subIndexTyp)
		buf.L("                %s%s = subIndex", objectsVar, needle)
		buf.L("        }")
		buf.N()
	}

	needle += fmt.Sprintf("[objects[i].%s]", nk[len(nk)-1].Name)
	buf.L("        value := %s%s", objectsVar, needle)
	buf.L("        if value == nil {")
	buf.L("                value = %s{}", field.Type.Name)
	buf.L("        }")
	buf.L("        objects[i].%s = value", field.Name)
	buf.L("}")
	buf.N()

	return nil
}
// Populate a field consisting of a slice of objects referencing the
// entity. This information is available by joining a the view or table
// associated with the type of the referenced objects, which must contain the
// natural key of the entity.