137 lines
5.4 KiB
Go
137 lines
5.4 KiB
Go
package assessment
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"slices"
|
|
"strings"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
const (
|
|
// maxBundleBytes bounds the size of a bundle document that ParseBundle
|
|
// reads.
|
|
maxBundleBytes = 1 << 20
|
|
// maxBundleFamilies bounds the family versions in one bundle, so one
|
|
// import stays one reasonably sized transaction.
|
|
maxBundleFamilies = 200
|
|
)
|
|
|
|
// Bundle is a named set of family versions of one category: one author, one
|
|
// independent review of the whole set, and the content of each version. It
|
|
// is the input of the loader and carries no grading policy: the policy is
|
|
// referenced when a category is opened.
|
|
//
|
|
// A bundle that someone prepared is not thereby reviewed. The review block
|
|
// records what a reviewer decided; a pending review has no reviewer.
|
|
type Bundle struct {
|
|
Name string `json:"name"`
|
|
Category string `json:"category"`
|
|
Author string `json:"author"`
|
|
Review BundleReview `json:"review"`
|
|
Families []BundleFamily `json:"families"`
|
|
}
|
|
|
|
// BundleReview is the independent review of a bundle. Reviewer is empty while
|
|
// Status is "pending".
|
|
type BundleReview struct {
|
|
Status string `json:"status"`
|
|
Reviewer string `json:"reviewer"`
|
|
}
|
|
|
|
// BundleFamily is the content of one family version in a bundle.
|
|
type BundleFamily struct {
|
|
Key string `json:"key"`
|
|
Version int32 `json:"version"`
|
|
Specialization string `json:"specialization"`
|
|
Title string `json:"title"`
|
|
SeedGenerator domain.AssessmentSeedGenerator `json:"seed_generator"`
|
|
AnswerKey domain.AssessmentAnswerKey `json:"answer_key"`
|
|
Rubric domain.AssessmentRubric `json:"rubric"`
|
|
}
|
|
|
|
// ParseBundle reads a bundle document strictly: an unknown field, trailing
|
|
// data, or a document above the size bound is an error wrapping
|
|
// domain.ErrInvalidAssessmentFamily. It does not validate the content; Drafts
|
|
// does.
|
|
func ParseBundle(reader io.Reader) (Bundle, error) {
|
|
const location = "assessment.ParseBundle"
|
|
content, err := io.ReadAll(io.LimitReader(reader, maxBundleBytes+1))
|
|
if err != nil {
|
|
return Bundle{}, fmt.Errorf("%s - read bundle: %w", location, err)
|
|
}
|
|
if len(content) > maxBundleBytes {
|
|
return Bundle{}, fmt.Errorf("%s - bundle exceeds %d bytes: %w", location, maxBundleBytes, domain.ErrInvalidAssessmentFamily)
|
|
}
|
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
|
decoder.DisallowUnknownFields()
|
|
var bundle Bundle
|
|
if err := decoder.Decode(&bundle); err != nil {
|
|
return Bundle{}, fmt.Errorf("%s - decode bundle: %w: %w", location, domain.ErrInvalidAssessmentFamily, err)
|
|
}
|
|
if decoder.More() {
|
|
return Bundle{}, fmt.Errorf("%s - trailing data after the bundle: %w", location, domain.ErrInvalidAssessmentFamily)
|
|
}
|
|
return bundle, nil
|
|
}
|
|
|
|
// Drafts builds the validated drafts of every family version, ordered by key
|
|
// and version, so that two versions of one family are stored in order. It
|
|
// returns an error wrapping domain.ErrInvalidAssessmentFamily for an invalid
|
|
// bundle, a repeated key and version, or a family set that is empty or too
|
|
// large. Every draft is imported as a draft whatever its review says;
|
|
// publication is a separate decision.
|
|
func (bundle Bundle) Drafts() ([]domain.AssessmentFamilyDraft, error) {
|
|
const location = "Bundle.Drafts"
|
|
name, err := domain.ParseAssessmentBundleName(bundle.Name)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - validate name: %w", location, err)
|
|
}
|
|
category, err := domain.ParseAssessmentCategory(bundle.Category)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - validate category: %w", location, err)
|
|
}
|
|
author, err := domain.ParseAssessmentAuthor(bundle.Author)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - validate author: %w", location, err)
|
|
}
|
|
if len(bundle.Families) == 0 || len(bundle.Families) > maxBundleFamilies {
|
|
return nil, fmt.Errorf("%s - bundle must have 1 to %d families: %w", location, maxBundleFamilies, domain.ErrInvalidAssessmentFamily)
|
|
}
|
|
families := slices.Clone(bundle.Families)
|
|
slices.SortFunc(families, func(left, right BundleFamily) int {
|
|
if byKey := strings.Compare(left.Key, right.Key); byKey != 0 {
|
|
return byKey
|
|
}
|
|
return int(left.Version) - int(right.Version)
|
|
})
|
|
drafts := make([]domain.AssessmentFamilyDraft, 0, len(families))
|
|
for index, family := range families {
|
|
if index > 0 && family.Key == families[index-1].Key && family.Version == families[index-1].Version {
|
|
return nil, fmt.Errorf("%s - family %s version %d appears twice: %w", location, family.Key, family.Version, domain.ErrInvalidAssessmentFamily)
|
|
}
|
|
draft, err := domain.NewAssessmentFamilyDraft(domain.NewAssessmentFamilyDraftInput{
|
|
Key: domain.AssessmentFamilyKey(family.Key),
|
|
Version: family.Version,
|
|
Category: category,
|
|
Specialization: domain.AssessmentSpecialization(family.Specialization),
|
|
Title: family.Title,
|
|
BundleName: name,
|
|
Author: author,
|
|
Reviewer: domain.AssessmentOperator(bundle.Review.Reviewer),
|
|
ReviewStatus: domain.AssessmentReviewStatus(bundle.Review.Status),
|
|
Generator: family.SeedGenerator,
|
|
AnswerKey: family.AnswerKey,
|
|
Rubric: family.Rubric,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - family %s version %d: %w", location, family.Key, family.Version, err)
|
|
}
|
|
drafts = append(drafts, draft)
|
|
}
|
|
return drafts, nil
|
|
}
|