Skip to content

canonical-templates.1 rc#20

Open
malikbenkirane wants to merge 7 commits intocanonicalfrom
canonical-templates-16
Open

canonical-templates.1 rc#20
malikbenkirane wants to merge 7 commits intocanonicalfrom
canonical-templates-16

Conversation

@malikbenkirane
Copy link
Contributor

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

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
@malikbenkirane
Copy link
Contributor Author

tag v0.1.0-canonical-templates.1

Malik Benkirane added 6 commits June 22, 2023 00:02
Publicly announce surrealDriver interface as SurrealDB
Just renamed surrealDriver to SurrealDB to use other drivers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant