Skip to content

Commit

Permalink
chore: moved date exported method to internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
skartikey committed May 16, 2022
1 parent 37f8fb0 commit 7eeac7a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
40 changes: 40 additions & 0 deletions internal/date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/internal/zoneinfo"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)

Expand All @@ -25,3 +27,41 @@ func GetTimeInLocation(t values.Time, location string, offset values.Duration) (
}
return values.NewTime(t), nil
}

func GetLocationFromObjArgs(args values.Object) (string, values.Duration, error) {
a := interpreter.NewArguments(args)
return GetLocationFromFluxArgs(a)
}

func GetLocationFromFluxArgs(args interpreter.Arguments) (string, values.Duration, error) {
location, err := args.GetRequiredObject("location")
if err != nil {
return "UTC", values.ConvertDurationNsecs(0), err
}
return GetLocation(location)
}

func GetLocation(location values.Object) (string, values.Duration, error) {
var (
name, offset values.Value
ok bool
)

name, ok = location.Get("zone")
if !ok {
return "UTC", values.ConvertDurationNsecs(0), errors.New(codes.Invalid, "zone property missing from location record")
} else if got := name.Type().Nature(); got != semantic.String {
return "UTC", values.ConvertDurationNsecs(0), errors.Newf(codes.Invalid, "zone property for location must be of type %s, got %s", semantic.String, got)
}

if offset, ok = location.Get("offset"); ok {
if got := offset.Type().Nature(); got != semantic.Duration {
return "UTC", values.ConvertDurationNsecs(0), errors.Newf(codes.Invalid, "offset property for location must be of type %s, got %s", semantic.Duration, got)
}
}

if name.IsNull() {
return "UTC", offset.Duration(), nil
}
return name.Str(), offset.Duration(), nil
}
4 changes: 2 additions & 2 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,8 @@ func TestStack(t *testing.T) {
FunctionName: "window",
Location: ast.SourceLocation{
File: "universe/universe.flux",
Start: ast.Position{Line: 3715, Column: 12},
End: ast.Position{Line: 3715, Column: 51},
Start: ast.Position{Line: 3716, Column: 12},
End: ast.Position{Line: 3716, Column: 51},
Source: `window(every: inf, timeColumn: timeDst)`,
},
},
Expand Down
2 changes: 1 addition & 1 deletion libflux/go/libflux/buildinfo.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ var sourceHashes = map[string]string{
"stdlib/universe/union_heterogeneous_test.flux": "5158d9efdf88ec10945e84f72cb7ed9669b25f967b7b4f778d2456d52ecd34da",
"stdlib/universe/union_test.flux": "8529f2f609d9876f975ad648dec24eb60b569e8ac1c38e4f980e2234e68149a9",
"stdlib/universe/unique_test.flux": "02180f651906f48cc57cc777d443a64bd2155394ac0ade68888f47f5900e5408",
"stdlib/universe/universe.flux": "4dee405c283abb4d8c88a8daf5da190cfa163cc801c9568f4e85494e728c3ee6",
"stdlib/universe/universe.flux": "c8a3bbd18143113965ca29f94e8ff86c68eea21427fb1e971b4b8334ff2f8802",
"stdlib/universe/universe_truncateTimeColumn_test.flux": "7ca30c57336b22ae430a082d33589fa919f4558c3ed6dbe37ecde367d602435c",
"stdlib/universe/window_aggregate_test.flux": "e2dba18647b2a180cafe81dd00a43077eb489421d229785e63b270eb79ddb2b1",
"stdlib/universe/window_default_start_align_test.flux": "162f9d452fed411edfdcb11ef1b8ae0c565d87bbc351596d277c7db55fe67e8f",
Expand Down
60 changes: 10 additions & 50 deletions stdlib/date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/date"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/interval"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)

Expand All @@ -39,7 +37,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -58,7 +56,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -77,7 +75,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -96,7 +94,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -115,7 +113,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -134,7 +132,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -153,7 +151,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -172,7 +170,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -192,7 +190,7 @@ func init() {
if err != nil {
return nil, err
}
location, offset, err := getLocationFromObjArgs(args)
location, offset, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -262,7 +260,7 @@ func init() {
if err != nil {
return nil, err
}
location, _, err := getLocationFromObjArgs(args)
location, _, err := date.GetLocationFromObjArgs(args)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -307,44 +305,6 @@ func getTime(args values.Object) (values.Value, error) {
return tArg, nil
}

func getLocationFromObjArgs(args values.Object) (string, values.Duration, error) {
a := interpreter.NewArguments(args)
return GetLocationFromFluxArgs(a)
}

func GetLocationFromFluxArgs(args interpreter.Arguments) (string, values.Duration, error) {
location, err := args.GetRequiredObject("location")
if err != nil {
return "UTC", values.ConvertDurationNsecs(0), err
}
return getLocation(location)
}

func getLocation(location values.Object) (string, values.Duration, error) {
var (
name, offset values.Value
ok bool
)

name, ok = location.Get("zone")
if !ok {
return "UTC", values.ConvertDurationNsecs(0), errors.New(codes.Invalid, "zone property missing from location record")
} else if got := name.Type().Nature(); got != semantic.String {
return "UTC", values.ConvertDurationNsecs(0), errors.Newf(codes.Invalid, "zone property for location must be of type %s, got %s", semantic.String, got)
}

if offset, ok = location.Get("offset"); ok {
if got := offset.Type().Nature(); got != semantic.Duration {
return "UTC", values.ConvertDurationNsecs(0), errors.Newf(codes.Invalid, "offset property for location must be of type %s, got %s", semantic.Duration, got)
}
}

if name.IsNull() {
return "UTC", offset.Duration(), nil
}
return name.Str(), offset.Duration(), nil
}

func getTimeableTime(ctx context.Context, args values.Object) (values.Time, error) {
var tm values.Time
t, err := getTime(args)
Expand Down
3 changes: 2 additions & 1 deletion stdlib/date/durations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/date"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/internal/function"
"github.com/influxdata/flux/internal/zoneinfo"
Expand Down Expand Up @@ -63,7 +64,7 @@ func addDuration(ctx context.Context, t values.Value, d values.Duration, scale i
return nil, err
}

name, offset, err := getLocation(loc)
name, offset, err := date.GetLocation(loc)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions stdlib/universe/hour_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
datepackage "github.com/influxdata/flux/stdlib/date"
"github.com/influxdata/flux/values"
)

Expand Down Expand Up @@ -50,7 +49,7 @@ func createHourSelectionOpSpec(args flux.Arguments, a *flux.Administration) (flu
}
spec.Stop = stop

location, offset, err := datepackage.GetLocationFromFluxArgs(args)
location, offset, err := date.GetLocationFromFluxArgs(args)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7eeac7a

Please sign in to comment.