PR comments

This commit is contained in:
Mihail Vratchanski 2025-06-12 09:26:11 +03:00
parent 99d354462a
commit 1300c7b128
2 changed files with 24 additions and 20 deletions

View File

@ -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 {

View File

@ -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.