<commit_before>package puppetquery

import (
	"encoding/json"
	"log"
)

type Any interface{}
type QueryString []Any

func ActiveNodes() QueryString {
	return QueryString{Any("="), QueryString{Any("node"), Any("active")}, Any(true)}
}

func hasOp(op string, tree QueryString) bool {
	if len(tree) > 0 {
		o, ok := tree[0].(string)
		return ok && o == op
	}
	return false
}

func BinOp(binop string, left, right QueryString) QueryString {
	switch {
	case len(left) == 0:
		return right
	case len(right) == 0:
		return left
	case len(left) > 2 && hasOp(binop, left):
		return append(left, right)
	case len(right) > 2 && hasOp(binop, right):
		return append(right, left)
	}
	return QueryString{Any(binop), left, right}
}

func And(left, right QueryString) QueryString {
	return BinOp("and", left, right)
}

func Or(left, right QueryString) QueryString {
	return BinOp("or", left, right)
}

func Not(tree QueryString) QueryString {
	switch {
	case len(tree) == 0:
		return tree
	case len(tree) == 2 && hasOp("not", tree):
		return tree[1].(QueryString)
	}
	return QueryString{Any("not"), tree}
}

func FactCompare(name, op string, value Any) QueryString {
	return QueryString{op, QueryString{Any("fact"), Any(name)}, value}
}

func (q *QueryString) ToJson() string {
	b, err := json.Marshal(q)
	if err != nil {
		log.Println("ERROR: cannot marshal: ", err)
		return ""
	}
	return string(b)
}<commit_msg>refs #000 hide implementation of query string<commit_after>package puppetquery

import (
	"encoding/json"
	"log"
)

type any interface{}
type QueryString []any

func ActiveNodes() QueryString {
	return QueryString{any("="), QueryString{any("node"), any("active")}, any(true)}
}

func hasOp(op string, tree QueryString) bool {
	if len(tree) > 0 {
		o, ok := tree[0].(string)
		return ok && o == op
	}
	return false
}

func BinOp(binop string, left, right QueryString) QueryString {
	switch {
	case len(left) == 0:
		return right
	case len(right) == 0:
		return left
	case len(left) > 2 && hasOp(binop, left):
		return append(left, right)
	case len(right) > 2 && hasOp(binop, right):
		return append(right, left)
	}
	return QueryString{any(binop), left, right}
}

func And(left, right QueryString) QueryString {
	return BinOp("and", left, right)
}

func Or(left, right QueryString) QueryString {
	return BinOp("or", left, right)
}

func Not(tree QueryString) QueryString {
	switch {
	case len(tree) == 0:
		return tree
	case len(tree) == 2 && hasOp("not", tree):
		return tree[1].(QueryString)
	}
	return QueryString{any("not"), tree}
}

func FactCompare(name, op string, value any) QueryString {
	return QueryString{op, QueryString{any("fact"), any(name)}, value}
}

func (q *QueryString) ToJson() string {
	b, err := json.Marshal(q)
	if err != nil {
		log.Println("ERROR: cannot marshal: ", err)
		return ""
	}
	return string(b)
}
{'fraction_non_alphanumeric': 0.1524045261669024, 'fraction_numerical': 0.007425742574257425, 'mean_word_length': 4.69215291750503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1126077', 'n_tokens_mistral': 1037, 'n_tokens_neox': 994, 'n_words': 294}