Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tools link of gojs-tool #280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xujinzheng
Copy link

gojs-tool

A tool for export package funcs, types, vars for VM (like goja and otto) to use

Install

go get -u -v github.com/gogap/gojs-tool

Command

> gojs-tool gen --help
 
NAME:
   gojs-tool gen - generate modules

USAGE:
   gojs-tool gen [command options] packages

OPTIONS:
   --template value, -t value  template filename in templates folder without extension ("goja, otto")
   --recusive, -r              recusive generate code
   --namespace, -n             generate submodules loader, access object by dot, only work while recusive enabled
   --gopath value              the package in which GOPATH [$GOPATH]

Example

example> pwd
github.com/xujinzheng/playgo/example

example> gojs-tool gen -t otto -r -n github.com/sirupsen/logrus ## for otto

example> tree modules
modules
└── github.com
    └── sirupsen
        └── logrus
            ├── hooks
            │   ├── hooks.go
            │   ├── syslog
            │   │   └── syslog.go
            │   └── test
            │       └── test.go
            └── logrus.go

Auto generated code (otto)

package logrus

import (
	original_logrus "github.com/sirupsen/logrus"
)

import (
	"github.com/robertkrimen/otto"
)

type Logrus struct {
	// Functions
	AddHook             interface{}
	Debug               interface{}
	Debugf              interface{}
	Debugln             interface{}
	Error               interface{}
   //..........more funcs..........

	// Var and consts
	AllLevels     interface{}
	DebugLevel    interface{}
	ErrorKey      interface{}
	ErrorLevel    interface{}
	//..........more vars..........

	// Types (value type)
	Entry         interface{}
	JSONFormatter interface{}
	Logger        interface{}
	MutexWrap     interface{}
	TextFormatter interface{}

	// Types (pointer type)
	NewJSONFormatter interface{}
	NewLogger        interface{}
	NewMutexWrap     interface{}
	NewTextFormatter interface{}
}

var (
	module = &Logrus{
		// Functions
		AddHook:             original_logrus.AddHook,
		Debug:               original_logrus.Debug,
		Debugf:              original_logrus.Debugf,
		Debugln:             original_logrus.Debugln,
		//..........more funcs..........

		// Var and consts
		AllLevels:     original_logrus.AllLevels,
		DebugLevel:    original_logrus.DebugLevel,
		ErrorKey:      original_logrus.ErrorKey,
		ErrorLevel:    original_logrus.ErrorLevel,
		//..........more vars..........

		// Types (value type)
		Entry:         func() original_logrus.Entry { return original_logrus.Entry{} },
		JSONFormatter: func() original_logrus.JSONFormatter { return original_logrus.JSONFormatter{} },
		Logger:        func() original_logrus.Logger { return original_logrus.Logger{} },
		MutexWrap:     func() original_logrus.MutexWrap { return original_logrus.MutexWrap{} },
		TextFormatter: func() original_logrus.TextFormatter { return original_logrus.TextFormatter{} },

		// Types (pointer type)
		NewJSONFormatter: func() *original_logrus.JSONFormatter { return &original_logrus.JSONFormatter{} },
		NewLogger:        func() *original_logrus.Logger { return &original_logrus.Logger{} },
		NewMutexWrap:     func() *original_logrus.MutexWrap { return &original_logrus.MutexWrap{} },
		NewTextFormatter: func() *original_logrus.TextFormatter { return &original_logrus.TextFormatter{} },
	}
)

func Enable(o *otto.Otto) {
	o.Set("logrus", module)
}

Use native module in otto

package main

import (
	"fmt"
)

import (
	"github.com/xujinzheng/playgo/example/modules/github.com/denverdino/aliyungo"
	"github.com/xujinzheng/playgo/example/modules/github.com/sirupsen/logrus"
)

import (
	"github.com/robertkrimen/otto"
)

func main() {

	runtime := otto.New()

	logrus.Enable(runtime)
	aliyungo.Enable(runtime)

	_, err := runtime.Run(`
		var entryA = logrus.NewEntry(undefined)
		var entryB = logrus.Entry()
		
		logrus.Println("entryA:",entryA)
		logrus.Println("entryB:",entryB)
    	
    	logrus.WithField("Hello", "World").Println("I am gojs")

    	// gojs-tool gen --template otto --recusive --namespace github.com/denverdino/aliyungo
    	// wrapper packages in namespace, access by dot
    	var client = aliyungo.cs.NewClient("","") 

    	logrus.Println("client", client)
    `)

	if err != nil {
		fmt.Println(err)
		return
	}
}

example> go run main.go
INFO[0000] entryA: &{<nil> map[] 0001-01-01 00:00:00 +0000 UTC panic  <nil>}
INFO[0000] entryB: {<nil> map[] 0001-01-01 00:00:00 +0000 UTC panic  <nil>}
INFO[0000] I am gojs                                     Hello=World
INFO[0000] client &{  https://cs.aliyuncs.com 2015-12-15 false  0xc4201824b0}

@deoxxa
Copy link
Collaborator

deoxxa commented Oct 27, 2017

Hey, this is really neat! I'll take a look at it properly sometime soon and make sure it's suitable to link to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants