From 1300c7b1287523555cb572419f7dd83825e2eb7b Mon Sep 17 00:00:00 2001 From: Mihail Vratchanski Date: Thu, 12 Jun 2025 09:26:11 +0300 Subject: [PATCH] PR comments --- pkg/yqlib/datetime_preprocessor.go | 39 ++++++++++++++++-------------- pkg/yqlib/decoder_goccy_yaml.go | 5 ++-- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pkg/yqlib/datetime_preprocessor.go b/pkg/yqlib/datetime_preprocessor.go index aab94943..ecc6f3da 100644 --- a/pkg/yqlib/datetime_preprocessor.go +++ b/pkg/yqlib/datetime_preprocessor.go @@ -16,16 +16,6 @@ func NewDateTimePreprocessor(enabled bool) *DateTimePreprocessor { return &DateTimePreprocessor{enabled: enabled} } -// ISO8601 date/time patterns that should be automatically tagged as timestamps -var dateTimePatterns = []*regexp.Regexp{ - // RFC3339 / ISO8601 with timezone: 2006-01-02T15:04:05Z or 2006-01-02T15:04:05+07:00 - regexp.MustCompile(`^\s*([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?(?:Z|[+-][0-9]{2}:[0-9]{2}))\s*$`), - // Date only: 2006-01-02 - regexp.MustCompile(`^\s*([0-9]{4}-[0-9]{2}-[0-9]{2})\s*$`), - // RFC3339 without timezone: 2006-01-02T15:04:05 - regexp.MustCompile(`^\s*([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?)\s*$`), -} - // isValidDateTime checks if a string represents a valid ISO8601/RFC3339 datetime func isValidDateTime(value string) bool { // Try to parse with RFC3339 first @@ -34,12 +24,12 @@ func isValidDateTime(value string) bool { } // Try to parse date-only format - if _, err := time.Parse("2006-01-02", value); err == nil { + if _, err := time.Parse(time.DateOnly, value); err == nil { return true } // Try RFC3339 without timezone - if _, err := time.Parse("2006-01-02T15:04:05", value); err == nil { + if _, err := time.Parse(strings.TrimSuffix(time.RFC3339, "Z07:00"), value); err == nil { return true } @@ -94,9 +84,10 @@ func isSkippableLine(line string) bool { trimmed := strings.TrimSpace(line) // Skip empty lines, comments, directives, document separators - if trimmed == "" || strings.HasPrefix(trimmed, "#") || + switch { + case trimmed == "" || strings.HasPrefix(trimmed, "#") || strings.HasPrefix(trimmed, "%") || strings.HasPrefix(trimmed, "---") || - strings.HasPrefix(trimmed, "...") { + strings.HasPrefix(trimmed, "..."): return true } @@ -106,9 +97,10 @@ func isSkippableLine(line string) bool { } // Skip multi-line scalar indicators - if strings.HasSuffix(trimmed, "|") || strings.HasSuffix(trimmed, ">") || + switch { + case strings.HasSuffix(trimmed, "|") || strings.HasSuffix(trimmed, ">") || strings.HasSuffix(trimmed, "|-") || strings.HasSuffix(trimmed, ">-") || - strings.HasSuffix(trimmed, "|+") || strings.HasSuffix(trimmed, ">+") { + strings.HasSuffix(trimmed, "|+") || strings.HasSuffix(trimmed, ">+"): return true } @@ -161,13 +153,14 @@ func (dtp *DateTimePreprocessor) processArrayItemLine(line string) string { trimmedValue := strings.TrimSpace(afterDash) // Skip if value is empty, quoted, or already complex - if trimmedValue == "" || + switch { + case trimmedValue == "" || strings.HasPrefix(trimmedValue, "\"") || strings.HasPrefix(trimmedValue, "'") || strings.HasPrefix(trimmedValue, "{") || strings.HasPrefix(trimmedValue, "[") || strings.HasPrefix(trimmedValue, "&") || - strings.HasPrefix(trimmedValue, "*") { + strings.HasPrefix(trimmedValue, "*"): return line } @@ -180,6 +173,16 @@ func (dtp *DateTimePreprocessor) processArrayItemLine(line string) string { return line } +// ISO8601 date/time patterns that should be automatically tagged as timestamps +var dateTimePatterns = []*regexp.Regexp{ + // RFC3339 / ISO8601 with timezone: 2006-01-02T15:04:05Z or 2006-01-02T15:04:05+07:00 + regexp.MustCompile(`^\s*([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?(?:Z|[+-][0-9]{2}:[0-9]{2}))\s*$`), + // Date only: 2006-01-02 + regexp.MustCompile(`^\s*([0-9]{4}-[0-9]{2}-[0-9]{2})\s*$`), + // RFC3339 without timezone: 2006-01-02T15:04:05 + regexp.MustCompile(`^\s*([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?)\s*$`), +} + // matchesDateTimePattern checks if a value matches any of the datetime regex patterns func (dtp *DateTimePreprocessor) matchesDateTimePattern(value string) bool { for _, pattern := range dateTimePatterns { diff --git a/pkg/yqlib/decoder_goccy_yaml.go b/pkg/yqlib/decoder_goccy_yaml.go index 0343f316..3755c3ac 100644 --- a/pkg/yqlib/decoder_goccy_yaml.go +++ b/pkg/yqlib/decoder_goccy_yaml.go @@ -24,6 +24,9 @@ const ( docSeparatorPlaceholder = "$yqDocSeparator$" ) +var commentLineRegEx = regexp.MustCompile(`^\s*#`) +var yamlDirectiveLineRegEx = regexp.MustCompile(`^\s*%YA`) // e.g., %YAML + type goccyYamlDecoder struct { decoder yaml.Decoder cm yaml.CommentMap @@ -62,8 +65,6 @@ func NewGoccyYAMLDecoder(prefs YamlPreferences) Decoder { // directives, and document separators before the main YAML content is parsed. // It returns a reader for the remaining stream and the extracted leading content. func (dec *goccyYamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, string, error) { - var commentLineRegEx = regexp.MustCompile(`^\s*#`) - var yamlDirectiveLineRegEx = regexp.MustCompile(`^\s*%YA`) // e.g., %YAML var sb strings.Builder for { peekBytes, err := reader.Peek(4) // Peek up to 4 bytes to identify line types.