Add rdf ontology nodes, make processing mandatory.

このコミットが含まれているのは:
Cory Slep 2018-12-01 16:36:33 +01:00
コミット c61bd2d129
4個のファイルの変更136行の追加6行の削除

36
tools/exp/rdf/nodes.go ノーマルファイル
ファイルの表示

@ -0,0 +1,36 @@
package rdf
var _ RDFNode = &AliasedDelegate{}
// AliasedDelegate will call the delegated RDFNode if the passed in keys
// conform to either the spec or alias.
type AliasedDelegate struct {
Spec string
Alias string
Name string
Delegate RDFNode
}
// Enter calls the Delegate's Enter if the key conforms to the Spec or Alias.
func (a *AliasedDelegate) Enter(key string, ctx *ParsingContext) (bool, error) {
if IsKeyApplicable(key, a.Spec, a.Alias, a.Name) {
return a.Delegate.Enter(key, ctx)
}
return false, nil
}
// Exit calls the Delegate's Exit if the key conforms to the Spec or Alias.
func (a *AliasedDelegate) Exit(key string, ctx *ParsingContext) (bool, error) {
if IsKeyApplicable(key, a.Spec, a.Alias, a.Name) {
return a.Delegate.Exit(key, ctx)
}
return false, nil
}
// Apply calls the Delegate's Apply if the key conforms to the Spec or Alias.
func (a *AliasedDelegate) Apply(key string, value interface{}, ctx *ParsingContext) (bool, error) {
if IsKeyApplicable(key, a.Spec, a.Alias, a.Name) {
return a.Delegate.Apply(key, value, ctx)
}
return false, nil
}

ファイルの表示

@ -1,19 +1,97 @@
package rdf
import (
"fmt"
)
const (
rdfSpec = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
langstringSpec = "langstring"
propertySpec = "Property"
valueSpec = "value"
)
type RDFOntology struct{}
func (o *RDFOntology) SpecURI() string {
return "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
return rdfSpec
}
func (o *RDFOntology) Load() ([]RDFNode, error) {
return nil, nil
return o.LoadAsAlias("")
}
func (o *RDFOntology) LoadAsAlias(s string) ([]RDFNode, error) {
return nil, nil
return []RDFNode{
&AliasedDelegate{
Spec: rdfSpec,
Alias: s,
Name: langstringSpec,
Delegate: &langstring{},
},
&AliasedDelegate{
Spec: rdfSpec,
Alias: s,
Name: propertySpec,
Delegate: &property{},
},
&AliasedDelegate{
Spec: rdfSpec,
Alias: s,
Name: valueSpec,
Delegate: &value{},
},
}, nil
}
func (o *RDFOntology) LoadElement(name string, payload map[string]interface{}) ([]RDFNode, error) {
return nil, nil
}
var _ RDFNode = &langstring{}
type langstring struct{}
func (l *langstring) Enter(key string, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf langstring cannot be entered")
}
func (l *langstring) Exit(key string, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf langstring cannot be exited")
}
func (l *langstring) Apply(key string, value interface{}, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf langstring cannot be applied")
}
var _ RDFNode = &property{}
type property struct{}
func (p *property) Enter(key string, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf property cannot be entered")
}
func (p *property) Exit(key string, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf property cannot be exited")
}
func (p *property) Apply(key string, value interface{}, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf property cannot be applied")
}
var _ RDFNode = &value{}
type value struct{}
func (v *value) Enter(key string, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf value cannot be entered")
}
func (v *value) Exit(key string, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf value cannot be exited")
}
func (v *value) Apply(key string, value interface{}, ctx *ParsingContext) (bool, error) {
return true, fmt.Errorf("rdf value cannot be applied")
}

ファイルの表示

@ -108,7 +108,7 @@ func enterFirstNode(nodes []RDFNode, key string, ctx *ParsingContext) error {
return err
}
}
return nil
return fmt.Errorf("no RDFNode applicable for entering %q", key)
}
// exitFirstNode will Exit the first RDFNode that returns true or an error.
@ -120,7 +120,7 @@ func exitFirstNode(nodes []RDFNode, key string, ctx *ParsingContext) error {
return err
}
}
return nil
return fmt.Errorf("no RDFNode applicable for exiting %q", key)
}
// applyFirstNode will Apply the first RDFNode that returns true or an error.
@ -132,7 +132,7 @@ func applyFirstNode(nodes []RDFNode, key string, value interface{}, ctx *Parsing
return err
}
}
return nil
return fmt.Errorf("no RDFNode applicable for applying %q with value %v", key, value)
}
// parseJSONLDContext implements a super basic JSON-LD @context parsing

ファイルの表示

@ -12,6 +12,22 @@ const (
ID = "@id"
)
// IsKeyApplicable returns true if the key has a spec or alias prefix and the
// property is equal to the desired name.
//
// If 'alias' is an empty string, it is ignored.
func IsKeyApplicable(key, spec, alias, name string) bool {
if key == spec+name {
return true
} else if len(alias) > 0 {
strs := strings.Split(key, ALIAS_DELIMITER)
if len(strs) > 1 && strs[0] != HTTP && strs[0] != HTTPS {
return strs[0] == alias && strs[1] == name
}
}
return false
}
// splitAlias splits a possibly-aliased string, without splitting on the colon
// if it is part of the http or https spec.
func splitAlias(s string) []string {