Skip to content

openapi: make the nil-safe slot readers of a statement methods of statement.Schema #2073

In progress

Description

@OmarAlJarrah

Problem

statement.Schema holds its keywords in pointer slots (Kinds, Values, Format, Text, Array, Object, Choice, Logic, Notes), each nil where the schema writes no keyword of it. Every reader that wants one slot's value first steps past the nil pointer, and each package wrote its own function for it.

$ git grep -nE '^func [a-zA-Z]+In\([a-z]+ \*statement\.Schema\) ' -- 'compilers/*.go' ':!*_test.go' | grep -v looseIn | wc -l
17
$ git grep -nE '^func namedFormat\(' -- 'compilers/*.go'
compilers/openapi/internal/schema/scalar.go:51:func namedFormat(s *statement.Schema) string {

(looseIn is a predicate over Logic, not a slot reader.) Seventeen ...In functions and namedFormat make eighteen readers of one shape, three of them declared twice (objectIn, arrayIn, discriminatorIn, in schema and in annotation), with #1996 (at 6630476; the statement package does not exist on main).

That is, in schema: kindsIn, enumIn, constIn, choiceIn, objectIn, arrayIn, allOfIn, discriminatorIn, textIn, namedFormat; in annotation: notesIn, extensionsIn, arrayIn, objectIn, logicIn, xmlIn, externalDocsIn, discriminatorIn. Each is a nil check and a field read (objectIn: if s == nil || s.Object == nil { return statement.Object{} }; return *s.Object). objectIn alone has 35 call sites (30 in schema, 5 in annotation; git grep -nw objectIn -- 'compilers/openapi/internal/schema/*.go' ':!*_test.go' | grep -vc ':func '). Three more of the same shape sit beside them under other names: defaultOf, prefixItems and (reading two slots) nullableTrue.

Three of them exist twice, so a change to one reaches only its own package's callers.

Cause

statement.Schema has four methods (Held, Property, Key, find) and no accessor for a slot, so each consumer wrote one beside its own call sites.

Direction

A draft. Declare each once, as a method of *statement.Schema that tolerates a nil receiver (s.Object(), s.Array(), s.Choice(), s.Kinds(), ...), and delete the functions. A method on a nil pointer receiver is idiomatic where the zero value is the answer, and Held and Property already do. A name that collides with the slot field (Object the field and Object() the method) needs a different spelling for one or the other; settle it in the change.

Done when the command above lists none but looseIn and go test ./compilers/... is unchanged. No document, golden or IR changes.

Out of scope

Readers that do more than step past a nil slot (itemsSchema, which also reads whether items is a list, declaredInstanceTypes). Whether the slots themselves should be values rather than pointers.

Related: #1996, #1991

Priority: P4, rule 1 (a refactor that changes no lowering, validation, IR or emit-contract behaviour; it unblocks no higher-tier work). git grep counts above at 6630476.

Activity

  1. added
    type:chore
    priority:P4
    on Nov 18, 2026
  2. OmarAlJarrah commented on Nov 18, 2026

    @OmarAlJarrah
    MemberAuthor

    Part of this is on the branch of #1996 (at 8ed8087), which leaves the issue open for the rest.

    Done there: the fourteen readers of a keyword inside a slot are nil-safe methods of *statement.Schema (Types, Enum, Const, FormatName, PrefixItems, Discriminator, Default, XML, ExternalDocs, and the five that return a whole slot, ArrayKeywords, ObjectKeywords, TextKeywords, ChoiceKeywords, LogicKeywords, named so because the fields hold the bare names), the free functions are gone, three of which were declared twice, and a table test reads each on a nil schema and a nil slot.

    Left: notesIn, extensionsIn and allOfIn stay free functions. They read a field the schema holds itself, not a keyword inside a slot, and Schema carries 18 methods against the cap of 20, so the three would pass it. nullableTrue stays too: it classifies the key's node. Closing this wants either those three read another way or a reason recorded for leaving them.

  3. OmarAlJarrah commented on Nov 29, 2026

    @OmarAlJarrah
    MemberAuthor

    #2354 closes this at its merge without the change it asks, for the reason below.

    At 1cf9539a7 the Done-when command lists three readers:

    $ git grep -nE '^func [a-zA-Z]+In\([a-z]+ \*statement\.Schema\) ' -- 'compilers/*.go' ':!*_test.go' | grep -v looseIn
    compilers/openapi/internal/annotation/annotation.go:689:func notesIn(schema *statement.Schema) *statement.Notes {
    compilers/openapi/internal/annotation/annotation.go:698:func extensionsIn(schema *statement.Schema) []statement.Extension {
    compilers/openapi/internal/schema/branch.go:132:func allOfIn(schema *statement.Schema) []statement.Subschema {

    Each steps past a nil schema, not a nil slot, which a method of statement.Schema would read off its own receiver. statement.Schema holds 18 methods (git grep -hE '^func \(([[:alnum:]_]+ )?\*?Schema\) ' -- 'compilers/openapi/internal/statement/*.go' ':!*_test.go' | wc -l) against the cap of 20 in internal/archtest (maxMethodsPerType), so the three as methods break it, and trading find away to make room would leave no room for the next. Each of the three now says why it is a function (3e493d576). find's in-band -1 is filed apart as #2331.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:P4type:chore

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions