package iceberg
import (
"encoding"
"fmt"
"strconv"
"strings"
)
func ParseTransform (s string ) (Transform , error ) {
s = strings .ToLower (s )
switch {
case strings .HasPrefix (s , "bucket" ):
matches := regexFromBrackets .FindStringSubmatch (s )
if len (matches ) != 2 {
break
}
n , _ := strconv .Atoi (matches [1 ])
return BucketTransform {NumBuckets : n }, nil
case strings .HasPrefix (s , "truncate" ):
matches := regexFromBrackets .FindStringSubmatch (s )
if len (matches ) != 2 {
break
}
n , _ := strconv .Atoi (matches [1 ])
return TruncateTransform {Width : n }, nil
default :
switch s {
case "identity" :
return IdentityTransform {}, nil
case "void" :
return VoidTransform {}, nil
case "year" :
return YearTransform {}, nil
case "month" :
return MonthTransform {}, nil
case "day" :
return DayTransform {}, nil
case "hour" :
return HourTransform {}, nil
}
}
return nil , fmt .Errorf ("%w: %s" , ErrInvalidTransform , s )
}
type Transform interface {
fmt .Stringer
encoding .TextMarshaler
ResultType (t Type ) Type
}
type IdentityTransform struct {}
func (t IdentityTransform ) MarshalText () ([]byte , error ) {
return []byte (t .String ()), nil
}
func (IdentityTransform ) String () string { return "identity" }
func (IdentityTransform ) ResultType (t Type ) Type { return t }
type VoidTransform struct {}
func (t VoidTransform ) MarshalText () ([]byte , error ) {
return []byte (t .String ()), nil
}
func (VoidTransform ) String () string { return "void" }
func (VoidTransform ) ResultType (t Type ) Type { return t }
type BucketTransform struct {
NumBuckets int
}
func (t BucketTransform ) MarshalText () ([]byte , error ) {
return []byte (t .String ()), nil
}
func (t BucketTransform ) String () string { return fmt .Sprintf ("bucket[%d]" , t .NumBuckets ) }
func (BucketTransform ) ResultType (Type ) Type { return PrimitiveTypes .Int32 }
type TruncateTransform struct {
Width int
}
func (t TruncateTransform ) MarshalText () ([]byte , error ) {
return []byte (t .String ()), nil
}
func (t TruncateTransform ) String () string { return fmt .Sprintf ("truncate[%d]" , t .Width ) }
func (TruncateTransform ) ResultType (t Type ) Type { return t }
type YearTransform struct {}
func (t YearTransform ) MarshalText () ([]byte , error ) {
return []byte (t .String ()), nil
}
func (YearTransform ) String () string { return "year" }
func (YearTransform ) ResultType (Type ) Type { return PrimitiveTypes .Int32 }
type MonthTransform struct {}
func (t MonthTransform ) MarshalText () ([]byte , error ) {
return []byte (t .String ()), nil
}
func (MonthTransform ) String () string { return "month" }
func (MonthTransform ) ResultType (Type ) Type { return PrimitiveTypes .Int32 }
type DayTransform struct {}
func (t DayTransform ) MarshalText () ([]byte , error ) {
return []byte (t .String ()), nil
}
func (DayTransform ) String () string { return "day" }
func (DayTransform ) ResultType (Type ) Type { return PrimitiveTypes .Date }
type HourTransform struct {}
func (t HourTransform ) MarshalText () ([]byte , error ) {
return []byte (t .String ()), nil
}
func (HourTransform ) String () string { return "hour" }
func (HourTransform ) ResultType (Type ) Type { return PrimitiveTypes .Int32 }
The pages are generated with Golds v0.8.2 . (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 .