-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvention.go
More file actions
241 lines (224 loc) · 7.14 KB
/
Copy pathconvention.go
File metadata and controls
241 lines (224 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package config
import (
"fmt"
"strings"
"gopkg.in/yaml.v3"
"github.com/jeduden/mdsmith/internal/convention"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/yamlutil"
)
// applyConvention reads the top-level Convention selector from the
// loaded config (if any) and stores its rule presets on
// cfg.ConventionPreset. The preset is applied as a base layer
// beneath the user's own rule config during effective-rule
// resolution; cfg.Rules is left untouched here so per-file
// provenance (`mdsmith kinds resolve`) can show the convention as
// its own layer rather than collapsing it into the default layer.
//
// Validation:
//
// - Unknown convention name → error naming the field and listing
// valid names.
// - Convention and a user-supplied rules.markdown-flavor.flavor
// disagree → error naming both values. A convention sets a
// flavor; a user-supplied flavor that does not match is
// rejected at config load so the error surfaces once, not on
// every check.
func applyConvention(cfg *Config) error {
if cfg == nil {
return nil
}
// Validate and convert all user-defined conventions upfront, even
// when no convention is selected — this catches typos at load time.
userMap, err := buildUserConventionMap(cfg)
if err != nil {
return err
}
if cfg.Convention == "" {
return nil
}
conv, err := convention.Lookup(cfg.Convention, userMap)
if err != nil {
return fmt.Errorf("convention: %w", err)
}
if rc, ok := cfg.Rules["markdown-flavor"]; ok && conv.Flavor != convention.FlavorAny {
// A convention with FlavorAny is renderer-agnostic (e.g.
// no-llm-tells); it imposes no flavor and never conflicts with a
// user's markdown-flavor selection, so the guard is skipped.
userFlavor, err := stringSetting(
rc.Settings, "flavor", "rules.markdown-flavor.flavor",
)
if err != nil {
return err
}
if userFlavor != "" && userFlavor != conv.Flavor.String() {
return fmt.Errorf(
"rules.markdown-flavor: convention %q requires flavor %q, but flavor is set to %q",
conv.Name, conv.Flavor, userFlavor,
)
}
}
preset := make(map[string]RuleCfg, len(conv.Rules))
for ruleName, p := range conv.Rules {
preset[ruleName] = RuleCfg{
Enabled: p.Enabled,
Settings: cloneSettings(p.Settings),
}
}
cfg.ConventionPreset = preset
return nil
}
// stringSetting reads a string-typed setting from a settings map. A
// missing key returns "" with no error; a present key with a
// non-string value returns an error naming the offending field path
// so users see the problem at config load time.
func stringSetting(settings map[string]any, key, fieldPath string) (string, error) {
v, ok := settings[key]
if !ok {
return "", nil
}
s, ok := v.(string)
if !ok {
return "", fmt.Errorf("%s: must be a string, got %T", fieldPath, v)
}
return s, nil
}
// copyConventionPreset returns a deep copy of a convention preset
// map. Each RuleCfg's settings map is cloned so callers can mutate
// the result without affecting the source.
func copyConventionPreset(p map[string]RuleCfg) map[string]RuleCfg {
if p == nil {
return nil
}
out := make(map[string]RuleCfg, len(p))
for k, v := range p {
out[k] = copyRuleCfg(v)
}
return out
}
// buildUserConventionMap validates every entry in cfg.Conventions and
// returns them as a map[string]convention.Convention ready for
// Lookup. Validation checks:
// - The name must not be a reserved built-in name.
// - The flavor must be a recognised flavor string.
// - Each rule name must be registered.
// - Each rule's settings must pass the rule's own ApplySettings
// check (called on a cloned instance so the registry is unaffected).
//
// Returns nil when cfg.Conventions is empty.
func buildUserConventionMap(cfg *Config) (map[string]convention.Convention, error) {
if len(cfg.Conventions) == 0 {
return nil, nil
}
reserved := make(map[string]bool, len(convention.Names()))
for _, name := range convention.Names() {
reserved[name] = true
}
result := make(map[string]convention.Convention, len(cfg.Conventions))
for name, uc := range cfg.Conventions {
if reserved[name] {
return nil, fmt.Errorf(
"conventions.%s: name is reserved by a built-in convention",
name,
)
}
fl, ok := convention.ParseFlavor(uc.Flavor)
if !ok {
return nil, fmt.Errorf(
"convention %q: unknown flavor %q",
name, uc.Flavor,
)
}
rules := make(map[string]convention.RulePreset, len(uc.Rules))
for ruleName, rc := range uc.Rules {
r := rule.ByName(ruleName)
if r == nil {
return nil, fmt.Errorf(
"convention %q: unknown rule %q",
name, ruleName,
)
}
if len(rc.Settings) > 0 {
if err := validateConventionRuleSettings(r, name, ruleName, rc.Settings); err != nil {
return nil, err
}
}
rules[ruleName] = convention.RulePreset{
Enabled: rc.Enabled,
Settings: cloneSettings(rc.Settings),
}
}
result[name] = convention.Convention{
Name: name,
Flavor: fl,
Rules: rules,
}
}
return result, nil
}
// validateConventionRuleSettings clones the rule and calls
// ApplySettings to check that settings is well-formed. Using a clone
// avoids mutating the shared singleton in the rule registry.
func validateConventionRuleSettings(
r rule.Rule,
conventionName, ruleName string,
settings map[string]any,
) error {
if _, ok := r.(rule.Configurable); !ok {
return fmt.Errorf(
"convention %q rule %q: rule has no configurable settings",
conventionName, ruleName,
)
}
cc := rule.CloneRule(r).(rule.Configurable)
if err := cc.ApplySettings(settings); err != nil {
return fmt.Errorf("convention %q rule %q: %w", conventionName, ruleName, err)
}
return nil
}
// validateConventionScalar rejects YAML that uses anchors or
// aliases anywhere in the document, then returns an error when
// the top-level `convention:` value in the raw YAML is not a
// string scalar. yaml.v3 silently coerces bare ints and bools
// into string fields, which would surface as "unknown
// convention 123" instead of a clean type error. Inspecting the
// raw node tag is the only way to catch the type mismatch before
// that coercion happens.
func validateConventionScalar(data []byte) error {
// Reject anchors/aliases up front, as the kind-file and
// convention-file loaders do. The direct yaml.Unmarshal
// below is deliberate: its parse errors must stay swallowed
// so the caller's follow-up UnmarshalSafe (Load and
// ParseBytes share that pipeline) reports them instead.
if err := yamlutil.RejectYAMLAliases(data); err != nil {
return err
}
var node yaml.Node
if err := yaml.Unmarshal(data, &node); err != nil {
return nil
}
if node.Kind != yaml.DocumentNode || len(node.Content) == 0 {
return nil
}
mapping := node.Content[0]
if mapping.Kind != yaml.MappingNode {
return nil
}
for i := 0; i+1 < len(mapping.Content); i += 2 {
if mapping.Content[i].Value != "convention" {
continue
}
v := mapping.Content[i+1]
if v.Kind != yaml.ScalarNode {
return fmt.Errorf("convention: must be a string scalar")
}
if v.Tag != "" && v.Tag != "!!str" {
return fmt.Errorf(
"convention: must be a string, got %s",
strings.TrimPrefix(v.Tag, "!!"),
)
}
return nil
}
return nil
}