<commit_before>package object

import (
	"crypto/sha256"
	"fmt"
	"math"
	"reflect"
	"sort"
	"strings"
)

// ObjectHash consistently hashes a map.
// It is based on <NAME>'s object hash, but using the same type hints
// as TJSON instead.
// TODO add redaction
func ObjectHash(o *Object) ([]byte, error) {
	return objecthash(o.ToMap(), true)
}

func hintsFromKey(k string) []TypeHint {
	ps := strings.Split(k, ":")
	if len(ps) == 1 {
		return nil
	}
	hs := []TypeHint{}
	kh := strings.Replace(ps[1], "<", "", -1)
	kh = strings.Replace(kh, ">", "", -1)
	for _, sh := range kh {
		hs = append(hs, GetTypeHint(string(sh)))
	}
	return hs
}

func objecthash(m map[string]interface{}, skipSig bool) ([]byte, error) {
	b := []byte{}
	ks := []string{}
	for k := range m {
		if len(k) > 0 && k[0] == '_' {
			continue
		}
		// TODO(geoah) is there a better way of doing this?
		// TODO(geoah) better question is, do we need @?
		if k == "@" {
			continue
		}
		if skipSig && strings.HasPrefix(k, "@signature") {
			continue
		}
		ks = append(ks, k)
	}
	sort.Strings(ks)
	x := map[string]interface{}{}
	for _, k := range ks {
		v := m[k]
		ts := hintsFromKey(k)
		hv := hashValueAs(k, v, ts...)
		if hv == nil {
			continue
		}
		// fmt.Println("hashing value for", k, "as", fmt.Sprintf("%x", hv))
		hk := hash(HintString, []byte(k))
		b = append(b, hk...)
		b = append(b, hv...)
		x[k] = hv
	}
	h := hash(HintObject, b)
	return h, nil
}

func hash(p TypeHint, b []byte) []byte {
	h := sha256.New()
	if _, err := h.Write([]byte(p)); err != nil {
		panic(err)
	}
	if _, err := h.Write(b); err != nil {
		panic(err)
	}
	return h.Sum(nil)
}

func hashValueAs(k string, o interface{}, ts ...TypeHint) []byte {
	v := reflect.ValueOf(o)
	t := reflect.TypeOf(o)

	if len(ts) == 0 {
		return nil
	}

	switch ts[0] {
	case HintArray:
		if v.Len() == 0 {
			return nil
		}
		vs := []interface{}{}
		for i := 0; i < v.Len(); i++ {
			vs = append(vs, v.Index(i).Interface())
		}
		b := []byte{}
		for _, iv := range vs {
			b = append(b, hashValueAs(k, iv, ts[1:]...)...)
		}
		// TODO(geoah) hint SHOULD NOT be array, but array + inner hint
		return hash(HintArray, b)
	case HintString:<commit_msg>fix(object): fix nil values<commit_after>package object

import (
	"crypto/sha256"
	"fmt"
	"math"
	"reflect"
	"sort"
	"strings"
)

// ObjectHash consistently hashes a map.
// It is based on <NAME>'s object hash, but using the same type hints
// as TJSON instead.
// TODO add redaction
func ObjectHash(o *Object) ([]byte, error) {
	return objecthash(o.ToMap(), true)
}

func hintsFromKey(k string) []TypeHint {
	ps := strings.Split(k, ":")
	if len(ps) == 1 {
		return nil
	}
	hs := []TypeHint{}
	for _, sh := range ps[1] {
		hs = append(hs, GetTypeHint(string(sh)))
	}
	return hs
}

func objecthash(m map[string]interface{}, skipSig bool) ([]byte, error) {
	b := []byte{}
	ks := []string{}
	for k := range m {
		if len(k) > 0 && k[0] == '_' {
			continue
		}
		// TODO(geoah) is there a better way of doing this?
		// TODO(geoah) better question is, do we need @?
		if k == "@" {
			continue
		}
		if skipSig && strings.HasPrefix(k, "@signature") {
			continue
		}
		ks = append(ks, k)
	}
	sort.Strings(ks)
	x := map[string]interface{}{}
	for _, k := range ks {
		v := m[k]
		if v == nil {
			continue
		}
		ts := hintsFromKey(k)
		hv := hashValueAs(k, v, ts...)
		if hv == nil {
			continue
		}
		// fmt.Println("hashing value for", k, "as", fmt.Sprintf("%x", hv))
		hk := hash(HintString, []byte(k))
		b = append(b, hk...)
		b = append(b, hv...)
		x[k] = hv
	}
	h := hash(HintObject, b)
	return h, nil
}

func hash(p TypeHint, b []byte) []byte {
	h := sha256.New()
	if _, err := h.Write([]byte(p)); err != nil {
		panic(err)
	}
	if _, err := h.Write(b); err != nil {
		panic(err)
	}
	return h.Sum(nil)
}

func hashValueAs(k string, o interface{}, ts ...TypeHint) []byte {
	if o == nil {
		return nil
	}

	v := reflect.ValueOf(o)
	t := reflect.TypeOf(o)

	if len(ts) == 0 {
		return nil
	}

	switch ts[0] {
	case HintArray:
		if v.Len() == 0 {
			return nil
		}
		vs := []interface{}{}
		for i := 0; i < v.Len(); i++ {
			vs = append(vs, v.Index(i).Interface())
		}
		b := []byte{}
		for _, iv := range vs {
			b = append(b, hashValueAs(k, iv, ts[1:]...)...)
		}
		// TODO(geoah) hint SHOULD NOT be array, but array + inner hint
		return hash(HintArray, b)
	case HintString:
{'fraction_non_alphanumeric': 0.19682685674867786, 'fraction_numerical': 0.0073580133363991725, 'mean_word_length': 2.9726027397260273, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, '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': '4117178', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1732, 'n_words': 493}