-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jsonschema: Switch to map based encoding
Provides a public interface for #35, and switches the internal encoding logic to rely on maps, as propsed in #51. This makes the implementation simpler and more straight forward. Compared to the poposed solution 1 in #35 that would rely on passing an io.Writer to an encoder method, this solution will more easily handle a corner-case where a custom validator writes zero bytes to the io.Writer. The reduction in complexity does not come for free though. This looks to be 4x slower for very tiny schemas, and aproximately 2x slower for small schemas. As jsonschema is used as API documentation, and can be easily cached, this performance loss should still be quite acceptable.
- Loading branch information
Showing
12 changed files
with
245 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"io" | ||
"strconv" | ||
import "github.com/rs/rest-layer/schema" | ||
|
||
"github.com/rs/rest-layer/schema" | ||
) | ||
type arrayBuilder schema.Array | ||
|
||
func encodeArray(w io.Writer, v *schema.Array) error { | ||
ew := errWriter{w: w} | ||
|
||
ew.writeString(`"type": "array"`) | ||
func (v arrayBuilder) JSONSchema() (map[string]interface{}, error) { | ||
m := map[string]interface{}{ | ||
"type": "array", | ||
} | ||
if v.MinLen > 0 { | ||
ew.writeFormat(`, "minItems": %s`, strconv.FormatInt(int64(v.MinLen), 10)) | ||
m["minItems"] = v.MinLen | ||
} | ||
if v.MaxLen > 0 { | ||
ew.writeFormat(`, "maxItems": %s`, strconv.FormatInt(int64(v.MaxLen), 10)) | ||
m["maxItems"] = v.MaxLen | ||
} | ||
if v.ValuesValidator != nil { | ||
ew.writeString(`, "items": {`) | ||
if ew.err == nil { | ||
ew.err = encodeValidator(w, v.ValuesValidator) | ||
builder, err := validatorBuilder(v.ValuesValidator) | ||
if err != nil { | ||
return nil, err | ||
} | ||
items, err := builder.JSONSchema() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ew.writeString("}") | ||
m["items"] = items | ||
} | ||
return ew.err | ||
return m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"io" | ||
import "github.com/rs/rest-layer/schema" | ||
|
||
"github.com/rs/rest-layer/schema" | ||
) | ||
type boolBuilder schema.Bool | ||
|
||
func encodeBool(w io.Writer, v *schema.Bool) error { | ||
ew := errWriter{w: w} | ||
ew.writeString(`"type": "boolean"`) | ||
return ew.err | ||
func (v boolBuilder) JSONSchema() (map[string]interface{}, error) { | ||
return map[string]interface{}{"type": "boolean"}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.