Documentation
¶
Index ¶
- Constants
- Variables
- type Base
- func (b Base) BoolVar(name string) bool
- func (b Base) BuilderName() string
- func (b Base) BuilderResultType() string
- func (Base) Comment() string
- func (Base) Fields() []*FieldSpec
- func (b Base) FilenameBase() string
- func (b Base) GenerateSymbol(s string) bool
- func (b Base) GetKeyName(fieldName string) string
- func (Base) Imports() []string
- func (b Base) KeyNamePrefix() string
- func (b Base) Name() string
- func (b Base) Package() string
- func (b Base) StringVar(name string) string
- func (b Base) SymbolName(s string) string
- type FieldSpec
- func (f *FieldSpec) Comment(s string) *FieldSpec
- func (f *FieldSpec) ConstantValue(s string) *FieldSpec
- func (f *FieldSpec) Extra(name string, value interface{}) *FieldSpec
- func (f *FieldSpec) GetComment() string
- func (f *FieldSpec) GetConstantValue() string
- func (f *FieldSpec) GetExtra(name string) interface{}
- func (f *FieldSpec) GetIsConstant() bool
- func (f *FieldSpec) GetIsExtension() bool
- func (f *FieldSpec) GetJSON() string
- func (f *FieldSpec) GetKeyName(object Interface) string
- func (f *FieldSpec) GetName() string
- func (f *FieldSpec) GetRequired() bool
- func (f *FieldSpec) GetType() *TypeSpec
- func (f *FieldSpec) GetUnexportedName() string
- func (f *FieldSpec) IsExtension(b bool) *FieldSpec
- func (f *FieldSpec) JSON(s string) *FieldSpec
- func (f *FieldSpec) Required(b bool) *FieldSpec
- func (f *FieldSpec) Unexported(s string) *FieldSpec
- type InitializerArgumentStyle
- type Interface
- type TypeSpec
- func (ts *TypeSpec) AcceptValue(b bool) *TypeSpec
- func (ts *TypeSpec) AcceptValueMethodName(s string) *TypeSpec
- func (ts *TypeSpec) ApparentType(s string) *TypeSpec
- func (ts *TypeSpec) Element(s string) *TypeSpec
- func (ts *TypeSpec) GetAcceptValueMethodName() string
- func (ts *TypeSpec) GetApparentType() string
- func (ts *TypeSpec) GetElement() string
- func (ts *TypeSpec) GetGetValueMethodName() string
- func (ts *TypeSpec) GetInterfaceDecoder() string
- func (ts *TypeSpec) GetIsInterface() bool
- func (ts *TypeSpec) GetName() string
- func (ts *TypeSpec) GetPointerType() string
- func (ts *TypeSpec) GetRawType() string
- func (ts *TypeSpec) GetSupportsLen() bool
- func (ts *TypeSpec) GetValue(b bool) *TypeSpec
- func (ts *TypeSpec) GetValueMethodName(s string) *TypeSpec
- func (ts *TypeSpec) GetZeroVal() string
- func (ts *TypeSpec) InitializerArgumentStyle(ias InitializerArgumentStyle) *TypeSpec
- func (ts *TypeSpec) InterfaceDecoder(s string) *TypeSpec
- func (ts *TypeSpec) IsInterface(b bool) *TypeSpec
- func (ts *TypeSpec) PointerType(s string) *TypeSpec
- func (ts *TypeSpec) RawType(s string) *TypeSpec
- func (ts *TypeSpec) SliceStyleInitializerArgument() bool
- func (ts *TypeSpec) SupportsLen(b bool) *TypeSpec
- func (ts *TypeSpec) ZeroVal(s string) *TypeSpec
Constants ¶
const ( InitializerArgumentAsSingleArg = iota InitializerArgumentAsSlice )
Variables ¶
var ByteSliceType = Type(byteslice.Buffer{}). ApparentType(`[]byte`). AcceptValue(true). GetValueMethodName(`Bytes`). ZeroVal(`[]byte(nil)`)
ByteSliceType represents a `[]byte` type. Since `sketch` mostly works with JSON, it needs to handle `[]byte` fields being encoded/decoded with base64 encoding transparently. Therefore unless the default behavior for `encoding/json` already work for you, you may need tweaking. The `ByteSliceType` uses `byteslice.Type` internally, which allows the user to specify the base64 encoding.
var NativeByteSliceType = Type([]byte(nil)).
ApparentType(`[]byte`).
PointerType(`[]byte`).
RawType(`[]byte`)
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct { // Variables is a storage for various global and default variables // used during code generation. While it is a public field, end-users // should not be using this variable for any purposes. It is only // visible because the sketch utility must be able to assign certain // parameters during the generation phase, and therefore the // field must be exported. // // tl;dr: DON'T USE IT (unless you are hacking sketch internals) Variables map[string]interface{} }
Base is the struct that defines all of your schemas. You must include this as an embedded field in your schema object directly:
package mypkg type Object struct { schema.Base }
Because sketch reads your schema definitions via Go's package inclusion mechanism, your schema object name must be an exported name (e.g. "MyObject" instead of "myObject")
func (Base) BoolVar ¶
BoolVar returns the value stored in the Variables field as a bool. If the value does not exist or the value is not of a bool type, then returns false
func (Base) BuilderName ¶
BuilderName returns the name of the Builder object. By default a name comprising of the return value from schema's `Name()` method and `Builder` will be used (e.g. "FooBuilder").
If you are using an unexported name for your schema, you probably want to provide your own `BuilderName()` and `BuilderResultType()` methods, which control the struct name of the builder, and the returning value from calling `Build()` on the builder, respectively
func (Base) BuilderResultType ¶
BuilderResultType returns the name of the type that the builder object returns upon calling `Build()`.
If you are using an unexported name for your schema, you probably want to provide your own `BuilderName()` and `BuilderResultType()` methods, which control the struct name of the builder, and the returning value from calling `Build()` on the builder, respectively
func (Base) Comment ¶
Comment returns the comment that should go withh the generated object. The comment should NOT contain the object name, as it would be taken from the return value of `Name` method
func (Base) Fields ¶
Fields returns the list of fields that should be associated with the schema object. User usually must
func (Base) FilenameBase ¶
FilenameBase is used to generate the filename when generating files. Normally the name snake-cased version of the schema object (NOT the return value of `Name` method call) is used, but when you provide a value for thie method, the value is used verbatim
func (Base) GenerateSymbol ¶
GenerateSymbol should return true if the given method is allowed to be generated. The argument consists of a prefix (e.g. "object." or "builder.") followed by the actual method name.
By default all methods are allowed. Users may configure this on a per-object basis by providing their own `GenerateSymbol` method.
func (Base) GetKeyName ¶
func (Base) KeyNamePrefix ¶
KeyNamePrefix returns the prefix that should be added to key name constants. By default no prefix is added, but if you have multiple objects with same field names, you will have to provide them with a prefix.
When --with-key-name-prefix is specified, the default value is set to the name of the object, forcing sketch to generate key name constants in the form of `ObjectName` + `FieldName` + `Key`. You only need to provide your custom `KeyNamePrefix` method when you want to override this default behavior
func (Base) Name ¶
Name returns the name of the object to be generated. By default this value is set to the name of the schema object you created. Users may configure a different name by providing their own `Name` method.
For example, since all schema objects must be exported, you would have to provide a custom `Name` method to tell sketch to generate an unexported object (e.g. `type Object { schema.Base }; func (Object) Name() string { return "object" }`)
func (Base) Package ¶
Package returns the name of the package that a schema belongs to. By default this value is set to the last element of the destination directory. For example, if you are generating files under `/home/lestrrat/foo`, the package name shall be `foo` by default.
Users may configure a different name by providing their own `Package` method -- however,
func (Base) StringVar ¶
StringVar returns the value stored in the Variables field as a string. If the value does not exist or the value is not of a string type, then returns the empty string
func (Base) SymbolName ¶
SymbolName takes an internal name like "object.method.Foo" and returns the actual symbol name
type FieldSpec ¶
type FieldSpec struct {
// contains filtered or unexported fields
}
FieldSpec represents a field that belongs to a particular schema.
func (*FieldSpec) ConstantValue ¶
ConstantValue sets the string value that should be used when fetching this field. When ConstantValue is specified, calling `Set` on this field would be a no-op (no error is returned)
func (*FieldSpec) GetComment ¶
func (*FieldSpec) GetConstantValue ¶
func (*FieldSpec) GetIsConstant ¶
func (*FieldSpec) GetIsExtension ¶
GetIsExtension returns true if this field is an extension, and not part of the object per se. You will need to declare methods to get/set and/or otherwise work this variable by yourself
func (*FieldSpec) GetKeyName ¶
func (*FieldSpec) GetRequired ¶
func (*FieldSpec) GetUnexportedName ¶
func (*FieldSpec) IsExtension ¶
SExtension declares the field as an extension, and not part of the object as defined by the JSON representation. That is to say, this field exist in the Go struct, but not in the JSON structures that it serizlizes to or deserializes from.
Fields defined as extensions are expected to be _internal_ to the object. They are not exposed by either Get/Set, and do not get any sort of accessors.
func (*FieldSpec) JSON ¶
JSON specifies the JSON field name. If unspecified, the unexported name is used.
func (*FieldSpec) Unexported ¶
Unexported specifies the unexported name for this field. If unspecified, the name of the field is automatically converted into a camel-case string with the first phrase being lower cased
type InitializerArgumentStyle ¶
type InitializerArgumentStyle int
InitializerArgumentStyle is used when you would like to override the default behavior and specify that a pseudo-TypeSpec (those types that are not constructed from actual Go objects)'s Builder methods should take a variadic form vs singular form.
type Interface ¶
type Interface interface { Name() string Package() string Fields() []*FieldSpec Comment() string KeyNamePrefix() string GetKeyName(string) string }
Interface exists to provide an abstraction for multiple schema objects that embed schema.Base object in the intermediate program that sketch produces. Users of sketch generally need not worry about this interface.
type TypeSpec ¶
type TypeSpec struct {
// contains filtered or unexported fields
}
TypeSpec is used to store information about a type, and contains various pieces of hints to generate objects/builders.
One important concept that you need to be aware of is that of Apparent Types and Storage Types. An apparent type refers to the type that the end user sees. The pparent type may or may not match the storage type, which is the type that the generated object stores the data as.
For example, you may want to expose a field as accepting a slice of strings (`[]string`), but store it as an object, maybe something like a custom `StringList` that you created. In this case `[]string` is the apparent type, and `StringList` is the storage type.
The `TypeSpec` object is meant to store the storage type, but you can associate the corresponding apparent type via the `ApparentType` method.
func Type ¶
func Type(v interface{}) *TypeSpec
Type creates a new TypeSpec from a piece of Go data using reflection. It populates all the required fields by inspecting the structure, which you can override later.
func TypeName ¶
TypeName creates a TypeSpec from a string name.
If you are allowed to include the struct into the schema code, you should not be using this function. Only use this function when you either have to refer to objects that you are about to generate using sketch, or for objects that you cannot import because of cyclic dependency, etc.
Unlike `Type`, this constructor only takes the name of the type and otherwise has no other information. Therefore it assumes many things, and you will have to set many parameers manually.
The defualt zero value is assumed to be `nil`
If the name starts with a `[]`, then `IsSlice()` is automatically set to true If the name starts with a `map[`, then `IsMap()` is automatically set to true
func (*TypeSpec) AcceptValue ¶
AcceptValue specifies that this type implements the `AcceptValue` method. The `AcceptValue` method must take a single `interface{}` argument, and set the internal value from the given argument, which could be anything that can either be accepted from JSON source, or from a user attempting to set a value to a field of this type.
For example, if you are storing `time.Time` as `mypkg.EpochTime`, and you want users to set `time.Time` values via the Builder (instead of `mypkg.EpochTime`), you will want to implement a `AcceptValue(interface{}) error` method, and implement it such that the value of given `interface{}` is properly set to the internal representation of `mypkg.EpochTime`.
Similarly, if this `mypkg.EpochTime` field is represented as an integer in the JSON source, you might want to handle that as well.
By default the method name for this method is `AcceptValue`, but you will be able to change it by setting a value with the `AcceptValueMethodName`. Calling `AcceptValue(true)` is equivalent to `AcceptValueMethodName("AcceptValue")`
func (*TypeSpec) AcceptValueMethodName ¶
AcceptValueMethodName sets the name of the method that fulfills the `AcceptValue` semantics. Set to the empty string if you would like to indicate that the type does not implement the `AcceptValue` interface.
func (*TypeSpec) ApparentType ¶
func (*TypeSpec) GetAcceptValueMethodName ¶
GetAcceptValueMethodName returns the name of the `AcceptValue` method.
func (*TypeSpec) GetApparentType ¶
func (*TypeSpec) GetElement ¶
func (*TypeSpec) GetGetValueMethodName ¶
GetGetValueMethodName returns the name of the `GetValue` method.
func (*TypeSpec) GetInterfaceDecoder ¶
func (*TypeSpec) GetIsInterface ¶
func (*TypeSpec) GetPointerType ¶
func (*TypeSpec) GetRawType ¶
func (*TypeSpec) GetSupportsLen ¶
func (*TypeSpec) GetValue ¶
GetValue specifies that this type implements the `GetValue` method. The `GetValue` method must return a single element, which represents the apparent (user-facing) type of the field.
For example, if you are storing `time.Time` as `mypkg.EpochTime`, and you want users to get `time.Time` values from the accessor (instead of `mypkg.EpochTime`), you will want to implement a `GetValue() time.Time` method, and make sure that the TypeSpec recognizes its existance.
By default the method name for this method is `GetValue`, but you will be able to change it by setting a value with the `GetValueMethodName`. Calling `GetValue(true)` is equivalent to `GetValueMethodName("GetValue")`
func (*TypeSpec) GetValueMethodName ¶
GetValueMethodName sets the name of the method that fulfills the `GetValue` semantics. Set to the empty string if you would like to indicate that the type does not implement the `GetValue` interface.
func (*TypeSpec) GetZeroVal ¶
func (*TypeSpec) InitializerArgumentStyle ¶
func (ts *TypeSpec) InitializerArgumentStyle(ias InitializerArgumentStyle) *TypeSpec
func (*TypeSpec) InterfaceDecoder ¶
InterfaceDecoder should be set to the name of the function that can take a `[]byte` variable and return a value assignable to the type. For example a type specified as below
schema.Type(`mypkg.Interface`).InterfaceDecoder(`mypkg.Parse`)
may produce code resembling
var val mypkg.Interface var err error val, err = mypkg.Parse(src) // src is []byte
This value is not automatically assigned. Therefore you will laways need to specify this if you are referring to an interface
func (*TypeSpec) IsInterface ¶
IsInterface should be set to true if the type is an interface. When this is true, the decoding logic generated changes. See also `InterfaceDecoder`
func (*TypeSpec) PointerType ¶
PointerType specifies the "indirect" type of a field. The fields are stored as _pointers_ to the actual type, so for most types we simply prepend a `*` to the type. For example for a `string` type, the indirect type would be `*string`, whereas for `*Foo` type, we just use `*Foo` as the indirect type. But for cases when you would like to store an interface, for example, you might want to avoid prepending the `*` by explicitly specifying the name of the indirect type.