From 9b279662d324ef07e3d09d1a5da1be160ba8f20a Mon Sep 17 00:00:00 2001 From: Santhosh Kumar Tekuri Date: Fri, 17 May 2024 17:55:15 +0530 Subject: [PATCH] godoc --- compiler.go | 14 +++++++++++++- content.go | 5 ++++- draft.go | 7 +++++++ format.go | 1 + loader.go | 2 ++ schema.go | 3 +++ 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/compiler.go b/compiler.go index 829f9b7..942c36a 100644 --- a/compiler.go +++ b/compiler.go @@ -17,6 +17,7 @@ type Compiler struct { mediaTypes map[string]*MediaType } +// NewCompiler create CompilerObject. func NewCompiler() *Compiler { return &Compiler{ schemas: map[urlPtr]*Schema{}, @@ -94,6 +95,10 @@ func (c *Compiler) RegisterContentMediaType(mt *MediaType) { } // RegisterVocabulary registers custom vocabulary. +// +// NOTE: +// - vocabularies are disabled for draft >= 2019-09 +// see [Compiler.AssertVocabs] func (c *Compiler) RegisterVocabulary(vocab *Vocabulary) { c.roots.vocabularies[vocab.URL] = vocab } @@ -163,7 +168,7 @@ func (c *Compiler) MustCompile(loc string) *Schema { return sch } -// Compile compiles json-schema at gven loc. +// Compile compiles json-schema at given loc. func (c *Compiler) Compile(loc string) (*Schema, error) { uf, err := absolute(loc) if err != nil { @@ -296,11 +301,18 @@ func (q *queue) get(up urlPtr) *Schema { // regexp -- +// Regexp is the representation of compiled regular expression. type Regexp interface { fmt.Stringer + + // MatchString reports whether the string s contains + // any match of the regular expression. MatchString(string) bool } +// RegexpEngine parses a regular expression and returns, +// if successful, a Regexp object that can be used to +// match against text. type RegexpEngine func(string) (Regexp, error) func (re RegexpEngine) validate(v any) error { diff --git a/content.go b/content.go index 9dfb6db..8d62e58 100644 --- a/content.go +++ b/content.go @@ -8,7 +8,9 @@ import ( // Decoder specifies how to decode specific contentEncoding. type Decoder struct { - Name string + // Name of contentEncoding. + Name string + // Decode given string to byte array. Decode func(string) ([]byte, error) } @@ -23,6 +25,7 @@ var decoders = map[string]*Decoder{ // MediaType specified how to validate bytes against specific contentMediaType. type MediaType struct { + // Name of contentMediaType. Name string // Validate checks whether bytes conform to this mediatype. diff --git a/draft.go b/draft.go index 54f067f..fd040ce 100644 --- a/draft.go +++ b/draft.go @@ -6,13 +6,17 @@ import ( "strings" ) +// Position tells possible tokens in json. type Position uint const ( + // PosProp represents all properties of json object. PosProp Position = 0 + // PosItem represents all items of json array. PosItem Position = 1 ) +// SchemaPosition tells where to look for subschema inside keyword. type SchemaPosition []Position func (sp SchemaPosition) collect(v any, ptr jsonPointer, target map[jsonPointer]any) { @@ -39,6 +43,7 @@ func (sp SchemaPosition) collect(v any, ptr jsonPointer, target map[jsonPointer] } } +// Subschemas tells possible Subschemas for given keyword. type Subschemas map[string][]SchemaPosition func (ss Subschemas) collect(obj map[string]any, ptr jsonPointer, target map[jsonPointer]any) { @@ -54,6 +59,7 @@ func (ss Subschemas) collect(obj map[string]any, ptr jsonPointer, target map[jso } } +// A Draft represents json-schema specification. type Draft struct { version int url string @@ -65,6 +71,7 @@ type Draft struct { defaultVocabs []string // names of default vocabs } +// String returns the specification url. func (d *Draft) String() string { return d.url } diff --git a/format.go b/format.go index 02dccb4..7652a76 100644 --- a/format.go +++ b/format.go @@ -12,6 +12,7 @@ import ( // Format defined specific format. type Format struct { + // Name of format. Name string // Validate checks if given value is of this format. diff --git a/loader.go b/loader.go index 821f5ce..e4e803b 100644 --- a/loader.go +++ b/loader.go @@ -22,6 +22,7 @@ type URLLoader interface { // -- +// FileLoader loads json file url. type FileLoader struct{} func (l FileLoader) Load(url string) (any, error) { @@ -37,6 +38,7 @@ func (l FileLoader) Load(url string) (any, error) { return UnmarshalJSON(f) } +// ToFile is helper method to convert file url to file path. func (l FileLoader) ToFile(url string) (string, error) { u, err := gourl.Parse(url) if err != nil { diff --git a/schema.go b/schema.go index f25125d..683839c 100644 --- a/schema.go +++ b/schema.go @@ -6,6 +6,8 @@ import ( "math/big" ) +// Schema is the regpresentation of a compiled +// jsonschema. type Schema struct { up urlPtr resource *Schema @@ -167,6 +169,7 @@ func (jt jsonType) String() string { // -- +// Types encapsulates list of json value types. type Types int func newTypes(v any) *Types {