<commit_before>package models

//go:generate enumer -type=Format -autotrimprefix -json

import (
	"fmt"
	"time"

	"github.com/sleepinggenius2/gosmi/types"
)

type Format byte

const (
	FormatNone     Format = 0
	FormatEnumName Format = 1 << iota
	FormatEnumValue
	FormatBits
	FormatString
	FormatUnits
	FormatDurationShort
	FormatAll Format = 0xff & ^FormatUnits
)

func ResolveFormat(formats []Format) (format Format) {
	if len(formats) == 0 {
		return FormatAll
	}
	for _, f := range formats {
		format |= f
	}
	return
}

type Value struct {
	Format    Format
	Formatted string
	Raw       interface{}
}

func (v Value) Bytes() []byte {
	if b, ok := v.Raw.([]byte); ok {
		return b
	}
	return []byte{}
}

func (v Value) Duration() time.Duration {
	if d, ok := v.Raw.(time.Duration); ok {
		return d
	}
	return time.Duration(0)
}

func (v Value) Int64() int64 {
	if i, ok := v.Raw.(int64); ok {
		return i
	}
	return 0
}

func (v Value) String() string {
	if v.Format == FormatNone {
		return fmt.Sprintf("%v", v.Raw)
	}
	return v.Formatted
}

type ValueFormatter func(interface{}) Value

func (n Node) FormatValue(value interface{}, flags ...Format) Value {
	return n.Type.FormatValue(value, flags...)
}

func (n Node) GetValueFormatter(flags ...Format) ValueFormatter {
	return n.Type.GetValueFormatter(flags...)
}

func (t Type) FormatValue(value interface{}, flags ...Format) Value {
	formatFlags := ResolveFormat(flags)
	switch t.BaseType {
	case types.BaseTypeOctetString:
		switch t.Name {
		case "IpAddress", "InetAddress", "IpV4orV6Addr":
			return GetInetAddressFormatted(value, formatFlags)
		}
		return GetOctetStringFormatted(value, formatFlags, t.Format)
	case types.BaseTypeBits:
		if t.Enum == nil {
			return GetBitsFormatted(value, formatFlags)
		}
		return GetEnumBitsFormatted(value, formatFlags, t.Enum)
	case types.BaseTypeEnum:
		return GetEnumFormatted(value, formatFlags, t.Enum)
	}<commit_msg>Adds formatting functions for scalar and column nodes<commit_after>package models

//go:generate enumer -type=Format -autotrimprefix -json

import (
	"fmt"
	"time"

	"github.com/sleepinggenius2/gosmi/types"
)

type Format byte

const (
	FormatNone     Format = 0
	FormatEnumName Format = 1 << iota
	FormatEnumValue
	FormatBits
	FormatString
	FormatUnits
	FormatDurationShort
	FormatAll Format = 0xff & ^FormatUnits
)

func ResolveFormat(formats []Format) (format Format) {
	if len(formats) == 0 {
		return FormatAll
	}
	for _, f := range formats {
		format |= f
	}
	return
}

type Value struct {
	Format    Format
	Formatted string
	Raw       interface{}
}

func (v Value) Bytes() []byte {
	if b, ok := v.Raw.([]byte); ok {
		return b
	}
	return []byte{}
}

func (v Value) Duration() time.Duration {
	if d, ok := v.Raw.(time.Duration); ok {
		return d
	}
	return time.Duration(0)
}

func (v Value) Int64() int64 {
	if i, ok := v.Raw.(int64); ok {
		return i
	}
	return 0
}

func (v Value) String() string {
	if v.Format == FormatNone {
		return fmt.Sprintf("%v", v.Raw)
	}
	return v.Formatted
}

type ValueFormatter func(interface{}) Value

func (n Node) FormatValue(value interface{}, flags ...Format) Value {
	return n.Type.FormatValue(value, flags...)
}

func (n Node) GetValueFormatter(flags ...Format) ValueFormatter {
	return n.Type.GetValueFormatter(flags...)
}

func (n ScalarNode) FormatValue(value interface{}, flags ...Format) Value {
	return n.Type.FormatValue(value, flags...)
}

func (n ScalarNode) GetValueFormatter(flags ...Format) ValueFormatter {
	return n.Type.GetValueFormatter(flags...)
}

func (n ColumnNode) FormatValue(value interface{}, flags ...Format) Value {
	return n.Type.FormatValue(value, flags...)
}

func (n ColumnNode) GetValueFormatter(flags ...Format) ValueFormatter {
	return n.Type.GetValueFormatter(flags...)
}

func (t Type) FormatValue(value interface{}, flags ...Format) Value {
	formatFlags := ResolveFormat(flags)
	switch t.BaseType {
	case types.BaseTypeOctetString:
		switch t.Name {
		case "IpAddress", "InetAddress", "IpV4orV6Addr":
			return GetInetAddressFormatted(value, formatFlags)
		}
		return GetOctetStringFormatted(value, formatFlags, t.Format)
	case types.BaseTypeBits:
		if t.Enum == nil {
			return GetBitsFormatted(value, formatFlags)
		}
		return GetEnumBitsFormatted(value, formatFlags, t.Enum)
	case types.BaseTypeEnum:
		return GetEnumFormatted(value, formatFlags, t.Enum)
	}
{'fraction_non_alphanumeric': 0.12614678899082568, 'fraction_numerical': 0.006880733944954129, 'mean_word_length': 4.059164733178655, 'pattern_counts': {'":': 2, '<': 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': '867565', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1475, 'n_words': 405}