Skip to content

Commit

Permalink
Merge pull request #78 from cjslep/dev
Browse files Browse the repository at this point in the history
Update next generation experimental tool
  • Loading branch information
cjslep committed Dec 5, 2018
2 parents c2325ed + d2182dc commit 9c3005b
Show file tree
Hide file tree
Showing 14 changed files with 6,963 additions and 241 deletions.
200 changes: 44 additions & 156 deletions tools/exp/main.go
Original file line number Diff line number Diff line change
@@ -1,167 +1,55 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"github.com/dave/jennifer/jen"
"github.com/go-fed/activity/tools/exp/codegen"
"github.com/go-fed/activity/tools/exp/props"
"github.com/go-fed/activity/tools/exp/types"
"github.com/cjslep/activity/tools/exp/rdf"
"github.com/cjslep/activity/tools/exp/rdf/owl"
"github.com/cjslep/activity/tools/exp/rdf/rdfs"
"github.com/cjslep/activity/tools/exp/rdf/schema"
"github.com/cjslep/activity/tools/exp/rdf/xsd"
"io/ioutil"
)

var registry *rdf.RDFRegistry

func mustAddOntology(o rdf.Ontology) {
if registry == nil {
registry = rdf.NewRDFRegistry()
}
if err := registry.AddOntology(o); err != nil {
panic(err)
}
}

func init() {
mustAddOntology(&xsd.XMLOntology{})
mustAddOntology(&owl.OWLOntology{})
mustAddOntology(&rdf.RDFOntology{})
mustAddOntology(&rdfs.RDFSchemaOntology{})
mustAddOntology(&schema.SchemaOntology{})
}

var (
input = flag.String("input", "spec.json", "Input JSON-LD specification used to generate Go code.")
)

