schema: expose Kind in kind schema struct, NewSchemaFromBytes() function.
This commit is contained in:
+33
-15
@@ -39,20 +39,16 @@ func FetchSchemaFromURL(schemaURL string) (Schema, error) {
|
|||||||
return Schema{}, fmt.Errorf("failed to read schema response: %w", err)
|
return Schema{}, fmt.Errorf("failed to read schema response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var schema Schema
|
return NewSchemaFromBytes(body)
|
||||||
if err := yaml.Unmarshal(body, &schema); err != nil {
|
|
||||||
return Schema{}, fmt.Errorf("failed to parse schema: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return schema, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Schema struct {
|
type Schema struct {
|
||||||
GenericTags map[string]ContentSpec `yaml:"generic_tags" json:"generic_tags,omitempty"`
|
GenericTags map[string]ContentSpec `yaml:"generic_tags" json:"generic_tags,omitempty"`
|
||||||
Kinds map[string]KindSchema `yaml:"kinds" json:"kinds,omitempty"`
|
Kinds map[string]*KindSchema `yaml:"kinds" json:"kinds,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type KindSchema struct {
|
type KindSchema struct {
|
||||||
|
Kind nostr.Kind `yaml:"-" json:"kind"`
|
||||||
Description string `yaml:"description" json:"description,omitempty"`
|
Description string `yaml:"description" json:"description,omitempty"`
|
||||||
InUse bool `yaml:"in_use" json:"in_use,omitempty"`
|
InUse bool `yaml:"in_use" json:"in_use,omitempty"`
|
||||||
Content ContentSpec `yaml:"content" json:"content,omitempty"`
|
Content ContentSpec `yaml:"content" json:"content,omitempty"`
|
||||||
@@ -86,17 +82,31 @@ type Validator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewValidatorFromBytes(schemaData []byte) (Validator, error) {
|
func NewValidatorFromBytes(schemaData []byte) (Validator, error) {
|
||||||
schema := Schema{
|
schema, err := NewSchemaFromBytes(schemaData)
|
||||||
GenericTags: make(map[string]ContentSpec),
|
if err != nil {
|
||||||
Kinds: make(map[string]KindSchema),
|
return Validator{}, err
|
||||||
}
|
|
||||||
if err := yaml.Unmarshal(schemaData, &schema); err != nil {
|
|
||||||
return Validator{}, fmt.Errorf("failed to parse schema: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewValidatorFromSchema(schema), nil
|
return NewValidatorFromSchema(schema), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewSchemaFromBytes(schemaData []byte) (Schema, error) {
|
||||||
|
schema := Schema{
|
||||||
|
GenericTags: make(map[string]ContentSpec),
|
||||||
|
Kinds: make(map[string]*KindSchema),
|
||||||
|
}
|
||||||
|
if err := yaml.Unmarshal(schemaData, &schema); err != nil {
|
||||||
|
return Schema{}, fmt.Errorf("failed to parse schema: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := range schema.Kinds {
|
||||||
|
kn, _ := strconv.ParseUint(k, 10, 16)
|
||||||
|
schema.Kinds[k].Kind = nostr.Kind(kn)
|
||||||
|
}
|
||||||
|
|
||||||
|
return schema, nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewValidatorFromSchema(sch Schema) Validator {
|
func NewValidatorFromSchema(sch Schema) Validator {
|
||||||
validator := Validator{
|
validator := Validator{
|
||||||
Schema: sch,
|
Schema: sch,
|
||||||
@@ -185,11 +195,19 @@ func NewValidatorFromSchema(sch Schema) Validator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewValidatorFromFile(filename string) (Validator, error) {
|
func NewValidatorFromFile(filename string) (Validator, error) {
|
||||||
|
schema, err := NewSchemaFromFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return Validator{}, err
|
||||||
|
}
|
||||||
|
return NewValidatorFromSchema(schema), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSchemaFromFile(filename string) (Schema, error) {
|
||||||
data, err := os.ReadFile(filename)
|
data, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Validator{}, fmt.Errorf("failed to read schema file: %w", err)
|
return Schema{}, fmt.Errorf("failed to read schema file: %w", err)
|
||||||
}
|
}
|
||||||
return NewValidatorFromBytes(data)
|
return NewSchemaFromBytes(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewValidatorFromURL(schemaURL string) (Validator, error) {
|
func NewValidatorFromURL(schemaURL string) (Validator, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user