I know how to get the user IP from the *http.Requeststruct:
strings.Split(r.RemoteAddr, ":")[0]
And I know how to define a template.FuncMap:
funcMap = template.FuncMap{
// gets the time since the post was posted
"since": func(t time.Time) string {
s := time.Since(t).String()
return strings.Replace(s[:strings.LastIndex(s, "m")+1], "h", "h ", 1)
},
}
How would I get the users IP from a template function defined in the template.FuncMap?
The func map is intended for helper functions, rather than data, and should be defined once before parsing templates, so this isn't a good place for it. You should instead pass in the data to the view when executing the template.
This would fit better in your data/context for the view. For example if you use a map[string]interface{} for that (one of the few places I'd use interface{}), you can simply assign it there:
userIP := strings.Split(r.RemoteAddr, ":")[0]
data := map[string]interface{}{"userIP":userIP}
err := tmpl.Execute(w,data)
Template:
<p>User IP:{{.userIP}}</p>
Related
I have a function that parses different fields in the array of type ValidationError to generate custom error messages something like the following function.
func foo(err validator.ValidationErrors) []string {
var errStr []string
for _, e := range err {
tag := e.Tag()
field := e.Field()
errStr = append(errStr, tag + ":" + field)
}
return errStr
}
I want to write unit test for this function to ensure that the custom message is as expected. How can I mock a variable of type validator.ValidationError. Below is the structure of ValidationError:
type ValidationErrors []FieldError
FieldError is an interface which contains functions (such as Tag(), Field(), etc.) to get error details.
If you want to unit-test a function that takes validator.ValidationErrors, just construct the test value yourself, using a type (possibly a struct) that implements FieldError.
The methods are not many, but if you want to implement only those that your function calls, you can embed validator.FieldError in the struct type:
type mockFieldError struct {
validator.FieldError
tag string
field string
}
func (e mockFieldError) Tag() string { return e.tag }
func (e mockFieldError) Field() string { return e.field }
And construct validator.ValidationErrors (note that the embedded validator.FieldError is uninitialized, so make sure the function under test doesn't call other methods you didn't implement, or it will panic):
ve := validator.ValidationErrors{
mockFieldError{tag: "a", field: "field1"},
mockFieldError{tag: "b", field: "field2"},
}
So now calling foo with the above value compiles and returns a string that you can assert against your expected output:
s := foo(ve)
fmt.Println(s) // [a:field1 b:field2]
Full playground: https://go.dev/play/p/-btZ6lrKk4V
I have a function which is getting called inside other function.
send_api.go
function *send_api*(client *http.Client,url string) map[string]string,error {
//send api request and parse the response and return the dict
return dictmap
for eg:{apple fruit}
}
Then this function is getting called in main() function
func *main()*{
getmap :=send_api(client *http.Client,"test.com")
}
good.go
func *get_dict_key*(key string) string,error {
value,ok := get_map[key]
if !ok {
return fmt.Errorf("value is nil")
}
return value ,nil
}
function *good*(client *http.client, key string) {
//get a dictionary value
dcmap,err := get_dict_key("apple")
if err != nil {
panic(err)
}
value := dcmap[key]
//use the value to do other processing
}
unit_test
func Test_good(t *testing.T) {
Convey("AND quadra and conusl dcs are mapped",t, func() {
mockResponses := send mock GET request to the URL and receive a response{"apple":"fruit"}
}
server, client := tools.TestClientServer(&mockResponses)
defer server.Close()
getMap := send_api(client.HTTPClient, "http://test")
//At this point getMAP has value {'apple' 'fruit'}
**q1.How to pass getMap value inside this get_dict_fkey function during testing?**
value := get_dict_key("apple")
good(client,"apple") #error:(value is nil)
Q1. **q1.How to pass getMap value inside this get_dict_function during testing?*
Any pointer would be helpful?
Assuming you are facing difficulty to mock http.Client, I would like to suggest following options.
1. Refactor the code
You need to refactor the code in such a way that you can pass the mockable dependencies to function that you would like to test.
For example,
Refactor func send_api(client *http.Client,url string) map[string]string,error so that it does api request and getting/parsing data, but call another function from it, which does the further processing (that actually you would like to test and not the http.Client part).
But, with this approach, you may not be able to test end to end flow. But you can test those functions separately.
2. Mock http.Client
Again, you may need to refactor your code. Some related article can be found here
Update: Recommending to watch justforfunc #16: unit testing HTTP servers
Is there a simple way like template.ParseFiles("base.html", "home.html") but for strings to build a template from a set of strings?
I have a base template and a list of pages templates (all as strings) that I want to build on top of base template.
I figured out how to merge them, but I my solution is quite verbose and doesn't look elegant enough, even though working.
You may create a new, empty template using template.New() function. Then you may use the Template.New() method to create a new, empty, associated template. And you may parse "into" this using the Template.Parse() method.
Here's how it could look like:
func parseTemplates(templs ...string) (t *template.Template, err error) {
t = template.New("_all")
for i, templ := range templs {
if _, err = t.New(fmt.Sprint("_", i)).Parse(templ); err != nil {
return
}
}
return
}
Testing it:
t, err := parseTemplates(
`{{define "one"}}I'm #1.{{end}}`,
`{{define "two"}}I'm #2, including #1: {{template "one" .}}{{end}}`,
)
if err != nil {
panic(err)
}
if err = t.ExecuteTemplate(os.Stdout, "two", nil); err != nil {
panic(err)
}
Output (try it on the Go Playground):
I'm #2, including #1: I'm #1.
Also see related question: Go template name
Note
While we could call the Template.Parse() method on a single template multiple times, and it would parse multiple named templates properly, it is still advisable to acquire a new template.Template for each by calling Template.New(). Because if the template texts have content outside of named templates, they will be overwritten and only the last would be retained. For example: abc {{define "one"}}no 1{{end}}. The static text "abc" would be lost by a subsequent Template.Parse() call.
This is also noted in the doc of Template.Parse():
(In multiple calls to Parse with the same receiver template, only one call can contain text other than space, comments, and template definitions.)
Maybe
for _, templ := range ListOfPagesTemplates{
YourBaseTemplate.Parse(templ)
}
err check absent for simplicity of reading
I have a HTML template that I execute passing a map[string]string variable. The template uses the variable to create the HTML output that I send to clients.
In addition to producing the HTML, I would like to use the very same template to generate some values that are retured to the main program, so I can use the same file to put some logic externally.
As far as I know, it is not possible to modify the variable I pass to Execute (something like {{.output = "value"}}).
So how could I get multiple output values from a template Execution?
You don't actually need to pass a funcmap, just pass the struct.
var tmpl = template.Must(template.New("test").Parse(`Before: {{.Data}}{{.Set "YY"}}, after: {{.Data}}`))
func main() {
c := &CustomData{"XX"}
tmpl.Execute(os.Stdout, c)
fmt.Println()
}
playground
You can always pass a FuncMap to the template, here's an extremely simple example:
const tmpl = `Before: {{.Data}}{{.Set "YY"}}, after: {{.Data}}`
type CustomData struct {
Data string
}
func (c *CustomData) Set(d string) string { // it has to return anything
c.Data = d
return ""
}
func main() {
c := &CustomData{"XX"}
funcMap := template.FuncMap{
"Set": c.Set,
}
t, _ := template.New("test").Funcs(funcMap).Parse(tmpl) // don't ignore errors in real code
t.Execute(os.Stdout, c)
fmt.Println()
}
playground
Is it possible to access the name of current template in Golang text/html/template without passing it as a data element to the template?
Thanks!
I'm hoping this is what you meant (from http://golang.org/pkg/text/template/#Template.Name)
func (t *Template) Name() string
"Name returns the name of the template."
If you mean to access the template name from within the template, I can only think to either add a function to the template.FuncMap, or, as you suggested to add the name as a data element.
The first would probably look something like:
var t = template.Must(template.New("page.html").ParseFiles("page.html"))
t.Funcs(template.FuncMap{"name": fmt.Sprint(t.Name())})
but I can't get it to work in the quick time I've messed about with it. Hopefully it might help point you in the right direction.
It would probably be easier in the long run just to add the name as a data element.
EDIT: In case anyone wants to know how to do it using template.FuncMap, it's basically a matter of defining the function after you create the template, then adding it to the FuncMap:
Full running example:
func main() {
const text = "{{.Thingtype}} {{templname}}\n"
type Thing struct {
Thingtype string
}
var thinglist = []*Thing{
&Thing{"Old"},
&Thing{"New"},
&Thing{"Red"},
&Thing{"Blue"},
}
t := template.New("things")
templateName := func() string { return t.Name() }
template.Must(t.Funcs(template.FuncMap{"templname": templateName}).Parse(text))
for _, p := range thinglist {
err := t.Execute(os.Stdout, p)
if err != nil {
fmt.Println("executing template:", err)
}
}
}
Outputs:
Old things
New things
Red things
Blue things
Playground link: http://play.golang.org/p/VAg5Gv5hCg