From e40149018cc6ddded29a94f7378769357cba8de2 Mon Sep 17 00:00:00 2001 From: mdrakos Date: Tue, 22 Oct 2024 10:03:36 -0700 Subject: [PATCH] Move atTime out of raw --- internal/runbits/runtime/rationalize.go | 2 ++ pkg/buildscript/buildscript.go | 6 ++++-- pkg/buildscript/marshal.go | 4 ++-- pkg/buildscript/marshal_buildexpression.go | 2 +- pkg/buildscript/raw.go | 3 --- pkg/buildscript/unmarshal.go | 7 ++++--- pkg/buildscript/unmarshal_buildexpression.go | 7 ------- pkg/platform/model/buildplanner/buildscript.go | 2 +- 8 files changed, 14 insertions(+), 19 deletions(-) diff --git a/internal/runbits/runtime/rationalize.go b/internal/runbits/runtime/rationalize.go index 3feac4ac8d..60426f9850 100644 --- a/internal/runbits/runtime/rationalize.go +++ b/internal/runbits/runtime/rationalize.go @@ -122,6 +122,8 @@ func RationalizeSolveError(proj *project.Project, auth *auth.Auth, rerr *error) ) // We communicate buildplanner errors verbatim as the intend is that these are curated by the buildplanner + // TODO: Maybe we should just forward any error message here? + // This could simplify the graphQL error handling. case errors.As(*rerr, &buildPlannerErr): *rerr = errs.WrapUserFacing(*rerr, buildPlannerErr.LocaleError(), diff --git a/pkg/buildscript/buildscript.go b/pkg/buildscript/buildscript.go index fcaf7981c7..3298f5a290 100644 --- a/pkg/buildscript/buildscript.go +++ b/pkg/buildscript/buildscript.go @@ -16,6 +16,8 @@ import ( // methods that are easy to understand and work with. type BuildScript struct { raw *rawBuildScript + + atTime *time.Time // set after initial read } func init() { @@ -42,11 +44,11 @@ func New() *BuildScript { } func (b *BuildScript) AtTime() *time.Time { - return b.raw.AtTime + return b.atTime } func (b *BuildScript) SetAtTime(t time.Time) { - b.raw.AtTime = &t + b.atTime = &t } func (b *BuildScript) Equals(other *BuildScript) (bool, error) { diff --git a/pkg/buildscript/marshal.go b/pkg/buildscript/marshal.go index c647616f47..173d7b7c34 100644 --- a/pkg/buildscript/marshal.go +++ b/pkg/buildscript/marshal.go @@ -28,9 +28,9 @@ const ( func (b *BuildScript) Marshal() ([]byte, error) { buf := strings.Builder{} - if b.raw.AtTime != nil { + if b.atTime != nil { buf.WriteString(assignmentString( - &assignment{atTimeKey, newString(b.raw.AtTime.Format(strfmt.RFC3339Millis))})) + &assignment{atTimeKey, newString(b.atTime.Format(strfmt.RFC3339Millis))})) buf.WriteString("\n") } diff --git a/pkg/buildscript/marshal_buildexpression.go b/pkg/buildscript/marshal_buildexpression.go index cb8a152803..15a6f62ef7 100644 --- a/pkg/buildscript/marshal_buildexpression.go +++ b/pkg/buildscript/marshal_buildexpression.go @@ -41,7 +41,7 @@ func (b *BuildScript) MarshalBuildExpression() ([]byte, error) { if err != nil { return nil, errs.Wrap(err, "Invalid timestamp: %s", strValue(value)) } - b.raw.AtTime = ptr.To(time.Time(atTime)) + b.atTime = ptr.To(time.Time(atTime)) continue // do not include this custom assignment in the let block case mainKey: key = inKey // rename diff --git a/pkg/buildscript/raw.go b/pkg/buildscript/raw.go index 8a2dbc3149..51f6ede04f 100644 --- a/pkg/buildscript/raw.go +++ b/pkg/buildscript/raw.go @@ -3,7 +3,6 @@ package buildscript import ( "strconv" "strings" - "time" "github.com/ActiveState/cli/internal/rtutils/ptr" "github.com/brunoga/deep" @@ -12,8 +11,6 @@ import ( // Tagged fields will be filled in by Participle. type rawBuildScript struct { Assignments []*assignment `parser:"@@+"` - - AtTime *time.Time // set after initial read } // clone is meant to facilitate making modifications to functions at marshal time. The idea is that these modifications diff --git a/pkg/buildscript/unmarshal.go b/pkg/buildscript/unmarshal.go index ffbab27f9e..4ae616089f 100644 --- a/pkg/buildscript/unmarshal.go +++ b/pkg/buildscript/unmarshal.go @@ -32,6 +32,7 @@ func Unmarshal(data []byte) (*BuildScript, error) { } // Extract 'at_time' value from the list of assignments, if it exists. + var atTime *time.Time for i, assignment := range raw.Assignments { key := assignment.Key value := assignment.Value @@ -42,11 +43,11 @@ func Unmarshal(data []byte) (*BuildScript, error) { if value.Str == nil { break } - atTime, err := strfmt.ParseDateTime(strValue(value)) + parsedAtTime, err := strfmt.ParseDateTime(strValue(value)) if err != nil { return nil, errs.Wrap(err, "Invalid timestamp: %s", strValue(value)) } - raw.AtTime = ptr.To(time.Time(atTime)) + atTime = ptr.To(time.Time(parsedAtTime)) break } @@ -60,5 +61,5 @@ func Unmarshal(data []byte) (*BuildScript, error) { seen[assignment.Key] = true } - return &BuildScript{raw}, nil + return &BuildScript{raw, atTime}, nil } diff --git a/pkg/buildscript/unmarshal_buildexpression.go b/pkg/buildscript/unmarshal_buildexpression.go index 77af730583..f4639aac5f 100644 --- a/pkg/buildscript/unmarshal_buildexpression.go +++ b/pkg/buildscript/unmarshal_buildexpression.go @@ -5,9 +5,7 @@ import ( "sort" "strconv" "strings" - "time" - "github.com/go-openapi/strfmt" "golang.org/x/text/cases" "golang.org/x/text/language" @@ -70,13 +68,8 @@ func (b *BuildScript) UnmarshalBuildExpression(data []byte) error { // Extract the 'at_time' from the solve node, if it exists, and change its value to be a // reference to "$at_time", which is how we want to show it in AScript format. if atTimeNode, err := b.getSolveAtTimeValue(); err == nil && atTimeNode.Str != nil && !strings.HasPrefix(strValue(atTimeNode), `$`) { - atTime, err := strfmt.ParseDateTime(strValue(atTimeNode)) - if err != nil { - return errs.Wrap(err, "Invalid timestamp: %s", strValue(atTimeNode)) - } atTimeNode.Str = nil atTimeNode.Ident = ptr.To("at_time") - b.raw.AtTime = ptr.To(time.Time(atTime)) } else if err != nil { return errs.Wrap(err, "Could not get at_time node") } diff --git a/pkg/platform/model/buildplanner/buildscript.go b/pkg/platform/model/buildplanner/buildscript.go index 28bd980ffc..52f038ee5f 100644 --- a/pkg/platform/model/buildplanner/buildscript.go +++ b/pkg/platform/model/buildplanner/buildscript.go @@ -52,10 +52,10 @@ func (b *BuildPlanner) GetBuildScript(commitID string) (*buildscript.BuildScript } script := buildscript.New() - script.SetAtTime(time.Time(resp.Commit.AtTime)) if err := script.UnmarshalBuildExpression(resp.Commit.Expression); err != nil { return nil, errs.Wrap(err, "failed to parse build expression") } + script.SetAtTime(time.Time(resp.Commit.AtTime)) return script, nil }