From 3f1f0179f717ceb680f0d59ff192f6cc42b681dc Mon Sep 17 00:00:00 2001 From: Lukas Jenicek Date: Mon, 11 Mar 2024 13:34:36 +0100 Subject: [PATCH] render godocs from parsed comments (#57) * render godocs from parsed comments * update examples with comments --- _examples/golang-basics/example.gen.go | 20 ++++++++++++++------ _examples/golang-basics/example.ridl | 12 ++++++++++-- _examples/golang-imports/api.gen.go | 6 +++--- enum.go.tmpl | 9 +++++++-- field.go.tmpl | 1 - struct.go.tmpl | 11 ++++++++--- types.go.tmpl | 5 +++++ 7 files changed, 47 insertions(+), 17 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 4713da0..7e841a1 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,6 +1,6 @@ -// example v0.0.1 8a28c642948c819a9e2f90878d37de257687ddad +// example v0.0.1 8713979f949e7797d4f7506750b6e18f3b9c7c1b // -- -// Code generated by webrpc-gen@v0.14.0-dev with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.15.0-dev with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -legacyErrors -fixEmptyArrays -out=./example.gen.go package main @@ -33,7 +33,7 @@ func WebRPCSchemaVersion() string { // Schema hash generated from your RIDL schema func WebRPCSchemaHash() string { - return "8a28c642948c819a9e2f90878d37de257687ddad" + return "8713979f949e7797d4f7506750b6e18f3b9c7c1b" } // @@ -43,7 +43,12 @@ func WebRPCSchemaHash() string { type Kind uint32 const ( - Kind_USER Kind = 0 + // user can only see number of transactions + Kind_USER Kind = 0 + // admin permissions + // can manage transactions + // revert transactions + // see analytics dashboard Kind_ADMIN Kind = 1 ) @@ -111,9 +116,12 @@ func (x *Intent) Is(values ...Intent) bool { return false } +// Defines users within out wallet app type User struct { - ID uint64 `json:"id" db:"id"` - Uuid uuid.UUID `json:"uuid" db:"id"` + ID uint64 `json:"id" db:"id"` + Uuid uuid.UUID `json:"uuid" db:"id"` + // unique identifier of the user + // must be unique ! Username string `json:"USERNAME" db:"username"` Role string `json:"role" db:"-"` Nicknames []Nickname `json:"nicknames" db:"-"` diff --git a/_examples/golang-basics/example.ridl b/_examples/golang-basics/example.ridl index 72f2444..6c0fdbc 100644 --- a/_examples/golang-basics/example.ridl +++ b/_examples/golang-basics/example.ridl @@ -4,8 +4,14 @@ name = example # name of your backend app version = v0.0.1 # version of your schema +# user role +# which defines which type of operations user can do enum Kind: uint32 - - USER + - USER # user can only see number of transactions + # admin permissions + # can manage transactions + # revert transactions + # see analytics dashboard - ADMIN enum Intent: string @@ -13,6 +19,7 @@ enum Intent: string - closeSession - validateSession +# Defines users within out wallet app struct User - id: uint64 + go.field.name = ID @@ -23,7 +30,8 @@ struct User + go.field.type = uuid.UUID + go.type.import = github.com/google/uuid - - username: string + # unique identifier of the user + - username: string # must be unique ! + json = USERNAME + go.tag.db = username diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index d2a1db0..5844b47 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -1,6 +1,6 @@ // example-api-service v1.0.0 eb9a5d4082a36a8cb84eaa4b3e1091fc4f1f6f3d // -- -// Code generated by webrpc-gen@v0.14.0-dev with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.15.0-dev with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=./proto/api.ridl -target=../../../gen-golang -out=./api.gen.go -pkg=main -server -client -legacyErrors=true -fmt=false package main @@ -38,8 +38,8 @@ func WebRPCSchemaHash() string { // type User struct { - Username string `json:"username"` - Age uint32 `json:"age"` + Username string `json:"username"` + Age uint32 `json:"age"` } type Location uint32 diff --git a/enum.go.tmpl b/enum.go.tmpl index 6e40198..7789cdb 100644 --- a/enum.go.tmpl +++ b/enum.go.tmpl @@ -10,8 +10,13 @@ type {{$name}} {{$type}} {{- end }} const ( -{{- range $fields}} - {{$name}}_{{.Name}} {{$name}} = {{.Value}} +{{- range $_, $field := $fields }} + {{- if gt (len $field.Comments) 0 -}} + {{- range $_, $comment := $field.Comments }} + // {{ replaceAll $comment "\"" "'" }} + {{- end }} + {{- end }} + {{$name}}_{{.Name}} {{$name}} = {{$field.Value}} {{- end}} ) diff --git a/field.go.tmpl b/field.go.tmpl index ad881a3..6b4292b 100644 --- a/field.go.tmpl +++ b/field.go.tmpl @@ -48,7 +48,6 @@ []{{template "field" dict "Name" $name "Type" (listElemType $type) "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $typeMeta}} {{- else if isCoreType $type -}} - {{if $optional}}*{{end}}{{ get $typeMap $type }} {{- else -}}{{- /* structs */ -}} diff --git a/struct.go.tmpl b/struct.go.tmpl index 9208315..d85e999 100644 --- a/struct.go.tmpl +++ b/struct.go.tmpl @@ -6,8 +6,13 @@ {{- $typePrefix := .TypePrefix -}} type {{$name}} struct { -{{range $_, $field := $fields -}} - {{template "field" dict "Name" $field.Name "PrintName" true "Type" $field.Type "Optional" $field.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $field.Meta "JsonTags" true "StructTags" true}} -{{end -}} +{{- range $_, $field := $fields -}} + {{- if gt (len $field.Comments) 0 -}} + {{- range $_, $comment := $field.Comments }} + // {{ replaceAll $comment "\"" "'" }} + {{- end }} + {{- end }} + {{template "field" dict "Name" $field.Name "PrintName" true "Type" $field.Type "Optional" $field.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $field.Meta "JsonTags" true "StructTags" true }} +{{- end }} } {{- end }} diff --git a/types.go.tmpl b/types.go.tmpl index 8ea36d0..4e4a3ca 100644 --- a/types.go.tmpl +++ b/types.go.tmpl @@ -20,6 +20,11 @@ {{ end -}} {{- if and (eq $type.Kind "struct") $opts.types }} +{{- if gt (len $type.Comments) 0 }} + {{- range $_, $comment := $type.Comments }} + // {{ replaceAll $comment "\"" "'" }} + {{- end }} +{{- end }} {{template "struct" dict "Name" $type.Name "TypeMap" $typeMap "TypePrefix" $typePrefix "Fields" $type.Fields}} {{ end -}}