Open
Conversation
Templates
-
Use NewDoc with NewField to generate a type
type A struct {
S string
id surrealhigh.Id
th surrealhigh.Thing
}
The new type has an under cover doc type with strict types so you
cannot mix things together too much and you can get help from the
powerful go compiler.
func (a A) doc() *docA {
var doc docA
doc.S = fDocA_S(a.S)
return &doc
}
type fDocA_S string
type fDocA_DocID surrealhigh.Id
The unexported doc can be marshaled and unmarshaled
type docA struct {
S fDocA_S `json:"s"`
DocID fDocA_DocID `json:"id"`
}
func (id fDocA_DocID) MarshalJSON() ([]byte, error) {
sid := surrealhigh.Id(id)
sth := sid.Thing(id.Table())
return []byte("\"" + sth + "\""), nil
}
func (v fDocA_DocID) UnmarshalJSON(b []byte) error {
tb := surrealhigh.Table("z")
th := surrealhigh.Thing("z:" + string(b))
id, err := surrealhigh.NewIDFromThing(th, tb)
if err != nil {
return fmt.Errorf("surrealhigh: new id from thing: %w", err)
}
for i := 0; i < 16; i++ {
v[i] = id[i]
}
return nil
}
The generated doc implements DocID
func (doc docA) Id() surrealhigh.Id {
return surrealhigh.Id(doc.DocID)
}
func (doc docA) Table() surrealhigh.Table { return "a" }
Fields are accessible for building queries
func (_ fDocA_S) Field() surrealhigh.Field { return "s" }
func (_ fDocA_DocID) Field() surrealhigh.Field { return "id" }
func (_ fDocA_DocID) Table() surrealhigh.Table { return "a" }
Tests
-
Simple handcrafted tests. You will need to manually remove
./templates/jennifer/gold/sigh_doc_a.go
I added it to `.gitignore`
Gen test only test errors.
References
-
https://golang-devops.github.io/gen/
https://github.com/xo/xo/tree/master/templates/go
https://go.dev/blog/generate
https://pkg.go.dev/cmd/go/internal/generate
https://github.com/dave/jennifer
misc
-
fields with qual
sh-gen-types cmd
Contributor
Author
|
tag v0.1.0-canonical-templates.1 |
added 6 commits
June 22, 2023 00:02
Publicly announce surrealDriver interface as SurrealDB Just renamed surrealDriver to SurrealDB to use other drivers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Templates
Use NewDoc with NewField to generate a type
The new type has an under cover doc type with strict types so you
cannot mix things together too much and you can get help from the
powerful go compiler.
The unexported doc can be marshaled and unmarshaled
The generated doc implements DocID
Fields are accessible for building queries
Tests
Simple handcrafted tests. You will need to manually remove
I added it to
.gitignoreGen test only test errors.
References
https://golang-devops.github.io/gen/
https://github.com/xo/xo/tree/master/templates/go
https://go.dev/blog/generate
https://pkg.go.dev/cmd/go/internal/generate
https://github.com/dave/jennifer
misc
fields with qual
sh-gen-types cmd