pluginunmarshal

package module
v0.0.0-...-397ad23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2016 License: MIT Imports: 4 Imported by: 0

README

pluginunmarshal Build Status GoDoc

Package pluginunmarshal unmarshals Go plugins into structs.

// Assume pathToPlugin refers to a Go plugin file compiled from this code:
//
//   package main
//
//   var Hello = "Hello from a plugin!"
//
//   func Add(a, b int) int {
//      return a + b
//   }
//

var v struct {
    Add     func(a, b int) int
    MyHello string `plugin:"Hello"`
    Ignored bool   `plugin:"-"`
}

err := pluginunmarshal.Open(pathToPlugin, &v)
if err != nil {
    panic(err)
}

fmt.Println(v.Add(2, 3))
fmt.Println(v.MyHello)
// Output:
// 5
// Hello from a plugin!

See the docs for more.

Documentation

Overview

Package pluginunmarshal unmarshals Go plugins into structs.

Example
package main

import (
	"fmt"

	"github.com/tcard/pluginunmarshal"
)

func main() {
	// Assume pathToPlugin refers to a Go plugin file compiled from this code:
	//
	//   package main
	//
	//   var Hello = "Hello from a plugin!"
	//
	//   func Add(a, b int) int {
	//   	return a + b
	//   }
	//

	var v struct {
		Add     func(a, b int) int
		MyHello string `plugin:"Hello"`
		Ignored bool   `plugin:"-"`
	}

	err := pluginunmarshal.Open(pathToPlugin, &v)
	if err != nil {
		panic(err)
	}

	fmt.Println(v.Add(2, 3))
	fmt.Println(v.MyHello)
}

var pathToPlugin string
Output:

5
Hello from a plugin!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(path string, v interface{}) error

Open is a convenience function for calling "plugin".Open and then calling Unmarshal with the resulting plugin.

func Unmarshal

func Unmarshal(p *plugin.Plugin, v interface{}) error

Unmarshal stores exported values from a plugin into the struct pointed to by v.

By default, each exported field in the struct will be assigned to a package-level exported value in the plugin with the same name as the field.

The plugin value's type must be assignable to the field's type.

If the plugin value is variable, the field's type can also be a pointer to the variable's type; in that case, the field will point to the variable.

If no such value exists in the plugin, Unmarshal returns an error.

This behavior can be modified by a struct tag with key "plugin", as follows:

// Field is ignored by this package.
Field int `plugin:"-"`

// Field is mapped to the package-level value Other from the plugin.
Field int `plugin:"Other"`

// If a value named "Field" isn't exported from the plugin, the struct
// field Field will be ignored instead of Unmarshal returning an error.
Field int `plugin:",omitempty"`

See package-level examples.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL