<commit_before>package main

import (
	"github.com/jmcvetta/neoism"
)

type PeopleDriver interface {
	Write(p person) error
	Read(uuid string) (p person, found bool, err error)
	Delete(uuid string) error
}

type PeopleCypherDriver struct {
	cypherRunner CypherRunner
}

func NewPeopleCypherDriver(cypherRunner CypherRunner) PeopleCypherDriver {
	return PeopleCypherDriver{cypherRunner}
}

func (pcd PeopleCypherDriver) Read(uuid string) (person, bool, error) {
	results := []struct {
		UUID              string `json:"uuid"`
		Name              string `json: "name"`
		BirthYear         int    `json: "birthYear"`
		Salutation        string `json: "salutation"`
		FactsetIdentifier string `json: "factsetIdentifier"`
	}{}

	query := &neoism.CypherQuery{
		Statement: `MATCH (n:Person {uuid:{uuid}}) return n.uuid 
		as uuid, n.name as name, 
		n.factsetIdentifier as factsetIdentifier,
		n.birthYear as birthYear,
		n.salutation as salutation`,
		Parameters: map[string]interface{}{
			"uuid": uuid,
		},
		Result: &results,
	}

	err := pcd.cypherRunner.CypherBatch([]*neoism.CypherQuery{query})

	if err != nil {
		return person{}, false, err
	}

	if len(results) == 0 {
		return person{}, false, nil
	}

	result := results[0]

	p := person{
		UUID:       result.UUID,
		Name:       result.Name,
		BirthYear:  result.BirthYear,
		Salutation: result.Salutation,
	}

	if result.FactsetIdentifier != "" {
		p.Identifiers = append(p.Identifiers, identifier{fsAuthority, result.FactsetIdentifier})
	}

	return p, true, nil

}

func (pcd PeopleCypherDriver) Write(p person) error {

	params := map[string]interface{}{
		"uuid": p.UUID,
	}

	if p.Name != "" {
		params["name"] = p.Name
	}

	if p.BirthYear != 0 {
		params["birthYear"] = p.BirthYear
	}

	if p.Salutation != "" {
		params["salutation"] = p.Salutation
	}

	for _, identifier := range p.Identifiers {
		if identifier.Authority == fsAuthority {
			params["factsetIdentifier"] = identifier.IdentifierValue
		}
	}

	query := &neoism.CypherQuery{
		Statement: `MERGE (n:Person {uuid: {uuid}}) 
					set n={allprops}
					return  n`,
		Parameters: map[string]interface{}{
			"uuid":     p.UUID,
			"allprops": params,
		},
	}

	return pcd.cypherRunner.CypherBatch([]*neoism.CypherQuery{query})

}

func (pcd PeopleCypherDriver) Delete(uuid string) error {
	query := &neoism.CypherQuery{
		Statement: `MATCH (n:Person {uuid:{uuid}}) DELETE n`,
		Parameters: map[string]interface{}{
			"uuid": uuid,
		},
	}

	return pcd.cypherRunner.CypherBatch([]*neoism.CypherQuery{query})
}

const (
	fsAuthority = "http://api.ft.com/system/FACTSET-PPL"
)
<commit_msg>Better cypher<commit_after>package main

import (
	"github.com/jmcvetta/neoism"
)

type PeopleDriver interface {
	Write(p person) error
	Read(uuid string) (p person, found bool, err error)
	Delete(uuid string) error
}

type PeopleCypherDriver struct {
	cypherRunner CypherRunner
}

func NewPeopleCypherDriver(cypherRunner CypherRunner) PeopleCypherDriver {
	return PeopleCypherDriver{cypherRunner}
}

func (pcd PeopleCypherDriver) Read(uuid string) (person, bool, error) {
	results := []struct {
		UUID              string `json:"uuid"`
		Name              string `json: "name"`
		BirthYear         int    `json: "birthYear"`
		Salutation        string `json: "salutation"`
		FactsetIdentifier string `json: "factsetIdentifier"`
	}{}

	query := &neoism.CypherQuery{
		Statement: `MATCH (n:Person {uuid:{uuid}}) return n.uuid 
		as uuid, n.name as name, 
		n.factsetIdentifier as factsetIdentifier,
		n.birthYear as birthYear,
		n.salutation as salutation`,
		Parameters: map[string]interface{}{
			"uuid": uuid,
		},
		Result: &results,
	}

	err := pcd.cypherRunner.CypherBatch([]*neoism.CypherQuery{query})

	if err != nil {
		return person{}, false, err
	}

	if len(results) == 0 {
		return person{}, false, nil
	}

	result := results[0]

	p := person{
		UUID:       result.UUID,
		Name:       result.Name,
		BirthYear:  result.BirthYear,
		Salutation: result.Salutation,
	}

	if result.FactsetIdentifier != "" {
		p.Identifiers = append(p.Identifiers, identifier{fsAuthority, result.FactsetIdentifier})
	}

	return p, true, nil

}

func (pcd PeopleCypherDriver) Write(p person) error {

	params := map[string]interface{}{
		"uuid": p.UUID,
	}

	if p.Name != "" {
		params["name"] = p.Name
	}

	if p.BirthYear != 0 {
		params["birthYear"] = p.BirthYear
	}

	if p.Salutation != "" {
		params["salutation"] = p.Salutation
	}

	for _, identifier := range p.Identifiers {
		if identifier.Authority == fsAuthority {
			params["factsetIdentifier"] = identifier.IdentifierValue
		}
	}

	query := &neoism.CypherQuery{
		Statement: `MERGE (n:Thing {uuid: {uuid}}) 
					set n={allprops}
					set n :Concept
					set n :Person
		`,
		Parameters: map[string]interface{}{
			"uuid":     p.UUID,
			"allprops": params,
		},
	}

	return pcd.cypherRunner.CypherBatch([]*neoism.CypherQuery{query})

}

func (pcd PeopleCypherDriver) Delete(uuid string) error {
	//TODO: this need to use the approach described in :
	// https://docs.google.com/document/d/1Ec-umbNOZa9zht2FImAY-fsMDFgDDxBUMeokDTZZ3tQ/edit#heading=h.pgfg88uoy07a
	query := &neoism.CypherQuery{
		Statement: `MATCH (n:Person {uuid:{uuid}}) DELETE n`,
		Parameters: map[string]interface{}{
			"uuid": uuid,
		},
	}

	return pcd.cypherRunner.CypherBatch([]*neoism.CypherQuery{query})
}

const (
	fsAuthority = "http://api.ft.com/system/FACTSET-PPL"
)

{'fraction_non_alphanumeric': 0.1388374676045909, 'fraction_numerical': 0.00259163272861903, 'mean_word_length': 3.8763537906137184, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3464485', 'n_tokens_mistral': 2101, 'n_tokens_neox': 1919, 'n_words': 417}