carlos / todomvc-bakeoff Public

Clone
git clone https://amadan.net/carlos/todomvc-bakeoff

Plain git — no account needed to clone.

Download

Download this file

1package bakeoff
2
3import (
4 "encoding/json"
5 "errors"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "slices"
10)
11
12// ResultsDir is where results/<slug>.json live, relative to the repo root.
13var ResultsDir = "results"
14
15// Readability is Fable's blind rubric score (per-field median of 3 runs).
16type Readability struct {
17 Naming, Structure, Traceability, Errors, Comments, Overall float64
18 Notes string
19}
20
21// Behaviour is one project's run of the shared suite.
22type Behaviour struct{ Passed, Failed, Skipped int }
23
24// AppResult is everything the report shows for one app.
25type AppResult struct {
26 Slug, Language, Status, URL, Version, Stack string
27 Source string
28 BehaviourJSOn, BehaviourJSOff Behaviour
29 TTFBEmpty, TTFB50 Stats
30 Wake []float64
31 Load []LoadResult
32 Weight Weight
33 Memory Memory
34 Code CodeStats
35 Build Build
36 TokensBuild, TokensFix, TokensSecurity Tokens
37 TokensReview Tokens
38 Readability Readability
39 ReviewFindings []string
40 Notes []string
41}
42
43// LoadIn reads dir/<slug>.json, or returns an empty result for slug.
44func LoadIn(dir, slug string) (AppResult, error) {
45 r := AppResult{Slug: slug}
46 b, err := os.ReadFile(filepath.Join(dir, slug+".json"))
47 if errors.Is(err, fs.ErrNotExist) {
48 return r, nil
49 }
50 if err != nil {
51 return r, err
52 }
53 err = json.Unmarshal(b, &r)
54 return r, err
55}
56
57// UpdateIn applies f to dir/<slug>.json and writes it back.
58func UpdateIn(dir, slug string, f func(*AppResult)) error {
59 r, err := LoadIn(dir, slug)
60 if err != nil {
61 return err
62 }
63 f(&r)
64 r.Slug = slug
65 b, err := json.MarshalIndent(r, "", " ")
66 if err != nil {
67 return err
68 }
69 if err := os.MkdirAll(dir, 0o755); err != nil {
70 return err
71 }
72 return os.WriteFile(filepath.Join(dir, slug+".json"), append(b, '\n'), 0o644)
73}
74
75// Update applies f to results/<slug>.json.
76func Update(slug string, f func(*AppResult)) error { return UpdateIn(ResultsDir, slug, f) }
77
78// Record is a partial update of an app's identity fields; empty fields
79// leave the stored value alone.
80type Record struct{ Language, Status, URL, Version, Stack, Source, Note string }
81
82// ApplyRecord copies the non-empty fields of rec into r.
83func ApplyRecord(r *AppResult, rec Record) {
84 set := func(dst *string, v string) {
85 if v != "" {
86 *dst = v
87 }
88 }
89 set(&r.Language, rec.Language)
90 set(&r.Status, rec.Status)
91 set(&r.URL, rec.URL)
92 set(&r.Version, rec.Version)
93 set(&r.Stack, rec.Stack)
94 set(&r.Source, rec.Source)
95 if rec.Note != "" && !slices.Contains(r.Notes, rec.Note) {
96 r.Notes = append(r.Notes, rec.Note)
97 }
98}
99