Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions cmd/didcore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

func didDocument() ModelDefinition {
return ModelDefinition{
Name: "Document",
Imports: []string{
`"github.com/nuts-foundation/go-did/v1/ld"`,
`ssi "github.com/nuts-foundation/go-did"`,
`"github.com/nuts-foundation/go-did/did"`,
`"github.com/nuts-foundation/go-did/v1/ld"`,
},
Fields: []FieldDefinition{
{
Name: "ID",
JSONName: "id",
GoType: "DID",
Required: true,
},
{
Name: "AlsoKnownAs",
JSONName: "alsoKnownAs",
GoType: "[]ssi.URI",
},
{
Name: "VerificationMethod",
JSONName: "verificationMethod",
GoType: "VerificationMethods",
},
{
Name: "Authentication",
JSONName: "authentication",
GoType: "VerificationRelationships",
},
{
Name: "AssertionMethod",
JSONName: "assertionMethod",
GoType: "VerificationRelationships",
},
{
Name: "KeyAgreement",
JSONName: "keyAgreement",
GoType: "VerificationRelationships",
},
{
Name: "CapabilityInvocation",
JSONName: "capabilityInvocation",
GoType: "VerificationRelationships",
},
{
Name: "CapabilityDelegation",
JSONName: "capabilityDelegation",
GoType: "VerificationRelationships",
},
{
Name: "Service",
JSONName: "service",
GoType: "[]Service",
},
},
}
}
50 changes: 50 additions & 0 deletions cmd/jsonld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

func generateLDSerializer(def ModelDefinition, implType string) string {
buf := "var _ " + def.Name + " = &" + implType + "{}\n"
buf += "\n"
buf += "type " + implType + " struct {\n"
buf += "\tld.Object\n"
buf += "\tcontext []interface{}\n"
buf += "}\n"
buf += "\n"
// Type-specific fields
for _, field := range def.Fields {
returnType := field.GoType
if !field.Required {
returnType = "(bool, " + field.GoType + ")"
}
buf += "func (o " + implType + ") " + field.Name + "() " + returnType + " {\n"
if field.Name == "Context" {
// Fixed Context field
buf += "\treturn o.context\n"
} else {
if field.Required {
buf += "\tok, obj := o.Get(\"" + field.IRI + "\")\n"
buf += "\tif !ok {\n"
buf += "\t\treturn " + ldNilValue(field.GoType) + "\n"
buf += "\t}\n"
buf += "\treturn " + converterFunc(field.GoType) + "(obj)\n"
} else {
buf += "\tok, obj := o.Get(\"" + field.IRI + "\")\n"
buf += "\tif !ok {\n"
buf += "\t\treturn false, " + converterFunc(field.GoType) + "(nil)\n"
buf += "\t}\n"
buf += "\treturn true, " + converterFunc(field.GoType) + "(obj)\n"
}
}
buf += "}\n\n"
}
return buf
}

func ldNilValue(goType string) string {
switch goType {
case "ld.IDObject":
return "ld.IDObject{}"
case "time.Time":
return "time.Time{}"
default:
return "nil"
}
}
11 changes: 11 additions & 0 deletions cmd/jwt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

func generateJWTSerializer(def ModelDefinition, implType string) string {
buf := "var _ " + def.Name + " = &" + implType + "{}\n"
buf += "\n"
buf += "type " + implType + " struct {\n"
buf += "\ttoken jwt.Token\n"
buf += "}\n"
buf += "\n"
return buf
}
122 changes: 122 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"os"
"strings"
)

func main() {
targets := []struct {
file string
pkg string
def ModelDefinition
}{
{
file: "../v1/vc/verifiable_credential.gen.go",
pkg: "vc",
def: verifiableCredential(),
},
{
file: "../v1/vc/issuer.gen.go",
pkg: "vc",
def: issuer(),
},
{
file: "../v1/vc/credential_subject.gen.go",
pkg: "vc",
def: credentialSubject(),
},
{
file: "../v1/did/model.gen.go",
pkg: "did",
def: didDocument(),
},
}
for _, target := range targets {
err := os.WriteFile(target.file, []byte(generate(target.pkg, target.def)), 0644)
if err != nil {
panic(err)
}
}
}

type ModelDefinition struct {
Name string
Fields []FieldDefinition
Imports []string
SupportLDSerialization bool
SupportJWTSerialization bool
}

type FieldDefinition struct {
Name string
JSONName string
IRI string
JWTClaim string
Required bool
DocLink string
GoType string
}

func generate(pkg string, def ModelDefinition) string {
buf := ""
buf += "package " + pkg + "\n\n"
buf += "\n"
// Imports
buf += "import (\n"
for _, imp := range def.Imports {
buf += "\t" + imp + "\n"
}
if def.SupportJWTSerialization {
buf += "\t\"github.com/lestrrat-go/jwx/v2/jwt\"\n"
}
buf += ")\n"
buf += "\n"
// Interface type
buf += "type " + def.Name + " interface {\n"
for _, field := range def.Fields {
buf += "\t// " + field.Name + " as defined by " + field.DocLink + "\n"
if field.Required {
buf += "\t" + field.Name + "() " + field.GoType + "\n"
} else {
buf += "\t" + field.Name + "() (bool, " + field.GoType + ")\n"
}
}
buf += "}\n"
buf += "\n"
if def.SupportLDSerialization {
buf += generateLDSerializer(def, "LD"+def.Name)
}
if def.SupportJWTSerialization {
buf += generateJWTSerializer(def, "JWT"+def.Name)
}
return buf
}

func converterFunc(goType string) string {
isSlice := goType[0] == '['
parts := strings.Split(goType, ".")
name := parts[len(parts)-1]
// Remove non-alphanumeric characters
name = strings.Map(func(r rune) rune {
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') {
return r
}
return -1
}, name)

var pkg string
if len(parts) > 1 || strings.ToLower(name) == name {
pkg = "ld"
}

if isSlice {
name += "s"
}
// First character to upper
name = "To" + strings.ToUpper(name[:1]) + name[1:]
if pkg != "" {
name = pkg + "." + name
}
return name
}
109 changes: 109 additions & 0 deletions cmd/vc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

func verifiableCredential() ModelDefinition {
return ModelDefinition{
Name: "VerifiableCredential",
SupportJWTSerialization: true,
SupportLDSerialization: true,
Imports: []string{
`"github.com/nuts-foundation/go-did/v1/ld"`,
`"time"`,
`"net/url"`,
},
Fields: []FieldDefinition{
{
Name: "Context",
GoType: "[]interface{}",
Required: true,
IRI: "@context",
JWTClaim: "vc.@context",
DocLink: "https://www.w3.org/TR/vc-data-model/#context-urls",
},
{
Name: "ID",
GoType: "*url.URL",
IRI: "@id",
JWTClaim: "jti",
},
{
Name: "Type",
GoType: "[]string",
Required: true,
IRI: "@type",
JWTClaim: "vc.@type",
DocLink: "https://www.w3.org/TR/vc-data-model/#types",
},
{
Name: "Issuer",
GoType: "Issuer",
Required: true,
IRI: "https://www.w3.org/2018/credentials#issuer",
JWTClaim: "iss",
DocLink: "https://www.w3.org/TR/vc-data-model/#issuer",
},
{
Name: "IssuanceDate",
GoType: "time.Time",
Required: true,
IRI: "https://www.w3.org/2018/credentials#issuanceDate",
JWTClaim: "nbf",
DocLink: "https://www.w3.org/TR/vc-data-model/#issuance",
},
{
Name: "ExpirationDate",
GoType: "time.Time",
Required: false,
IRI: "https://www.w3.org/2018/credentials#expirationDate",
JWTClaim: "exp",
DocLink: "https://www.w3.org/TR/vc-data-model/#expiration",
},
{
Name: "CredentialSubject",
GoType: "[]CredentialSubject",
Required: true,
IRI: "https://www.w3.org/2018/credentials#credentialSubject",
JWTClaim: "vc.credentialSubject",
DocLink: "https://www.w3.org/TR/vc-data-model/#credential-subject",
},
},
}
}

func issuer() ModelDefinition {
return ModelDefinition{
Name: "Issuer",
Imports: []string{
`"github.com/nuts-foundation/go-did/v1/ld"`,
`"net/url"`,
},
SupportLDSerialization: true,
SupportJWTSerialization: true,
Fields: []FieldDefinition{
{
Name: "ID",
GoType: "*url.URL",
IRI: "@id",
Required: true,
},
},
}
}

func credentialSubject() ModelDefinition {
return ModelDefinition{
Name: "CredentialSubject",
Imports: []string{
`"github.com/nuts-foundation/go-did/v1/ld"`,
`"net/url"`,
},
SupportLDSerialization: true,
SupportJWTSerialization: true,
Fields: []FieldDefinition{
{
Name: "ID",
GoType: "*url.URL",
IRI: "@id",
},
},
}
}
Loading