Serialize returns the Function as a byte slice. If the Function is invalid, then this returns a nil slice.
(ctx context.Context)
| 26 | |
| 27 | // Serialize returns the Function as a byte slice. If the Function is invalid, then this returns a nil slice. |
| 28 | func (function Function) Serialize(ctx context.Context) ([]byte, error) { |
| 29 | if !function.ID.IsValid() { |
| 30 | return nil, nil |
| 31 | } |
| 32 | |
| 33 | // Write all of the functions to the writer |
| 34 | writer := utils.NewWriter(256) |
| 35 | writer.VariableUint(3) // Version |
| 36 | // Write the function data |
| 37 | writer.Id(function.ID.AsId()) |
| 38 | writer.Id(function.ReturnType.AsId()) |
| 39 | writer.StringSlice(function.ParameterNames) |
| 40 | writer.IdTypeSlice(function.ParameterTypes) |
| 41 | writer.Bool(function.Variadic) |
| 42 | writer.Bool(function.IsNonDeterministic) |
| 43 | writer.Bool(function.Strict) |
| 44 | writer.String(function.Definition) |
| 45 | // Write the operations |
| 46 | writer.VariableUint(uint64(len(function.Operations))) |
| 47 | for _, op := range function.Operations { |
| 48 | writer.Uint16(uint16(op.OpCode)) |
| 49 | writer.String(op.PrimaryData) |
| 50 | writer.StringSlice(op.SecondaryData) |
| 51 | writer.String(op.Target) |
| 52 | writer.Int32(int32(op.Index)) |
| 53 | writer.StringMap(op.Options) |
| 54 | } |
| 55 | // Write version 1 data |
| 56 | writer.String(function.ExtensionName) |
| 57 | writer.String(function.ExtensionSymbol) |
| 58 | // Write version 2 data |
| 59 | writer.String(function.SQLDefinition) |
| 60 | writer.Bool(function.SetOf) |
| 61 | // Write version 3 data |
| 62 | writer.StringSlice(function.ParameterDefaults) |
| 63 | // Returns the data |
| 64 | return writer.Data(), nil |
| 65 | } |
| 66 | |
| 67 | // DeserializeFunction returns the Function that was serialized in the byte slice. Returns an empty Function (invalid |
| 68 | // ID) if data is nil or empty. |
no test coverage detected