Go: how to get autocomplete for any go file - go

Seems go has autocomplete data gettable via some go program. any example how to? e.g. i have file test.go and line:column 10:12. this pos (10:12) is after some func name, e.g. "getn" - can I get list of all funcs which begin with "getn".
ie, get autocomplete data.

Tks to comment. Answer is gocode https://github.com/nsf/gocode (it has document inside zip).

Related

Carvel YTT module returns no match on simple replacement overlay when integrating with go

I am trying to integrate carvel ytt module with my app. So far I am trying some basic stuff I have previously tested in playground. When I try to do the same thing in code, it always returns no match.
filesToProcess := yttfiles.NewSortedFiles([]*yttfiles.File{
yttfiles.MustNewFileFromSource(yttfiles.NewBytesSource(yttPath, readByteFile(yttPath))),
})
opts.DataValuesFlags = yttcmd.DataValuesFlags{
FromFiles: []string{filePath},
ReadFilesFunc: func(path string) ([]*yttfiles.File, error) {
return []*yttfiles.File{yttfiles.MustNewFileFromSource(yttfiles.NewBytesSource(path, readByteFile(path)))}, nil
},
}
output := opts.RunWithFiles( yttcmd.Input{Files: filesToProcess}, yttui.NewTTY(false))
yttPath is path to yaml containing overlay. Path is path to file I want to edit.
readByteFile is just wrapped ioutil.ReadFile.
I am not sure if using overlays in code like this is possible, because there is no example on overlays on their github.
Is this code completely wrong? Thanks in advance to anyone experienced who looks into this.
The way in which you're configuring and invoking ytt seems legit to me.
I hear you when you say this is stuff that's working in the Playground. But, if your program is running and you don't get an error, it makes me suspect that there's some subtle issue with the overlay itself.
If you could post a safe-to-share version of yttPath's contents, I might be able to help.
Another angle is to start with the examples/integrating-with-ytt/internal-templating/ example and stick the contents of your yttPath into the tpl variable in that example's run() function.

How to open YAML file, change something and save it back in Go?

I need to change some values in YAML file from Go code. In my case, I need to change values.yaml file from Helm chart. Since that file can change, I do not structure of the whole file in advance (for example developers added new YAML sections in it in various projects). I just know how section that I want to change looks like.
I understand there is YAML library in Go (https://github.com/go-yaml/yaml). It will not do the job, because it assumes I know in advance structure of the file that I need to change. All examples are something like:
1. create struct
2. unmarshal YAML to struct
3. change
4. marshal and save back
It is not working for me since I do not know exact format of file, hence I cannot do step 1, create struct.
This is part of YAML file I am trying to change:
image:
repository: nginx
tag: stable
pullPolicy: IfNotPresent
I understand this can be done with help of interface{}, but I do not understand how. Assuming that I understand struct, marshal/unmarshal YAML files, can someone provide code that will:
1. Load YAML file that has at least 20 entries in it and is of unknown structure
2. Change only 1 entry (in my case I want to change tag number for image section)
3. Save it back.
Thanks a lot !
Something like this should work:
data, err := ioutil.ReadFile(file)
var v interface{}
err = yaml.Unmarshal(data, &v)
img, ok := v.["image"].(map[interface{}]interface{})
if ok {
img["tag"] = "somevalue"
}
The yaml library I use unmarshals into map[interface{}]interface{}. You need to add the necessary error checking, type assertions, etc.
When done, you can yaml.Marshal(v) and write the result.

protoc-gen-go struct xxx covert to map[string]interface{}

The struct in the .pb.go file generated by .proto file has three additional fields and some other things.like this:
When converting this struct to json, if one field is empty, the field will not appear in json. Now I know it can be done using jsonpb.Marshaler.
m := jsonpb.Marshaler{EmitDefaults: true}
Now, I coverting struct to map[string]interface{}, put it in
InfluxDB. I have to convert struct to map[string]interface{}.The function NewPoint needs. like this:
I use structs.Map(value) function in go ,The transformed map has three additional fields, and running the program causes errors,like this:
{"error":"unable to parse 'txt,severity=1 CurrentValue=\"1002\",MetricAlias=\"CPU\",XXX_sizecache=0i,XXX_unrecognized= 1552551101': missing field value"}
When I remove these three fields, the program runs OK.These three fields are automatically generated, and I have a lot of structs.
What should I do?Thank you!
Protobuf generator adds some additional fields with names starting from XXX that are intended for optimizations. You can't change this behavior of protoc-gen-go.
The problem is in the way you convert struct to map[sting]interface{}. It's hard to figure out from which package exactly structs.Map comes from. Seems like it goes from here: https://github.com/fatih/structs/blob/master/structs.go#L89 - this code uses reflect to iterate through all fields of the structure and push them to map[sting]interface{}. You just need to write your own slightly modified version of FillMap routine that will omit XXX fields.

Go BigQuery package installs with errors (Go version 1.10.1)

For some reason, I get the following errors when installing the cloud.google.com/go/bigquery package. I'm a Go noob, so I assume I'm doing something wrong. I was wondering if anyone was able to replicate, or could point me in the direction about how to resolve these errors? For example, is using a bool type in place of a *bool legal in prior versions of Go? Perhaps this is a bug in the Google package?
$ go get -u cloud.google.com/go/bigquery
# cloud.google.com/go/bigquery
gocode/src/cloud.google.com/go/bigquery/query.go:166:22: cannot use true (type bool) as type *bool in assignment
gocode/src/cloud.google.com/go/bigquery/query.go:168:22: cannot use false (type bool) as type *bool in assignment
gocode/src/cloud.google.com/go/bigquery/query.go:199:15: cannot use qq.UseLegacySql (type *bool) as type bool in field value
gocode/src/cloud.google.com/go/bigquery/query.go:200:22: invalid operation: ! *bool
Looks like someone pushed a broken change to the Go BigQuery client last night.
https://github.com/GoogleCloudPlatform/google-cloud-go/commit/c718c274c122d2ca258bb8f93830d820cbb2160d
Should be fixed now. However, if you're using dep for Go dependency management, it looks like you'll need to set the revision like so in Gopkg.toml (if you are trying to use v0.21.0 of the GoogleCloudPlatform/google-cloud-go client):
[[constraint]]
name = "cloud.google.com/go"
revision = "c718c274c122d2ca258bb8f93830d820cbb2160d"
Sad times.

Convert xpath node back to html-markup in Go

import (
"fmt"
"gopkg.in/xmlpath.v2"
"log"
)
...
path := xmlpath.MustCompile("//div[#id='23']")
tree, err := xmlpath.ParseHTML(reader)
if err != nil {
log.Fatal("HTML parsing error, maybe not wellformed", err)
}
iter := path.Iter(tree)
for iter.Next() {
fmt.Println(iter.Node().String()) // returns only the values of the text-node
}
...
Is there a way to convert iter.Node() back to html markup like <div>...</div>? iter.Node().String() returns only the values of all inner text nodes. As far as I see the documentation of the xmlpath-package does not offer such function.
You are right - gopkg.in/xmlpath.v2 functions are limited to read content of nodes. And there is not many alternatives in Go to work with DOM.
From native Go libraries I can mention only goquery. It works only with HTML and does not support XPath but support CSS selectors. Maybe that would be enough in your case.
If you really need to work with both HTML and XML via XPath there is libxml wrapper for Go called gokogiri. It supports all features of libxml so you can get nodes, inner/outerHTML, attributes and other things. I used it to extract text content in one service which currently is in production state. It's a bit faster than PHP's DOMDocument. Only one limitation is fact that I'm not sure if it supports Go versions higher than 1.4.*. Oh and installation on Windows is a bit tricky.
I know this answer is to late, but still recommend these package written by native Go: xquery and xpath. it supports extract data or evaluate value from XML/HTML using XPath expression.

Resources