From 7eb1755c96c61dd08bf0b8a082dc967d4b4a2e0b Mon Sep 17 00:00:00 2001 From: Cory Slep Date: Sun, 6 Jan 2019 12:41:47 +0100 Subject: [PATCH] Add support for link relation values. --- tools/exp/rdf/rfc/ontology.go | 98 +++++++++++++++++++++++++++++++++++ tools/exp/spec.json | 2 +- 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/tools/exp/rdf/rfc/ontology.go b/tools/exp/rdf/rfc/ontology.go index 0c0857b..c524e3f 100644 --- a/tools/exp/rdf/rfc/ontology.go +++ b/tools/exp/rdf/rfc/ontology.go @@ -15,6 +15,7 @@ const ( rfcSpec = "https://tools.ietf.org/html/" bcp47Spec = "bcp47" mimeSpec = "rfc2045" // See also: rfc2046 and rfc6838 + relSpec = "rfc5988" ) type RFCOntology struct { @@ -43,6 +44,12 @@ func (o *RFCOntology) LoadAsAlias(s string) ([]rdf.RDFNode, error) { Name: mimeSpec, Delegate: &mime{pkg: o.Package}, }, + &rdf.AliasedDelegate{ + Spec: rfcSpec, + Alias: s, + Name: relSpec, + Delegate: &rel{pkg: o.Package}, + }, }, nil } @@ -66,6 +73,15 @@ func (o *RFCOntology) LoadSpecificAsAlias(alias, name string) ([]rdf.RDFNode, er Delegate: &mime{pkg: o.Package}, }, }, nil + case relSpec: + return []rdf.RDFNode{ + &rdf.AliasedDelegate{ + Spec: "", + Alias: "", + Name: alias, + Delegate: &rel{pkg: o.Package}, + }, + }, nil } return nil, fmt.Errorf("rfc ontology cannot find %q to alias to %q", name, alias) } @@ -81,6 +97,8 @@ func (o *RFCOntology) GetByName(name string) (rdf.RDFNode, error) { return &bcp47{pkg: o.Package}, nil case mimeSpec: return &mime{pkg: o.Package}, nil + case relSpec: + return &rel{pkg: o.Package}, nil } return nil, fmt.Errorf("rfc ontology could not find node for name %s", name) } @@ -244,3 +262,83 @@ func (m *mime) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bo } return true, nil } + +var _ rdf.RDFNode = &rel{} + +type rel struct { + pkg string +} + +func (*rel) Enter(key string, ctx *rdf.ParsingContext) (bool, error) { + return true, fmt.Errorf("rel cannot be entered") +} + +func (*rel) Exit(key string, ctx *rdf.ParsingContext) (bool, error) { + return true, fmt.Errorf("rel cannot be exited") +} + +func (r *rel) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) { + v := ctx.Result.GetReference(rfcSpec) + if len(v.Values[relSpec].Name) == 0 { + u, err := url.Parse(rfcSpec + relSpec) + if err != nil { + return true, err + } + val := &rdf.VocabularyValue{ + Name: relSpec, + URI: u, + DefinitionType: jen.String(), + Zero: "\"\"", + IsNilable: false, + SerializeFn: rdf.SerializeValueFunction( + r.pkg, + relSpec, + jen.String(), + []jen.Code{ + jen.Return( + jen.Id(codegen.This()), + jen.Nil(), + ), + }), + DeserializeFn: rdf.DeserializeValueFunction( + r.pkg, + relSpec, + jen.String(), + []jen.Code{ + jen.If( + jen.List( + jen.Id("s"), + jen.Id("ok"), + ).Op(":=").Id(codegen.This()).Assert(jen.String()), + jen.Id("ok"), + ).Block( + jen.Return( + jen.Id("s"), + jen.Nil(), + ), + ).Else().Block( + jen.Return( + jen.Lit(""), + jen.Qual("fmt", "Errorf").Call( + jen.Lit("%v cannot be interpreted as a string for rel"), + jen.Id(codegen.This()), + ), + ), + ), + }), + LessFn: rdf.LessFunction( + r.pkg, + relSpec, + jen.String(), + []jen.Code{ + jen.Return( + jen.Id("lhs").Op("<").Id("rhs"), + ), + }), + } + if err = v.SetValue(relSpec, val); err != nil { + return true, err + } + } + return true, nil +} diff --git a/tools/exp/spec.json b/tools/exp/spec.json index faa1204..38397d1 100644 --- a/tools/exp/spec.json +++ b/tools/exp/spec.json @@ -4369,7 +4369,7 @@ "isDefinedBy": "https://www.w3.org/TR/activitystreams-vocabulary/#dfn-rel", "range": { "type": "owl:Class", - "unionOf": [] + "unionOf": "rfc:rfc5988" }, "name": "rel", "url": "https://www.w3.org/TR/activitystreams-vocabulary/#dfn-rel"