<commit_before>package jsonld

import (
	"encoding/json"
	"reflect"
)

type payloadWithContext struct {
	Context Context `json:"@context"`
	Obj     *interface{}
}

func IsEmpty(v reflect.Value) bool {
	var ret bool
	switch v.Kind() {
	case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
		ret = v.IsNil()
	case reflect.Slice, reflect.Map:
		ret = v.Len() == 0
	case reflect.Struct:
		ret = func(reflect.Value) bool {
			var ret bool = true
			for i := 0; i < v.NumField(); i++ {
				ret = ret && IsEmpty(v.Field(i))
			}
			return ret
		}(v)
	case reflect.String:
		ret = v.String() == ""
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		ret = v.Int() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		ret = v.Uint() == 0
	case reflect.Float32, reflect.Float64:
		ret = v.Float() == 0.0
	}
	return ret
}

func getMap(v interface{}) map[string]interface{} {
	a := make(map[string]interface{})
	typ := reflect.TypeOf(v)
	val := reflect.ValueOf(v)
	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
		val = val.Elem()
	}

	for i := 0; i < typ.NumField(); i++ {
		cField := typ.Field(i)
		cValue := val.Field(i)
		if cField.Anonymous {
			for k, v := range getMap(cValue.Interface()) {
				a[k] = v
			}
		} else {
			if !IsEmpty(cValue) {
				a[cField.Name] = cValue.Interface()
			}
		}
	}

	return a
}

func (p *payloadWithContext) MarshalJSON() ([]byte, error) {
	a := make(map[string]interface{})
	a["@context"] = p.Context.Ref()

	for k, v := range getMap(*p.Obj) {
		a[k] = v
	}

	return json.Marshal(a)
}

func (p *payloadWithContext) UnmarshalJSON() {}

type Encoder struct{}

func Marshal(v interface{}, c *Context) ([]byte, error) {
	if c == nil {
		return json.Marshal(v)
	}
	p := payloadWithContext{*c, &v}
	return p.MarshalJSON()
}<commit_msg>Transform Context property into a pointer<commit_after>package jsonld

import (
	"encoding/json"
	"reflect"
)

type payloadWithContext struct {
	Context *Context `json:"@context"`
	Obj     *interface{}
}

func IsEmpty(v reflect.Value) bool {
	var ret bool
	switch v.Kind() {
	case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
		ret = v.IsNil()
	case reflect.Slice, reflect.Map:
		ret = v.Len() == 0
	case reflect.Struct:
		ret = func(reflect.Value) bool {
			var ret bool = true
			for i := 0; i < v.NumField(); i++ {
				ret = ret && IsEmpty(v.Field(i))
			}
			return ret
		}(v)
	case reflect.String:
		ret = v.String() == ""
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		ret = v.Int() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		ret = v.Uint() == 0
	case reflect.Float32, reflect.Float64:
		ret = v.Float() == 0.0
	}
	return ret
}

func getMap(v interface{}) map[string]interface{} {
	a := make(map[string]interface{})
	typ := reflect.TypeOf(v)
	val := reflect.ValueOf(v)
	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
		val = val.Elem()
	}

	for i := 0; i < typ.NumField(); i++ {
		cField := typ.Field(i)
		cValue := val.Field(i)
		if cField.Anonymous {
			for k, v := range getMap(cValue.Interface()) {
				a[k] = v
			}
		} else {
			if !IsEmpty(cValue) {
				a[cField.Name] = cValue.Interface()
			}
		}
	}

	return a
}

func (p *payloadWithContext) MarshalJSON() ([]byte, error) {
	a := make(map[string]interface{})
	if p.Context != nil {
		a["@context"] = p.Context.Ref()
	}

	for k, v := range getMap(*p.Obj) {
		a[k] = v
	}

	return json.Marshal(a)
}

func (p *payloadWithContext) UnmarshalJSON() {}

type Encoder struct{}

func Marshal(v interface{}, c *Context) ([]byte, error) {
	p := payloadWithContext{c, &v}
	return p.MarshalJSON()
}
{'fraction_non_alphanumeric': 0.17145957677699403, 'fraction_numerical': 0.013564839934888768, 'mean_word_length': 3.4582829504232167, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '4292695', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1389, 'n_words': 347}