Skip to content
Snippets Groups Projects
Select Git revision
  • 73eabcd36a84c616d21cdafd96c56238f9b46331
  • master default protected
  • fix/PRXS-3401-ValidateValidationOpts
  • feature/PRXS-3383-CollectionsRankSortAPI
  • feature/3149-LocaleCodeAsID-Feature
  • feature/PRXS-3383-CollectionsSort
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • feature/PRXS-3218-HideTemplateActions
  • feature/PRXS-3234-PruneIdents
  • feature/3146-UpdateItemStorageInterface
  • feature/3274-ObjectIndexesFixes
  • feature/PRXS-3143-3235-ReferenceOptions
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

collection_test.go

Blame
  • timestamp.go 1.74 KiB
    package field
    
    import (
    	"context"
    	"fmt"
    	"time"
    )
    
    var (
    	zeroTime      = time.Time{}
    	timestampType = &TimestampType{}
    )
    
    type TimestampParameters struct{}
    
    func (t TimestampParameters) Type() Type                   { return timestampType }
    func (t *TimestampParameters) Clone(reset bool) Parameters { return t }
    
    type TimestampType struct{}
    
    func (t TimestampType) Name() string {
    	return "timestamp"
    }
    
    func (TimestampType) NewParameters() Parameters {
    	return &TimestampParameters{}
    }
    
    func (TimestampType) IsEmpty(v interface{}) bool {
    	return v == 0 || v == nil
    }
    
    func toTimestamp(i interface{}) (interface{}, error) {
    	switch v := i.(type) {
    	case nil:
    		return nil, nil
    	case int64:
    		return v, nil
    	case int:
    		return int64(v), nil
    	case int8:
    		return int64(v), nil
    	case int32:
    		return int64(v), nil
    	case uint64:
    		return int64(v), nil
    	case uint:
    		return int64(v), nil
    	case uint8:
    		return int64(v), nil
    	case uint32:
    		return int64(v), nil
    	case float32:
    		return int64(v), nil
    	case float64:
    		return int64(v), nil
    	default:
    		return nil, fmt.Errorf("unsupported value type: \"%T\"", i)
    	}
    }
    
    func (TimestampType) Decode(_ context.Context, _ *Field, v interface{}) (interface{}, error) {
    	switch i := v.(type) {
    	case string:
    		duration, err := time.ParseDuration(i)
    		if err == nil {
    			return duration.Nanoseconds(), nil
    		}
    		t, err := time.Parse(time.TimeOnly, i)
    
    		if err == nil {
    			return t.AddDate(1, 0, 0).Sub(zeroTime).Nanoseconds(), nil
    		}
    		return nil, err
    	default:
    		return toTimestamp(i)
    	}
    }
    
    func (TimestampType) Encode(_ context.Context, _ *Field, v interface{}) (interface{}, error) {
    	switch i := v.(type) {
    	default:
    		return toTimestamp(i)
    	}
    }
    
    func Timestamp(o ...interface{}) *Field {
    	return NewField(&TimestampParameters{}, o...)
    }