@@ -24,7 +24,6 @@ import (
2424 "fmt"
2525 "go/format"
2626 "slices"
27- "strconv"
2827 "strings"
2928 "unicode"
3029)
@@ -60,14 +59,10 @@ func capitalize(part string) string {
6059// fieldEntry is one property of a spec definition, as the generated table holds it.
6160type fieldEntry struct {
6261 name , key string
63- // schemaDefault is the Go literal of the default of the property, or empty when the
64- // schema declares none.
65- schemaDefault string
6662}
6763
68- // emitFields returns the Go source of the tables of schema defaults, one for each
69- // definition of doc. doc is the schema that shapeForAuthoring and normalize wrote, so each
70- // definition has the title of its struct and holds the properties that become its fields.
64+ // emitFields returns a Go table mapping each spec field to its serialized key. The schema
65+ // has already been shaped for authoring and normalized before this function is called.
7166// A table is keyed by the name of the struct field, as go-jsonschema writes it.
7267func emitFields (doc map [string ]any ) ([]byte , error ) {
7368 definitions , ok := doc ["definitions" ].(map [string ]any )
@@ -91,17 +86,13 @@ func emitFields(doc map[string]any) ([]byte, error) {
9186 }
9287 fmt .Fprintf (
9388 & body ,
94- "\n // %s holds the key and the schema default of each field of %s.\n var %s = map[string]schemaField{\n " ,
89+ "\n // %s maps each field of %s to its serialized key .\n var %s = map[string]schemaField{\n " ,
9590 tableName (title ),
9691 title ,
9792 tableName (title ),
9893 )
9994 for _ , field := range fields {
100- fmt .Fprintf (& body , "\t %q: {key: %q" , field .name , field .key )
101- if field .schemaDefault != "" {
102- fmt .Fprintf (& body , ", schemaDefault: %s" , field .schemaDefault )
103- }
104- body .WriteString ("},\n " )
95+ fmt .Fprintf (& body , "\t %q: {key: %q},\n " , field .name , field .key )
10596 }
10697 body .WriteString ("}\n " )
10798 }
@@ -125,45 +116,8 @@ func getFieldEntries(definition string, properties map[string]any) ([]fieldEntry
125116 )
126117 }
127118 byName [name ] = key
128- node , _ := properties [key ].(map [string ]any )
129- literal , err := defaultLiteral (node ["default" ])
130- if err != nil {
131- return nil , fmt .Errorf ("definitions/%s/properties/%s: %w" , definition , key , err )
132- }
133- fields = append (fields , fieldEntry {name : name , key : key , schemaDefault : literal })
119+ fields = append (fields , fieldEntry {name : name , key : key })
134120 }
135121 slices .SortFunc (fields , func (a , b fieldEntry ) int { return cmp .Compare (a .name , b .name ) })
136122 return fields , nil
137123}
138-
139- // defaultLiteral returns the Go literal of a schema default. A number that the schema
140- // writes with a fraction or an exponent stays a float64, such as retry_delay's 300.0, and
141- // any other number is an int, because readSchema decodes a number as a json.Number. A null
142- // default says the property has none, as an absent one does.
143- func defaultLiteral (value any ) (string , error ) {
144- switch value := value .(type ) {
145- case nil :
146- return "" , nil
147- case bool :
148- return strconv .FormatBool (value ), nil
149- case string :
150- return strconv .Quote (value ), nil
151- case json.Number :
152- if strings .ContainsAny (value .String (), ".eE" ) {
153- number , err := value .Float64 ()
154- if err != nil {
155- return "" , err
156- }
157- literal := strconv .FormatFloat (number , 'f' , - 1 , 64 )
158- if ! strings .Contains (literal , "." ) {
159- literal += ".0"
160- }
161- return literal , nil
162- }
163- if _ , err := value .Int64 (); err != nil {
164- return "" , err
165- }
166- return value .String (), nil
167- }
168- return "" , fmt .Errorf ("the default %v is a %T, which the table cannot hold" , value , value )
169- }
0 commit comments