Skip to content
Piotr Kowalczuk edited this page Aug 15, 2016 · 2 revisions

Library by itself provide various types supported by postgres database. Here are some of them:

pqtgo library extend it by few more

Interface

All those types mentioned before implements common interface Type. It looks like that:

type Type interface {
    fmt.Stringer
    // Fingerprint returns unique identifier of the type.
    // Two different types can have same SQL representation.
    Fingerprint() string
}

Presence of Type interface allows to extend definition with custom types. Good use case would be another generation package for another language like Java or PHP.

MapableType

In case we would like to generate code with different type than it is defined within pqt library, pqt.MappableType comes with rescue. It allows to pass custom type so each generation package can have different output. Go generator provides for example pqtgo.TypeCustom.

Lets imagine that package like pqtjava exists.

func customizedColumn(cn string) *pqt.Column {
	ctgo := pqtgo.TypeCustom(
		&acme.Mandatory{},
		&acme.Optional{},
		&acme.Query{},
	)
	ctjava := pqtjava.TypeCustom(
		"com.acme.Models.CustomTypeMandatory"
		"com.acme.Models.CustomTypeOptional"
		"com.acme.Models.CustomTypeQuery"
	)
	return pqt.NewColumn(cn, pqt.TypeMappable(pqt.TypeJSONB(), ctgo, ctjava))
}

This example assumes that java library would have exactly the same way of building code (probably would not have).

Clone this wiki locally