mirror of
https://github.com/kjanat/articulate-parser.git
synced 2026-10-13 04:55:28 +02:00
refactor: comprehensive code quality improvements
- Add logger to all exporters for consistent error handling - Add Course.Validate() method with ValidationError type - Create shared test fixtures in internal/testdata - Refactor config passing: NewArticulateParser now takes *config.Config - Add NewLoggerFromConfig() factory for logger creation - Add context cancellation with signal handling (SIGINT/SIGTERM) - Add godoc examples for loggers and exporters - Add HTML exporter error path tests - Add fuzz tests for parser and HTMLCleaner - Fix markdown output: trim whitespace, collapse blank lines - Remove test output files from repo
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
// These structures closely match the JSON format used by Articulate Rise.
|
||||
package models
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Course represents the top-level structure of an Articulate Rise course.
|
||||
// It contains metadata and the actual course content.
|
||||
type Course struct {
|
||||
@@ -53,3 +55,46 @@ type LabelSet struct {
|
||||
// Labels is a mapping of label keys to their customized values
|
||||
Labels map[string]string `json:"labels"`
|
||||
}
|
||||
|
||||
// Validate checks whether the Course structure contains valid data.
|
||||
// It returns an error describing the first validation failure found,
|
||||
// or nil if the course is valid.
|
||||
//
|
||||
// Validation rules:
|
||||
// - ShareID must not be empty
|
||||
// - Course.ID must not be empty
|
||||
// - Course.Title must not be empty
|
||||
// - Each Lesson must have a non-empty ID and Title
|
||||
func (c *Course) Validate() error {
|
||||
if c.ShareID == "" {
|
||||
return &ValidationError{Field: "ShareID", Message: "share ID is required"}
|
||||
}
|
||||
if c.Course.ID == "" {
|
||||
return &ValidationError{Field: "Course.ID", Message: "course ID is required"}
|
||||
}
|
||||
if c.Course.Title == "" {
|
||||
return &ValidationError{Field: "Course.Title", Message: "course title is required"}
|
||||
}
|
||||
for i, lesson := range c.Course.Lessons {
|
||||
if lesson.ID == "" {
|
||||
return &ValidationError{Field: "Course.Lessons", Message: fmt.Sprintf("lesson at index %d has empty ID", i)}
|
||||
}
|
||||
if lesson.Title == "" {
|
||||
return &ValidationError{Field: "Course.Lessons", Message: fmt.Sprintf("lesson at index %d has empty title", i)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidationError represents a validation failure for a specific field.
|
||||
type ValidationError struct {
|
||||
// Field is the name of the field that failed validation
|
||||
Field string
|
||||
// Message describes the validation failure
|
||||
Message string
|
||||
}
|
||||
|
||||
// Error implements the error interface for ValidationError.
|
||||
func (e *ValidationError) Error() string {
|
||||
return fmt.Sprintf("validation error on %s: %s", e.Field, e.Message)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user