本文整理汇总了Golang中golang.org/x/tools/godoc/vfs/mapfs.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestMakeFnIncludeFile_BasicRule
func TestMakeFnIncludeFile_BasicRule(t *testing.T) {
templateRules := template.Rules{}
templateRules.Attach(func(path []interface{}, node interface{}) (interface{}, interface{}) {
key := interface{}(nil)
if len(path) > 0 {
key = path[len(path)-1]
}
if key == "content" {
return key, interface{}("replaced")
}
return key, node
})
fs := mapfs.New(map[string]string{"a": "{\"content\": 1}"})
fnIncludeFile := MakeFnIncludeFile(fs, &templateRules)
input := interface{}(map[string]interface{}{
"Fn::IncludeFile": "/a",
})
expected := interface{}(map[string]interface{}{"content": "replaced"})
newKey, newNode := fnIncludeFile([]interface{}{"x", "y"}, input)
if newKey != "y" {
t.Fatalf("FnIncludeFile modified the path (%v instead of %v)", newKey, "y")
}
if !reflect.DeepEqual(newNode, expected) {
t.Fatalf("FnIncludeFile did not return the expected result (%#v instead of %#v)", newNode, expected)
}
}
示例2: fsMapHandler
func fsMapHandler() types.RequestHandler {
var fileHandler = http.FileServer(httpfs.New(mapfs.New(fsmap)))
return types.RequestHandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
fileHandler.ServeHTTP(w, r)
})
}
示例3: TestNewNameSpace
func TestNewNameSpace(t *testing.T) {
// We will mount this filesystem under /fs1
mount := mapfs.New(map[string]string{"fs1file": "abcdefgh"})
// Existing process. This should give error on Stat("/")
t1 := vfs.NameSpace{}
t1.Bind("/fs1", mount, "/", vfs.BindReplace)
// using NewNameSpace. This should work fine.
t2 := emptyvfs.NewNameSpace()
t2.Bind("/fs1", mount, "/", vfs.BindReplace)
testcases := map[string][]bool{
"/": []bool{false, true},
"/fs1": []bool{true, true},
"/fs1/fs1file": []bool{true, true},
}
fss := []vfs.FileSystem{t1, t2}
for j, fs := range fss {
for k, v := range testcases {
_, err := fs.Stat(k)
result := err == nil
if result != v[j] {
t.Errorf("fs: %d, testcase: %s, want: %v, got: %v, err: %s", j, k, v[j], result, err)
}
}
}
}
示例4: TestReadOnly
func TestReadOnly(t *testing.T) {
m := map[string]string{"x": "y"}
rfs := mapfs.New(m)
wfs := ReadOnly(rfs)
if _, err := rfs.Stat("/x"); err != nil {
t.Error(err)
}
_, err := wfs.Create("/y")
if want := (&os.PathError{"create", "/y", ErrReadOnly}); !reflect.DeepEqual(err, want) {
t.Errorf("Create: got err %v, want %v", err, want)
}
err = wfs.Mkdir("/y")
if want := (&os.PathError{"mkdir", "/y", ErrReadOnly}); !reflect.DeepEqual(err, want) {
t.Errorf("Mkdir: got err %v, want %v", err, want)
}
err = wfs.Remove("/y")
if want := (&os.PathError{"remove", "/y", ErrReadOnly}); !reflect.DeepEqual(err, want) {
t.Errorf("Remove: got err %v, want %v", err, want)
}
}
示例5: ExampleWalk
func ExampleWalk() {
var fs http.FileSystem = httpfs.New(mapfs.New(map[string]string{
"zzz-last-file.txt": "It should be visited last.",
"a-file.txt": "It has stuff.",
"another-file.txt": "Also stuff.",
"folderA/entry-A.txt": "Alpha.",
"folderA/entry-B.txt": "Beta.",
}))
walkFn := func(path string, fi os.FileInfo, err error) error {
if err != nil {
log.Printf("can't stat file %s: %v\n", path, err)
return nil
}
fmt.Println(path)
return nil
}
err := vfsutil.Walk(fs, "/", walkFn)
if err != nil {
panic(err)
}
// Output:
// /
// /a-file.txt
// /another-file.txt
// /folderA
// /folderA/entry-A.txt
// /folderA/entry-B.txt
// /zzz-last-file.txt
}
示例6: Example
func Example() {
fs0 := httpfs.New(mapfs.New(map[string]string{
"zzz-last-file.txt": "It should be visited last.",
"a-file.txt": "It has stuff.",
"another-file.txt": "Also stuff.",
"folderA/entry-A.txt": "Alpha.",
"folderA/entry-B.txt": "Beta.",
}))
fs1 := httpfs.New(mapfs.New(map[string]string{
"sample-file.txt": "This file compresses well. Blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah!",
"not-worth-compressing-file.txt": "Its normal contents are here.",
"folderA/file1.txt": "Stuff 1.",
"folderA/file2.txt": "Stuff 2.",
"folderB/folderC/file3.txt": "Stuff C-3.",
}))
fs := union.New(map[string]http.FileSystem{
"/fs0": fs0,
"/fs1": fs1,
})
err := vfsutil.Walk(fs, "/", walk)
if err != nil {
panic(err)
}
// Output:
// /
// /fs0
// /fs0/a-file.txt
// /fs0/another-file.txt
// /fs0/folderA
// /fs0/folderA/entry-A.txt
// /fs0/folderA/entry-B.txt
// /fs0/zzz-last-file.txt
// /fs1
// /fs1/folderA
// /fs1/folderA/file1.txt
// /fs1/folderA/file2.txt
// /fs1/folderB
// /fs1/folderB/folderC
// /fs1/folderB/folderC/file3.txt
// /fs1/not-worth-compressing-file.txt
// /fs1/sample-file.txt
}
示例7: init
func init() {
enforceHosts = !appengine.IsDevAppServer()
playEnabled = true
log.Println("initializing godoc ...")
log.Printf(".zip file = %s", zipFilename)
log.Printf(".zip GOROOT = %s", zipGoroot)
log.Printf("index files = %s", indexFilenames)
goroot := path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
// read .zip file and set up file systems
const zipfile = zipFilename
rc, err := zip.OpenReader(zipfile)
if err != nil {
log.Fatalf("%s: %s\n", zipfile, err)
}
// rc is never closed (app running forever)
fs.Bind("/", zipfs.New(rc, zipFilename), goroot, vfs.BindReplace)
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
corpus := godoc.NewCorpus(fs)
corpus.Verbose = false
corpus.MaxResults = 10000 // matches flag default in main.go
corpus.IndexEnabled = true
corpus.IndexFiles = indexFilenames
if err := corpus.Init(); err != nil {
log.Fatal(err)
}
corpus.IndexDirectory = indexDirectoryDefault
go corpus.RunIndexer()
pres = godoc.NewPresentation(corpus)
pres.TabWidth = 8
pres.ShowPlayground = true
pres.ShowExamples = true
pres.DeclLinks = true
pres.NotesRx = regexp.MustCompile("BUG")
readTemplates(pres, true)
mux := registerHandlers(pres)
dl.RegisterHandlers(mux)
short.RegisterHandlers(mux)
// Register /compile and /share handlers against the default serve mux
// so that other app modules can make plain HTTP requests to those
// hosts. (For reasons, HTTPS communication between modules is broken.)
proxy.RegisterHandlers(http.DefaultServeMux)
log.Println("godoc initialization complete")
}
示例8: main
func main() {
server, err := blog.NewServer(cfg)
if err != nil {
log.Fatal(err)
}
http.Handle("/", server)
http.Handle("/lib/godoc/", http.StripPrefix("/lib/godoc/",
http.FileServer(httpfs.New(mapfs.New(static.Files))),
))
log.Fatal(http.ListenAndServe(":3999", nil))
}
示例9: NewTestShell
// NewTestShell builds a test shell, notionally at path, with a set of files available
// to it built from the files map. Note that relative paths in the map are
// considered wrt the path
func NewTestShell(path string, files map[string]string) (*TestShell, error) {
ts := TestShell{
CmdsF: blissfulSuccess,
Sh: Sh{
Cwd: path,
Env: os.Environ(),
},
}
fs := make(map[string]string)
for n, c := range files {
fs[strings.TrimPrefix(ts.Abs(n), "/")] = c
}
ts.FS = mapfs.New(fs)
return &ts, nil
}
示例10: Map
// Map returns a new FileSystem from the provided map. Map keys should be
// forward slash-separated pathnames and not contain a leading slash.
func Map(m map[string]string) FileSystem {
fs := mapFS{
m: m,
dirs: map[string]struct{}{"": struct{}{}},
FileSystem: mapfs.New(m),
}
// Create initial dirs.
for path := range m {
if err := MkdirAll(fs, filepath.Dir(path)); err != nil {
panic(err.Error())
}
}
return fs
}
示例11: newCorpus
func newCorpus(t *testing.T) *Corpus {
c := NewCorpus(mapfs.New(map[string]string{
"src/foo/foo.go": `// Package foo is an example.
package foo
import "bar"
const Pi = 3.1415
var Foos []Foo
// Foo is stuff.
type Foo struct{}
func New() *Foo {
return new(Foo)
}
`,
"src/bar/bar.go": `// Package bar is another example to test races.
package bar
`,
"src/other/bar/bar.go": `// Package bar is another bar package.
package bar
func X() {}
`,
"src/skip/skip.go": `// Package skip should be skipped.
package skip
func Skip() {}
`,
"src/bar/readme.txt": `Whitelisted text file.
`,
"src/bar/baz.zzz": `Text file not whitelisted.
`,
}))
c.IndexEnabled = true
c.IndexDirectory = func(dir string) bool {
return !strings.Contains(dir, "skip")
}
if err := c.Init(); err != nil {
t.Fatal(err)
}
return c
}
示例12: init
func init() {
playEnabled = true
log.Println("initializing godoc ...")
log.Printf(".zip file = %s", zipFilename)
log.Printf(".zip GOROOT = %s", zipGoroot)
log.Printf("index files = %s", indexFilenames)
goroot := path.Join("/", zipGoroot) // fsHttp paths are relative to '/'
// read .zip file and set up file systems
const zipfile = zipFilename
rc, err := zip.OpenReader(zipfile)
if err != nil {
log.Fatalf("%s: %s\n", zipfile, err)
}
// rc is never closed (app running forever)
fs.Bind("/", zipfs.New(rc, zipFilename), goroot, vfs.BindReplace)
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
corpus := godoc.NewCorpus(fs)
corpus.Verbose = false
corpus.MaxResults = 10000 // matches flag default in main.go
corpus.IndexEnabled = true
corpus.IndexFiles = indexFilenames
if err := corpus.Init(); err != nil {
log.Fatal(err)
}
corpus.IndexDirectory = indexDirectoryDefault
go corpus.RunIndexer()
pres = godoc.NewPresentation(corpus)
pres.TabWidth = 8
pres.ShowPlayground = true
pres.ShowExamples = true
pres.DeclLinks = true
pres.NotesRx = regexp.MustCompile("BUG")
readTemplates(pres, true)
registerHandlers(pres)
log.Println("godoc initialization complete")
}
示例13: main
func main() {
var fs http.FileSystem = httpfs.New(mapfs.New(map[string]string{
"sample-file.txt": "This file compresses well. Blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah!",
"not-worth-compressing-file.txt": "Its normal contents are here.",
"folderA/file1.txt": "Stuff in /folderA/file1.txt.",
"folderA/file2.txt": "Stuff in /folderA/file2.txt.",
"folderB/folderC/file3.txt": "Stuff in /folderB/folderC/file3.txt.",
// TODO: Empty folder somehow?
//"folder-empty/": "",
}))
err := vfsgen.Generate(fs, vfsgen.Options{
Filename: "test_vfsdata_test.go",
PackageName: "test_test",
})
if err != nil {
log.Fatalln(err)
}
}
示例14: loadFS
// loadFS reads the zip file of Go source code and binds.
func loadFS() {
// Load GOROOT (in zip file)
rsc, err := asset.Open(gorootZipFile) // asset file, never closed.
if err != nil {
panic(err)
}
offset, err := rsc.Seek(0, os.SEEK_END)
if err != nil {
panic(err)
}
if _, err = rsc.Seek(0, os.SEEK_SET); err != nil {
panic(err)
}
r, err := zip.NewReader(&readerAt{wrapped: rsc}, offset)
if err != nil {
panic(err)
}
fs.Bind("/", newZipFS(r, gorootZipFile), "/go", vfs.BindReplace)
// static files for godoc.
fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
}
示例15:
// This file was automatically generated based on the contents of *.tmpl
// If you need to update this file, change the contents of those files
// (or add new ones) and run 'go generate'
package main
import "golang.org/x/tools/godoc/vfs/mapfs"
var Templates = mapfs.New(map[string]string{
`ssl-config.tmpl`: "[ req ]\nprompt = no\ndistinguished_name=req_distinguished_name\nx509_extensions = va_c3\nencrypt_key = no\ndefault_keyfile=testing.key\ndefault_md = sha256\n\n[ va_c3 ]\nbasicConstraints=critical,CA:true,pathlen:1\n{{range . -}}\nsubjectAltName = IP:{{.}}\n{{end}}\n[ req_distinguished_name ]\nCN=registry.test\n",
})