Vocab alias autodetected at generation time.

Default aliases no longer used at deserialization time.
This commit is contained in:
Cory Slep 2019-01-19 17:57:20 +01:00
parent ac87264b54
commit 8942794712
4 changed files with 25 additions and 7 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/dave/jennifer/jen"
"net/url"
"strings"
"unicode"
)
const (
@ -601,7 +602,7 @@ func (c Converter) convertType(t rdf.VocabularyType,
tg, e = gen.NewTypeGenerator(
v.GetName(),
v.URI,
"", // TODO: Vocabulary alias
vocabNameToAlias(v.GetName()),
pm,
name,
comment,
@ -641,7 +642,7 @@ func (c Converter) convertFunctionalProperty(p rdf.VocabularyProperty,
fp = gen.NewFunctionalPropertyGenerator(
v.GetName(),
v.URI,
"", // TODO: Auto-generate aliases
vocabNameToAlias(v.GetName()),
pm,
toIdentifier(p),
comment,
@ -683,7 +684,7 @@ func (c Converter) convertNonFunctionalProperty(p rdf.VocabularyProperty,
nfp = gen.NewNonFunctionalPropertyGenerator(
v.GetName(),
v.URI,
"", // TODO: Auto-generate aliases
vocabNameToAlias(v.GetName()),
pm,
toIdentifier(p),
comment,
@ -1317,3 +1318,19 @@ func backPopulateProperty(r *rdf.RDFRegistry, p rdf.VocabularyProperty, genRefs
}
return
}
// vocabName turns a vocabulary name into an alias based on the capitalization
// of the name. Note that "ActivityStreams" will not be aliased, it will return
// an empty string.
func vocabNameToAlias(name string) string {
if strings.ToLower(name) == "activitystreams" {
return ""
}
s := ""
for _, r := range name {
if unicode.IsUpper(r) {
s += string(r)
}
}
return strings.ToLower(s)
}

View file

@ -365,7 +365,7 @@ func (p *FunctionalPropertyGenerator) serializationFuncs() (*codegen.Method, *co
[]jen.Code{jen.Id("i").Interface(), jen.Id("context").Map(jen.String()).String()},
[]jen.Code{jen.Op("*").Id(p.StructName()), jen.Error()},
[]jen.Code{
jen.Id("alias").Op(":=").Lit(p.vocabAlias),
jen.Id("alias").Op(":=").Lit(""),
jen.If(
jen.List(
jen.Id("a"),
@ -391,7 +391,7 @@ func (p *FunctionalPropertyGenerator) serializationFuncs() (*codegen.Method, *co
[]jen.Code{jen.Id("m").Map(jen.String()).Interface(), jen.Id("context").Map(jen.String()).String()},
[]jen.Code{jen.Op("*").Id(p.StructName()), jen.Error()},
[]jen.Code{
jen.Id("alias").Op(":=").Lit(p.vocabAlias),
jen.Id("alias").Op(":=").Lit(""),
jen.If(
jen.List(
jen.Id("a"),
@ -405,6 +405,7 @@ func (p *FunctionalPropertyGenerator) serializationFuncs() (*codegen.Method, *co
jen.If(
jen.Len(jen.Id("alias")).Op(">").Lit(0),
).Block(
jen.Commentf("Use alias both to find the property, and set within the property."),
jen.Id("propName").Op("=").Qual("fmt", "Sprintf").Call(
jen.Lit("%s:%s"),
jen.Id("alias"),

View file

@ -614,7 +614,7 @@ func (p *NonFunctionalPropertyGenerator) serializationFuncs() (*codegen.Method,
[]jen.Code{jen.Id("m").Map(jen.String()).Interface(), jen.Id("context").Map(jen.String()).String()},
[]jen.Code{jen.Qual(p.GetPublicPackage().Path(), p.InterfaceName()), jen.Error()},
[]jen.Code{
jen.Id("alias").Op(":=").Lit(p.vocabAlias),
jen.Id("alias").Op(":=").Lit(""),
jen.If(
jen.List(
jen.Id("a"),

View file

@ -719,7 +719,7 @@ func (t *TypeGenerator) deserializationFn() (deser *codegen.Function) {
[]jen.Code{jen.Id("m").Map(jen.String()).Interface(), jen.Id("context").Map(jen.String()).String()},
[]jen.Code{jen.Op("*").Id(t.TypeName()), jen.Error()},
[]jen.Code{
jen.Id("alias").Op(":=").Lit(t.vocabAlias),
jen.Id("alias").Op(":=").Lit(""),
jen.If(
jen.List(
jen.Id("a"),