package attrnorm
import (
"reflect"
"strings"
"unicode/utf8"
"go.opentelemetry.io/otel/attribute"
)
func Truncate (limit int , attr attribute .KeyValue ) attribute .KeyValue {
if limit < 0 {
return attr
}
switch attr .Value .Type () {
case attribute .STRING :
attr .Value = attribute .StringValue (truncate (limit , attr .Value .AsString ()))
case attribute .BYTESLICE :
s := attr .Value .AsString ()
if len (s ) > limit {
attr .Value = attribute .ByteSliceValue ([]byte (s [:limit ]))
}
case attribute .STRINGSLICE :
attr .Value , _ = truncateStringSliceValue (limit , attr .Value )
case attribute .SLICE :
attr .Value , _ = truncateSliceValue (limit , attr .Value )
case attribute .MAP :
attr .Value , _ = truncateMapValue (limit , attr .Value )
}
return attr
}
func TruncateValue (limit int , v attribute .Value ) attribute .Value {
if limit < 0 {
return v
}
switch v .Type () {
case attribute .STRING :
return attribute .StringValue (truncate (limit , v .AsString ()))
case attribute .BYTESLICE :
s := v .AsString ()
if len (s ) > limit {
return attribute .ByteSliceValue ([]byte (s [:limit ]))
}
case attribute .STRINGSLICE :
v , _ = truncateStringSliceValue (limit , v )
case attribute .SLICE :
v , _ = truncateSliceValue (limit , v )
case attribute .MAP :
v , _ = truncateMapValue (limit , v )
}
return v
}
func truncateValue(limit int , v attribute .Value ) (attribute .Value , bool ) {
switch v .Type () {
case attribute .STRING :
s := v .AsString ()
truncated := truncate (limit , s )
if truncated != s {
return attribute .StringValue (truncated ), true
}
case attribute .STRINGSLICE :
return truncateStringSliceValue (limit , v )
case attribute .BYTESLICE :
s := v .AsString ()
if len (s ) > limit {
return attribute .ByteSliceValue ([]byte (s [:limit ])), true
}
case attribute .SLICE :
return truncateSliceValue (limit , v )
case attribute .MAP :
return truncateMapValue (limit , v )
}
return v , false
}
func truncateStringSliceValue(limit int , value attribute .Value ) (attribute .Value , bool ) {
storage := valueStorage (value )
switch values := storage .(type ) {
case [0 ]string :
return value , false
case [1 ]string :
return truncateStrings (limit , value , values [:])
case [2 ]string :
return truncateStrings (limit , value , values [:])
case [3 ]string :
return truncateStrings (limit , value , values [:])
default :
array := reflect .ValueOf (storage )
if array .Kind () != reflect .Array || array .Type ().Elem () != stringType {
return value , false
}
return truncateStringArray (limit , value , array )
}
}
func truncateStrings(limit int , value attribute .Value , values []string ) (attribute .Value , bool ) {
var truncated []string
for i , s := range values {
newS := truncate (limit , s )
if truncated != nil {
truncated [i ] = newS
continue
}
if newS == s {
continue
}
truncated = make ([]string , len (values ))
copy (truncated , values [:i ])
truncated [i ] = newS
}
if truncated == nil {
return value , false
}
return attribute .StringSliceValue (truncated ), true
}
func truncateStringArray(limit int , value attribute .Value , values reflect .Value ) (attribute .Value , bool ) {
var truncated []string
for i := range values .Len () {
s := values .Index (i ).String ()
newS := truncate (limit , s )
if truncated != nil {
truncated [i ] = newS
continue
}
if newS == s {
continue
}
truncated = make ([]string , values .Len ())
for j := range i {
truncated [j ] = values .Index (j ).String ()
}
truncated [i ] = newS
}
if truncated == nil {
return value , false
}
return attribute .StringSliceValue (truncated ), true
}
func truncateSliceValue(limit int , value attribute .Value ) (attribute .Value , bool ) {
storage := valueStorage (value )
switch values := storage .(type ) {
case [0 ]attribute .Value :
return value , false
case [1 ]attribute .Value :
return truncateValueSlice (limit , value , values [:])
case [2 ]attribute .Value :
return truncateValueSlice (limit , value , values [:])
case [3 ]attribute .Value :
return truncateValueSlice (limit , value , values [:])
case [4 ]attribute .Value :
return truncateValueSlice (limit , value , values [:])
case [5 ]attribute .Value :
return truncateValueSlice (limit , value , values [:])
default :
array := reflect .ValueOf (storage )
if array .Kind () != reflect .Array || array .Type ().Elem () != valueType {
return value , false
}
return truncateValueArray (limit , value , array )
}
}
func truncateValueSlice(limit int , value attribute .Value , values []attribute .Value ) (attribute .Value , bool ) {
var truncated []attribute .Value
for i , elem := range values {
newElem , changed := truncateValue (limit , elem )
if truncated != nil {
truncated [i ] = newElem
continue
}
if !changed {
continue
}
truncated = make ([]attribute .Value , len (values ))
copy (truncated , values [:i ])
truncated [i ] = newElem
}
if truncated == nil {
return value , false
}
return attribute .SliceValue (truncated ...), true
}
func truncateValueArray(limit int , value attribute .Value , values reflect .Value ) (attribute .Value , bool ) {
var truncated []attribute .Value
for i := range values .Len () {
elem := values .Index (i ).Interface ().(attribute .Value )
newElem , changed := truncateValue (limit , elem )
if truncated != nil {
truncated [i ] = newElem
continue
}
if !changed {
continue
}
truncated = make ([]attribute .Value , values .Len ())
for j := range i {
truncated [j ] = values .Index (j ).Interface ().(attribute .Value )
}
truncated [i ] = newElem
}
if truncated == nil {
return value , false
}
return attribute .SliceValue (truncated ...), true
}
func truncateMapValue(limit int , value attribute .Value ) (attribute .Value , bool ) {
storage := valueStorage (value )
switch keyValues := storage .(type ) {
case [0 ]attribute .KeyValue :
return value , false
case [1 ]attribute .KeyValue :
return truncateKeyValueSlice (limit , value , keyValues [:])
case [2 ]attribute .KeyValue :
return truncateKeyValueSlice (limit , value , keyValues [:])
case [3 ]attribute .KeyValue :
return truncateKeyValueSlice (limit , value , keyValues [:])
case [4 ]attribute .KeyValue :
return truncateKeyValueSlice (limit , value , keyValues [:])
case [5 ]attribute .KeyValue :
return truncateKeyValueSlice (limit , value , keyValues [:])
default :
array := reflect .ValueOf (storage )
if array .Kind () != reflect .Array || array .Type ().Elem () != keyValueType {
return value , false
}
return truncateKeyValueArray (limit , value , array )
}
}
func truncateKeyValueSlice(
limit int ,
value attribute .Value ,
keyValues []attribute .KeyValue ,
) (attribute .Value , bool ) {
var truncated []attribute .KeyValue
for i , kv := range keyValues {
newValue , changed := truncateValue (limit , kv .Value )
if changed {
kv .Value = newValue
}
if truncated != nil {
truncated [i ] = kv
continue
}
if !changed {
continue
}
truncated = make ([]attribute .KeyValue , len (keyValues ))
copy (truncated , keyValues [:i ])
truncated [i ] = kv
}
if truncated == nil {
return value , false
}
return attribute .MapValue (truncated ...), true
}
func truncateKeyValueArray(limit int , value attribute .Value , keyValues reflect .Value ) (attribute .Value , bool ) {
var truncated []attribute .KeyValue
for i := range keyValues .Len () {
kv := keyValues .Index (i ).Interface ().(attribute .KeyValue )
newValue , changed := truncateValue (limit , kv .Value )
if changed {
kv .Value = newValue
}
if truncated != nil {
truncated [i ] = kv
continue
}
if !changed {
continue
}
truncated = make ([]attribute .KeyValue , keyValues .Len ())
for j := range i {
truncated [j ] = keyValues .Index (j ).Interface ().(attribute .KeyValue )
}
truncated [i ] = kv
}
if truncated == nil {
return value , false
}
return attribute .MapValue (truncated ...), true
}
var stringType = reflect .TypeFor [string ]()
func truncate(limit int , s string ) string {
if limit < 0 || len (s ) <= limit {
return s
}
var b strings .Builder
count := 0
for i , c := range s {
if c != utf8 .RuneError {
count ++
if count > limit {
return s [:i ]
}
continue
}
if limit == 0 {
return ""
}
_ , size := utf8 .DecodeRuneInString (s [i :])
if size == 1 {
b .Grow (len (s ) - 1 )
_, _ = b .WriteString (s [:i ])
s = s [i :]
break
}
count ++
if count > limit {
return s [:i ]
}
}
if b .Cap () == 0 {
return s
}
for i := 0 ; i < len (s ) && count < limit ; {
c := s [i ]
if c < utf8 .RuneSelf {
_ = b .WriteByte (c )
i ++
count ++
continue
}
_ , size := utf8 .DecodeRuneInString (s [i :])
if size == 1 {
i ++
continue
}
_, _ = b .WriteString (s [i : i +size ])
i += size
count ++
}
return b .String ()
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .