Ubuntu Pastebin

Paste from rog at Fri, 27 May 2016 18:16:34 +0000

Download as text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
package main
import (
	"os"
	"encoding/json"
	"log"
	_ "github.com/juju/juju/apiserver"
	"github.com/juju/juju/apiserver/common"
	"github.com/juju/juju/rpc/rpcreflect"
	"github.com/rogpeppe/apicompat/jsontypes"
)

type APIInfo struct {
	TypeInfo *jsontypes.Info
	Facades []FacadeInfo
}

type FacadeInfo struct {
	Name string
	Version int
	Methods []Method
}

type Method struct {
	Name string
	Param *jsontypes.Type	`json:",omitempty"`
	Result *jsontypes.Type	`json:",omitempty"`
}

func main() {
	info := jsontypes.NewInfo()
	for _, d := range common.Facades.ListDetails() {
		t := rpcreflect.ObjTypeOf(d.Type)
		
		for _, name := range t.MethodNames() {
			m, _ := t.Method(name)
			if m.Params != nil {
				info.TypeInfo(m.Params)
			}
			if m.Result != nil {
				info.TypeInfo(m.Result)
			}
		}
	}
	apiInfo := &APIInfo{
		TypeInfo: info,
	}
	for _, d := range common.Facades.ListDetails() {
		f := FacadeInfo{
			Name: d.Name,
			Version: d.Version,
		}
		t := rpcreflect.ObjTypeOf(d.Type)
		for _, name := range t.MethodNames() {
			m, _ := t.Method(name)
			fm := Method{
				Name: name,
			}
			if m.Params != nil {
				fm.Param = info.Ref(m.Params)
			}
			if m.Result != nil {
				fm.Result = info.Ref(m.Result)
			}
			f.Methods = append(f.Methods, fm)
		}
		apiInfo.Facades = append(apiInfo.Facades, f)
	}
	data, err := json.Marshal(apiInfo)
	if err != nil {
		log.Fatal(err)
	}
	os.Stdout.Write(data)
}

// in github.com/juju/juju/apiserver/common
//
//package common
//
//type FacadeDetails struct {
//	Name string
//	Version int
//	Factory interface{}
//	Type reflect.Type
//	Feature string
//}
//
//func (f *FacadeRegistry) ListDetails() []FacadeDetails {
//	names := make([]string, 0, len(f.facades))
//	for name := range f.facades {
//		names = append(names, name)
//	}
//	sort.Strings(names)
//	var details []FacadeDetails
//	for _, name := range names {
//		for v, info := range f.facades[name] {
//			details = append(details, FacadeDetails{
//				Name: name,
//				Version: v,
//				Factory: info.factory,
//				Type: info.facadeType,
//				Feature: info.feature,
//			})
//		}
//	}
//	return details
//}
Download as text