Add support for MIME media type.

このコミットが含まれているのは:
Cory Slep 2019-01-06 12:36:11 +01:00
コミット 1bdb66aa98
2個のファイルの変更99行の追加1行の削除

ファイルの表示

@ -14,6 +14,7 @@ import (
const (
rfcSpec = "https://tools.ietf.org/html/"
bcp47Spec = "bcp47"
mimeSpec = "rfc2045" // See also: rfc2046 and rfc6838
)
type RFCOntology struct {
@ -36,6 +37,12 @@ func (o *RFCOntology) LoadAsAlias(s string) ([]rdf.RDFNode, error) {
Name: bcp47Spec,
Delegate: &bcp47{pkg: o.Package},
},
&rdf.AliasedDelegate{
Spec: rfcSpec,
Alias: s,
Name: mimeSpec,
Delegate: &mime{pkg: o.Package},
},
}, nil
}
@ -50,6 +57,15 @@ func (o *RFCOntology) LoadSpecificAsAlias(alias, name string) ([]rdf.RDFNode, er
Delegate: &bcp47{pkg: o.Package},
},
}, nil
case mimeSpec:
return []rdf.RDFNode{
&rdf.AliasedDelegate{
Spec: "",
Alias: "",
Name: alias,
Delegate: &mime{pkg: o.Package},
},
}, nil
}
return nil, fmt.Errorf("rfc ontology cannot find %q to alias to %q", name, alias)
}
@ -63,6 +79,8 @@ func (o *RFCOntology) GetByName(name string) (rdf.RDFNode, error) {
switch name {
case bcp47Spec:
return &bcp47{pkg: o.Package}, nil
case mimeSpec:
return &mime{pkg: o.Package}, nil
}
return nil, fmt.Errorf("rfc ontology could not find node for name %s", name)
}
@ -146,3 +164,83 @@ func (b *bcp47) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (b
}
return true, nil
}
var _ rdf.RDFNode = &mime{}
type mime struct {
pkg string
}
func (*mime) Enter(key string, ctx *rdf.ParsingContext) (bool, error) {
return true, fmt.Errorf("MIME media type cannot be entered")
}
func (*mime) Exit(key string, ctx *rdf.ParsingContext) (bool, error) {
return true, fmt.Errorf("MIME media type cannot be exited")
}
func (m *mime) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) {
v := ctx.Result.GetReference(rfcSpec)
if len(v.Values[mimeSpec].Name) == 0 {
u, err := url.Parse(rfcSpec + mimeSpec)
if err != nil {
return true, err
}
val := &rdf.VocabularyValue{
Name: mimeSpec,
URI: u,
DefinitionType: jen.String(),
Zero: "\"\"",
IsNilable: false,
SerializeFn: rdf.SerializeValueFunction(
m.pkg,
mimeSpec,
jen.String(),
[]jen.Code{
jen.Return(
jen.Id(codegen.This()),
jen.Nil(),
),
}),
DeserializeFn: rdf.DeserializeValueFunction(
m.pkg,
mimeSpec,
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 MIME media type"),
jen.Id(codegen.This()),
),
),
),
}),
LessFn: rdf.LessFunction(
m.pkg,
mimeSpec,
jen.String(),
[]jen.Code{
jen.Return(
jen.Id("lhs").Op("<").Id("rhs"),
),
}),
}
if err = v.SetValue(mimeSpec, val); err != nil {
return true, err
}
}
return true, nil
}

ファイルの表示

@ -4192,7 +4192,7 @@
"isDefinedBy": "https://www.w3.org/TR/activitystreams-vocabulary/#dfn-mediatype",
"range": {
"type": "owl:Class",
"unionOf": []
"unionOf": "rfc:rfc2045"
},
"name": "mediaType",
"url": "https://www.w3.org/TR/activitystreams-vocabulary/#dfn-mediatype"