Skip to content

Instantly share code, notes, and snippets.

@iafan
Last active August 29, 2016 19:35
Show Gist options
  • Save iafan/8d347f4194aa6a668c2baa7de16713e0 to your computer and use it in GitHub Desktop.
Save iafan/8d347f4194aa6a668c2baa7de16713e0 to your computer and use it in GitHub Desktop.
Reimplementation of camilstore's gensearchtypes.sh in Go
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
)
func processDoc(pkg string, regexps []*regexp.Regexp) []byte {
args := []string{"doc", pkg}
cmd := exec.Command("go", args...)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Errorf("go doc %s error: %v, %v", pkg, err, string(out)))
os.Exit(1)
}
for _, re := range regexps {
out = re.ReplaceAll(out, nil)
}
return out
}
func main() {
var b bytes.Buffer
b.WriteString(`
// generated by gensearchtypes.go; DO NOT EDIT
/*
Copyright 2016 The Camlistore Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"net/url"
"time"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/types/camtypes"
)
// Duplicating the search pkg types in here - since we only use them for json
// decoding - , instead of importing them through the search package, which would
// bring in more dependencies, and hence a larger js file.
// To give an idea, the generated publisher.js is ~3.5MB, whereas if we instead import
// camlistore.org/pkg/search to use its types instead of the ones below, we grow to
// ~5.7MB.
`)
matchFunc := regexp.MustCompile("(?m)^func.*$")
matchIdented := regexp.MustCompile("(?m)^ .*$")
matchDescribeRequest := regexp.MustCompile("(?m)^ Request\\ \\*DescribeRequest.*$")
reSet1 := []*regexp.Regexp{matchFunc, matchIdented}
reSet2 := []*regexp.Regexp{matchDescribeRequest, matchFunc, matchIdented}
b.Write(processDoc("camlistore.org/pkg/search.SearchResult", reSet1))
b.Write(processDoc("camlistore.org/pkg/search.SearchResultBlob", reSet1))
b.Write(processDoc("camlistore.org/pkg/search.DescribeResponse", reSet1))
b.Write(processDoc("camlistore.org/pkg/search.MetaMap", reSet1))
// stripping DescribeRequest from DescribeBlob because it would pull a lot more of search pkg in
b.Write(processDoc("camlistore.org/pkg/search.DescribedBlob", reSet2))
b.Write(processDoc("camlistore.org/pkg/search.DescribedPermanode", reSet1))
fmt.Println(b.String())
}
@iafan
Copy link
Author

iafan commented Aug 29, 2016

See perkeep/perkeep#848 for further information.

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