func main() {
serializeIRIFn := *codegen.NewFunction(
"test",
"SerializeIRI",
[]jen.Code{jen.Id("u").Op("*").Qual("url", "URL")},
[]jen.Code{jen.List(jen.Interface(), jen.Error())},
[]jen.Code{jen.Empty()})
deserializeIRIFn := *codegen.NewFunction(
"test",
"DeserializeIRI",
[]jen.Code{jen.Interface()},
[]jen.Code{jen.List(jen.Op("*").Qual("url", "URL"), jen.Bool(), jen.Error())},
[]jen.Code{jen.Empty()})
lessIRIFn := *codegen.NewFunction(
"test",
"LessIRI",
[]jen.Code{jen.Id("lhs").Id("rhs").Op("*").Qual("url", "URL")},
[]jen.Code{jen.Bool()},
[]jen.Code{jen.Empty()})
serializeIntFn := *codegen.NewFunction(
"test",
"SerializeInt",
[]jen.Code{jen.Id("i").Op("*").Int()},
[]jen.Code{jen.List(jen.Interface(), jen.Error())},
[]jen.Code{jen.Empty()})
deserializeIntFn := *codegen.NewFunction(
"test",
"DeserializeInt",
[]jen.Code{jen.Interface()},
[]jen.Code{jen.List(jen.Int(), jen.Bool(), jen.Error())},
[]jen.Code{jen.Empty()})
lessIntFn := *codegen.NewFunction(
"test",
"LessInt",
[]jen.Code{jen.Id("lhs").Id("rhs").Int()},
[]jen.Code{jen.Bool()},
[]jen.Code{jen.Empty()})
x := props.NewFunctionalPropertyGenerator(
"test",
props.Identifier{
LowerName: "testFunctional",
CamelName: "TestFunctional",
},
[]props.Kind{
{
Name: props.Identifier{
LowerName: "iri",
CamelName: "IRI",
},
ConcreteKind: "*url.URL",
Nilable: true,
HasNaturalLanguageMap: false,
SerializeFn: serializeIRIFn,
DeserializeFn: deserializeIRIFn,
LessFn: lessIRIFn,
},
},
true)
y := props.NewFunctionalPropertyGenerator(
"test",
props.Identifier{
LowerName: "testFunctionalNonnil",
CamelName: "TestFunctionalNonil",
},
[]props.Kind{
{
Name: props.Identifier{
LowerName: "number",
CamelName: "Number",
},
ConcreteKind: "int",
Nilable: false,
HasNaturalLanguageMap: false,
SerializeFn: serializeIntFn,
DeserializeFn: deserializeIntFn,
LessFn: lessIntFn,
},
},
true)
z := props.NewFunctionalPropertyGenerator(
"test",
props.Identifier{
LowerName: "testFunctionalMultiType",
CamelName: "TestFunctionalMultiType",
},
[]props.Kind{
{
Name: props.Identifier{
LowerName: "iri",
CamelName: "IRI",
},
ConcreteKind: "*url.URL",
Nilable: true,
HasNaturalLanguageMap: false,
SerializeFn: serializeIRIFn,
DeserializeFn: deserializeIRIFn,
LessFn: lessIRIFn,
},
{
Name: props.Identifier{
LowerName: "number",
CamelName: "Number",
},
ConcreteKind: "int",
Nilable: false,
HasNaturalLanguageMap: false,
SerializeFn: serializeIntFn,
DeserializeFn: deserializeIntFn,
LessFn: lessIntFn,
},
},
true)
zz := props.NewNonFunctionalPropertyGenerator(
"test",
props.Identifier{
LowerName: "testNonFunctionalMultiType",
CamelName: "TestNonFunctionalMultiType",
},
[]props.Kind{
{
Name: props.Identifier{
LowerName: "iri",
CamelName: "IRI",
},
ConcreteKind: "*url.URL",
Nilable: true,
HasNaturalLanguageMap: false,
SerializeFn: serializeIRIFn,
DeserializeFn: deserializeIRIFn,
LessFn: lessIRIFn,
},
{
Name: props.Identifier{
LowerName: "number",
CamelName: "Number",
},
ConcreteKind: "int",
Nilable: false,
HasNaturalLanguageMap: false,
SerializeFn: serializeIntFn,
DeserializeFn: deserializeIntFn,
LessFn: lessIntFn,
},
},
true)
t1, err := types.NewTypeGenerator("test", "TestType", "TestType is a test type", []types.Property{x, y, z, zz}, nil, nil)
flag.Parse()

b, err := ioutil.ReadFile(*input)
if err != nil {
panic(err)
}
var inputJSON map[string]interface{}
err = json.Unmarshal(b, &inputJSON)
if err != nil {
panic(err)
}
p, err := rdf.ParseVocabulary(registry, inputJSON)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n\n", x.Definition().Definition())
fmt.Printf("%#v\n\n", y.Definition().Definition())
fmt.Printf("%#v\n\n", z.Definition().Definition())
s, t := zz.Definitions()
fmt.Printf("%#v\n\n%#v\n\n", s.Definition(), t.Definition())
fmt.Printf("%#v\n\n", types.TypeInterface("test").Definition())
fmt.Printf("%#v\n\n", t1.Definition().Definition())
fmt.Printf("done\n%s\n", p)
}
4 changes: 2 additions & 2 deletions tools/exp/props/funcprop.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func NewFunctionalPropertyGenerator(pkg string,
PropertyGenerator: PropertyGenerator{
Package: pkg,
HasNaturalLanguageMap: hasNaturalLanguageMap,
Name: name,
Kinds: kinds,
Name: name,
Kinds: kinds,
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions tools/exp/props/nonfuncprop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func NewNonFunctionalPropertyGenerator(pkg string,
PropertyGenerator: PropertyGenerator{
Package: pkg,
HasNaturalLanguageMap: hasNaturalLanguageMap,
Name: name,
Kinds: kinds,
Name: name,
Kinds: kinds,
},
}
}
Expand Down Expand Up @@ -66,9 +66,9 @@ func (p *NonFunctionalPropertyGenerator) iteratorTypeName() Identifier {
func (p *NonFunctionalPropertyGenerator) elementTypeGenerator() *FunctionalPropertyGenerator {
return &FunctionalPropertyGenerator{
PropertyGenerator: PropertyGenerator{
Package: p.PropertyGenerator.Package,
Name: p.iteratorTypeName(),
Kinds: p.Kinds,
Package: p.PropertyGenerator.Package,
Name: p.iteratorTypeName(),
Kinds: p.Kinds,
HasNaturalLanguageMap: p.PropertyGenerator.HasNaturalLanguageMap,
asIterator: true,
},
Expand Down
Loading

0 comments on commit 9c3005b

Please sign in to comment.