From 50601dc0444cdfc8ba493b9d943ade2a8aa0924c Mon Sep 17 00:00:00 2001 From: Hugo Wetterberg Date: Thu, 5 Mar 2026 22:21:45 +0100 Subject: [PATCH] use "#" instead of "+" as the variant type separator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "+" separator for document type variants (e.g. "core/article+template") collides with existing compound document types like "core/article+meta" where "+" is already part of the base type name. Switch to "#" as the separator, borrowing from URL fragment semantics where a fragment refers to "the same resource, but a different view". This makes "core/article#template" unambiguous — the part after "#" is always the variant suffix. Also removes the obsolete update-test-results.sh and update-entities.sh shell scripts. The CLI no longer has a "document" subcommand, so update-test-results.sh would silently overwrite golden files with empty output. The tests already support regeneration natively via REGENERATE=true. --- README.md | 10 +++++++--- document_constraint.go | 8 ++++---- testdata/example-article-template.json | 2 +- testdata/geo-info-template.json | 2 +- update-entities.sh | 14 -------------- update-test-results.sh | 12 ------------ validation_test.go | 8 ++++---- 7 files changed, 17 insertions(+), 39 deletions(-) delete mode 100755 update-entities.sh delete mode 100755 update-test-results.sh diff --git a/README.md b/README.md index 3f4c553..f6613d4 100644 --- a/README.md +++ b/README.md @@ -386,7 +386,7 @@ A list of available block attributes, and whether they can be used in pattern ma ## Document type variants -Document type variants allow documents to use a suffixed type like `"core/article+template"` and still match the base declaration `"core/article"`. Variants are configured on the validator, not in constraint sets, so that the set of allowed suffixes is controlled by the application. +Document type variants allow documents to use a suffixed type like `"core/article#template"` and still match the base declaration `"core/article"`. Variants are configured on the validator, not in constraint sets, so that the set of allowed suffixes is controlled by the application. Configure variants using `WithVariants`: @@ -396,7 +396,7 @@ if err != nil { log.Fatal(err) } -// Allow "+template" for all declared document types. +// Allow "#template" for all declared document types. validator = validator.WithVariants(revisor.Variant{ Name: "template", }) @@ -421,7 +421,11 @@ Variants are preserved across `WithConstraints` calls. Revisor implements a file-driven test in `TestValidateDocument` that checks so that all the "testdata/results/*.json" files match the validation results for the corresponding document under "testdata/". Result files with the prefix "base-" will be validated against "constraints/naviga.json", for result files with the prefix "example-" the "constraints/example.json" constraints will be used as well. -If the constraints have been updated, or new example documents have been added, the result files can be regenerated using `./update-test-results.sh`. +If the constraints have been updated, or new example documents have been added, the result files can be regenerated by running the tests with the `REGENERATE` environment variable set: + +``` +REGENERATE=true go test -run 'TestValidateDocument|TestCollection|TestDeprecation' ./... +``` ### Benchmarks diff --git a/document_constraint.go b/document_constraint.go index 4b58567..4d5afaa 100644 --- a/document_constraint.go +++ b/document_constraint.go @@ -147,8 +147,8 @@ func documentAttribute(d *newsdoc.Document, name string) (string, bool) { } // Variant defines a document type variant suffix. When a document type -// contains a "+" separator (e.g. "core/article+template"), the part after the -// last "+" is matched against configured variants. If Types is empty, the +// contains a "#" separator (e.g. "core/article#template"), the part after the +// last "#" is matched against configured variants. If Types is empty, the // variant applies to all declared document types. type Variant struct { Name string `json:"name"` @@ -156,10 +156,10 @@ type Variant struct { } // resolveVariant returns the base document type if the suffix after the last -// "+" matches a configured variant (and the base type is allowed for that +// "#" matches a configured variant (and the base type is allowed for that // variant). Returns docType unchanged if no variant matches. func resolveVariant(docType string, variants []Variant) string { - idx := strings.LastIndex(docType, "+") + idx := strings.LastIndex(docType, "#") if idx == -1 { return docType } diff --git a/testdata/example-article-template.json b/testdata/example-article-template.json index 2489e5a..9214b08 100644 --- a/testdata/example-article-template.json +++ b/testdata/example-article-template.json @@ -1,6 +1,6 @@ { "uuid": "98cedf81-ce8d-4dc4-bcbc-6091a756cc5c", - "type": "core/article+template", + "type": "core/article#template", "uri": "core://article/98cedf81-ce8d-4dc4-bcbc-6091a756cc5c", "title": "champ-inter", "content": [ diff --git a/testdata/geo-info-template.json b/testdata/geo-info-template.json index e685105..e19e396 100644 --- a/testdata/geo-info-template.json +++ b/testdata/geo-info-template.json @@ -1,5 +1,5 @@ { - "type": "core/place+template", + "type": "core/place#template", "uuid": "d9a8a437-3ed3-4b97-b9fc-cf236b6dc5e7", "title": "Some place", "meta": [ diff --git a/update-entities.sh b/update-entities.sh deleted file mode 100755 index bfc2c99..0000000 --- a/update-entities.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash - -echo "package revisor - -//nolint:misspell -var namedEntities = [][]byte{" > html_entities_data.go - -curl https://html.spec.whatwg.org/entities.json \ - | jq 'keys' | jq -r '.[]' | grep ';' \ - | awk -F'&' '{print "[]byte(\"" $2 "\"),"}' >> html_entities_data.go - -echo "}" >> html_entities_data.go - -gofmt -w html_entities_data.go diff --git a/update-test-results.sh b/update-test-results.sh deleted file mode 100755 index 2d7bf77..0000000 --- a/update-test-results.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -for file in testdata/*.json -do - name=$(basename $file) - go run ./cmd/revisor document \ - -json ./testdata/$name \ - > testdata/results/base-$name - go run ./cmd/revisor document -spec constraints/tt.json \ - -json ./testdata/$name \ - > testdata/results/example-$name -done diff --git a/validation_test.go b/validation_test.go index 800517f..305d80a 100644 --- a/validation_test.go +++ b/validation_test.go @@ -433,7 +433,7 @@ func TestTemplateDocumentType(t *testing.T) { t.Run("TemplateMatchesDeclares", func(t *testing.T) { doc := newsdoc.Document{ - Type: "core/article+template", + Type: "core/article#template", UUID: "00000000-0000-0000-0000-000000000001", URI: "article://test/1", } @@ -452,7 +452,7 @@ func TestTemplateDocumentType(t *testing.T) { t.Run("UnsupportedSuffixRejected", func(t *testing.T) { doc := newsdoc.Document{ - Type: "core/article+other", + Type: "core/article#other", UUID: "00000000-0000-0000-0000-000000000001", URI: "article://test/1", } @@ -473,7 +473,7 @@ func TestTemplateDocumentType(t *testing.T) { } if !found { - t.Error("expected undeclared document type error for +other suffix") + t.Error("expected undeclared document type error for #other suffix") } }) @@ -505,7 +505,7 @@ func TestTemplateDocumentType(t *testing.T) { ) doc := newsdoc.Document{ - Type: "core/article+template", + Type: "core/article#template", UUID: "00000000-0000-0000-0000-000000000001", URI: "article://test/1", }