How to replace string in Go template? - go

I use "text/template" module.
I have struct like this to parse XML from Blogger
type Media struct {
ThumbnailUrl string `xml:"url,attr"`
}
type Entry struct {
ID string `xml:"id"`
Published Date `xml:"published"`
Updated Date `xml:"updated"`
Draft Draft `xml:"control>draft"`
Title string `xml:"title"`
Content string `xml:"content"`
Tags Tags `xml:"category"`
Author Author `xml:"author"`
Media Media `xml:"thumbnail"`
Extra string
}
Then I create Go Template like this
[image]
src = "{{ replace .Media.ThumbnailUrl 's72-c' 's1600' }}"
link = ""
thumblink = "{{ .Media.ThumbnailUrl }}"
alt = ""
title = ""
author = ""
license = ""
licenseLink = ""
The replace function not defined. I want to replace URL from {{ .Media.ThumbnailUrl }}
For example:
from this URL
https://2.bp.blogspot.com/-DEeRanrBa6s/WGWGwA2qW5I/AAAAAAAADg4/feGUc-g9rXc9B7hXpKr0ecG9UOMXU3_VQCK4B/s72-c/pemrograman%2Bjavascript%2B-%2Bpetanikode.png
To this URL
https://2.bp.blogspot.com/-DEeRanrBa6s/WGWGwA2qW5I/AAAAAAAADg4/feGUc-g9rXc9B7hXpKr0ecG9UOMXU3_VQCK4B/s1600/pemrograman%2Bjavascript%2B-%2Bpetanikode.png

You can write a helper view function like this
func replace(input, from,to string) string {
return strings.Replace(input,from,to, -1)
}
funcMap = template.FuncMap{
"replace": replace,
}
template := template.New("").Funcs(internalFuncMap)
and use the template to render the view.
code ref links
https://github.com/sairam/kinli/blob/master/template_funcs.go#L57-L59
https://github.com/sairam/kinli/blob/master/templates.go#L48

Related

Ignored YAML tag

I have config.yml file:
vcenter:
connection: https
hostname: vcenter.mydomain.lan
username: readonlyauto#vsphere.local
password: mypasspord
port: 443
EsxiExcludeDatastores:
- datastore1
- datastore2
EsxiExcludeDatastores2:
datastores:
- datastore1
- datastore2
I'm trying to parse it with "gopkg.in/yaml.v2"
I created struct:
type FileConfig struct {
Vcenter struct {
ConnectionType string `yaml:"connection"`
Hostname string `yaml:"hostname"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Port int `yaml:"port"`
} `yaml:"vcenter"`
EsxiExcludeDatastores struct {
Datastores []string `yaml:"EsxiExcludeDatastores"`
}
EsxiExcludeDatastores2 struct {
Datastores []string `yaml:"datastores"`
} `yaml:"EsxiExcludeDatastores2"`
}
var AppConfig FileConfig
After parsing, I print results:
fmt.Println("Num of EsxiExcludeDatastores.Datastores = ", len(AppConfig.EsxiExcludeDatastores.Datastores))
fmt.Println("Num of EsxiExcludeDatastores2.Datastores = ", len(AppConfig.EsxiExcludeDatastores2.Datastores))
Num of EsxiExcludeDatastores.Datastores = 0
Num of EsxiExcludeDatastores2.Datastores = 2
Can you help me, where I did mistake in EsxiExcludeDatastores struct? As you see, everything is fine with EsxiExcludeDatastores2, but EsxiExcludeDatastores is empty.
I tried to do this in different ways, but with no result ...
You're almost there.
To "promote" yaml struct tag(s) to the "parent" field, add:
EsxiExcludeDatastores struct {
Datastores []string `yaml:"EsxiExcludeDatastores"`
} `yaml:",inline"` // <- this
https://play.golang.org/p/S7QxGaspTyN
From the yaml.v2 docs, a struct tag that uses the inline flag does the following:
Inline the field, which must be a struct or a map, causing all of its
fields or keys to be processed as if they were part of the outer
struct. For maps, keys must not conflict with the yaml keys of other
struct fields.

Update Contentful entry with a link to a reference filed (in Contentful)

I want to update a field on an entry that is a link to another entry inside Contentful (they call it 'Reference'). However they don't have any clear documentation on how to call the data using SwiftUI. I just need to know how to format that API call when using the Contentful Management API. Didn't get any help from the Contentful team nor in their Slack community, so I'm trying here.
The specific entry in question will be the episodes variable which is linked to [HPtvSeriesEpisodes]. No matter what format I try I get the error "Cannot convert value of type '[Entry]' to expected argument type '[HPtvSeriesEpisodes]"
I just want to know how should I format that specific line of code
This is my Data Model
struct HPtvSeries: Identifiable {
var id = UUID()
var seriesTitle: String
var seriesDescription: String
var seriesDate: String
var seriesType: String
var numberofEpisodes: Int
var seriesBanner: URL
var seriesThumbnail: URL
var episodes: [HPtvSeriesEpisodes]
}
The Content Management API call
class HPtvSeriesStore: ObservableObject{
#Published var hptvSermons: [HPtvSeries] = []
init(){
getArray(id: "hPtvSeries") { (items) in
items.forEach { (item) in
self.hptvSermons.append(HPtvSeries(
seriesTitle: item.fields["seriesTitle"] as! String,
seriesDescription: item.fields["seriesDescription"] as! String,
seriesDate: item.fields["seriesDate"] as! String ,
seriesType: item.fields["seriesType"] as! String ,
numberofEpisodes:item.fields["numberOfEpisodes"] as! Int,
seriesBanner: item.fields.linkedAsset(at: "seriesBanner")?.url ?? URL(string: "https://dl.dropbox.com/s/kqmc8c2i3banhhi/HPtv%20Logo_2.png?dl=0")!,
seriesThumbnail: item.fields.linkedAsset(at: "seriesThumbnail")?.url ?? URL(string: "https://dl.dropbox.com/s/kqmc8c2i3banhhi/HPtv%20Logo_2.png?dl=0")!,
// episodes: item.fields.linkedEntries(at: "episode") // Not sure how to format the code here
))
}
}
}
}

Is it possible to pass an array of json object as parameters of a URL?

I am working on unit test for a restAPI implementation in Golang.
I need to pass an array of object into url.
Here is an example of struct I have:
type version struct {
Name string `json:"name"`
Ver string `json:"ver"`
}
type event struct {
ID string `json:"id"`
Title string `json:"Title"`
Description string `json:"Description"`
Versions []version `json:"versions"`
}
The sample json input i tested in postman will be look like this one
{
"id": "101",
"title": "This is simple Golang title for testing!",
"Description":"Sample code for REST api implementation in Golang 2021!",
"versions": [
{
"name": "pingPong",
"ver": "10.2"
},
{
"name": "Ninja",
"ver": "10.24"
}
]
}
My question is that how can i pass an array of objects as URL parameters.
I expect to have something like below but not how to fill the ending part i highlighted by the ...
url?ID=20&Title=urlTitle&Description=UrlDescription&...
I don't know how you want the URL like, so I wrote it myself in a way that you can change it any way you want, And let me add that I don't know how many versions you have, so I wrote in such a way that no matter how many versions you have, it can handle it.
package main
import (
"fmt"
"strings"
"encoding/json"
)
var jsonData string =
`{
"id": "101",
"title": "This is simple Golang title for testing!",
"Description":"Sample code for REST api implementation in Golang 2021!",
"versions": [
{
"name": "pingPong",
"ver": "10.2"
},
{
"name": "Ninja",
"ver": "10.24"
}
]
}`
type (
Event struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Versions []Version `json:"versions"`
}
Version struct {
Name string `json:"name"`
Ver string `json:"ver"`
}
)
func fillVersions(event *Event, baseUrl string) string {
var finalUrl string = baseUrl
for index, value := range event.Versions {
restUrl := fmt.Sprintf("Version%d=%s-%s", index + 1, value.Name, value.Ver)
finalUrl = fmt.Sprintf(
finalUrl + "%s" + "&",
restUrl,
)
}
return strings.TrimRight(finalUrl, "&")
}
func main() {
var event Event
json.Unmarshal([]byte(jsonData), &event)
baseUrl := fmt.Sprintf(
"https://test.com/test?Id=%s&Title=%s&Description=%s&",
event.Id,
event.Title,
event.Description,
)
finalUrl := fillVersions(&event, baseUrl)
fmt.Println(finalUrl)
}
The output of the program is as follows:
https://test.com/test?Id=101&Title=This is simple Golang title for testing!&Description=Sample code for REST api implementation in Golang 2021!&Version1=pingPong-10.2&Version2=Ninja-10.24
I would also like to say that the last & will be removed, If you don't want to do this, Remove the following line and write as follows (also remove the strings library from the import scope):
return strings.TrimRight(finalUrl, "&") // remove this
return finalUrl // add this

How to perform conditional required validation ussing ozzo validation in golang?

In Golang ozzo-validation, how can I validate a field which is dependent on another field ?
For example, if I have the following:
return validation.ValidateStruct(&c,
validation.Field(&c.Name, validation.Required, validation.Length(5, 20)),
validation.Field(&c.Gender, validation.In("Female", "Male")),
validation.Field(&c.Email, is.Email),
validation.Field(&c.Address),
How can I add a validation that the Address is required only if email is not empty?
You can achieve it in two ways-
Adding your own custom rules
Conditionally add FieldRules based on precondition-value i.e check Email while creating field rules then supply it to validation.ValidateStruct
For e.g.:
type Sample struct {
Name string
Gender string
Email string
Address Address
}
type Address struct {
// ... fields
}
func (s Sample) Validate() error {
var fieldRules []*validation.FieldRules
fieldRules = append(fieldRules, validation.Field(&s.Name, validation.Required, validation.Length(5, 20)))
fieldRules = append(fieldRules, validation.Field(&s.Gender, validation.In("Female", "Male")))
fieldRules = append(fieldRules, validation.Field(&s.Email, is.Email))
if len(strings.TrimSpace(s.Email)) > 0 {
fieldRules = append(fieldRules, validation.Field(&s.Address, validation.Required))
fieldRules = append(fieldRules, validation.Field(&s.Address))
}
return validation.ValidateStruct(&s, fieldRules...)
}
The library now supports conditional validation by the validation.When function.
Here is a code snipped which fits the validation you described.
package main
import (
"fmt"
validation "github.com/go-ozzo/ozzo-validation" // or "github.com/go-ozzo/ozzo-validation/v4" if "When" not found
)
type Entry struct {
Name string
Gender string
Email string
Address string
}
func main() {
v := func(e Entry) {
fmt.Println(validation.ValidateStruct(&e,
validation.Field(&e.Name, validation.Required, validation.Length(5, 20)),
// Note that if gender is "" and not required, validation returns no error.
validation.Field(&e.Gender, validation.Required, validation.In("Female", "Male")),
validation.Field(&e.Address, validation.When(e.Email != "", validation.Required.Error("Address is required if Email is set"))),
))
}
// All is fine for no Email.
e := Entry{
Name: "My name is!",
Gender: "Male",
}
v(e)
// Validation fails for Email and no Address.
e = Entry{
Name: "My name is!",
Gender: "Male",
Email: "a#org.com",
}
v(e)
}
It outputs.
<nil>
Address: Address is required if Email is set.
The library documentation describes it as well: https://github.com/go-ozzo/ozzo-validation#conditional-validation

Rendering template.HTML directly into templates

I've recently swapped out datastores and as a side-effect have had to change a struct field from template.HTML to string to be compatible with the marshaller/DB driver. This field, RenderedDesc, contains rendered HTML as passed through russross/blackfriday.
Previously I could just pass the whole struct into the template "as is" and call {{ .RenderedDesc }} in the template.
Because it's now a string, I've added a filter to convert it back on template render:
templates.go
func RenderUnsafe(s string) template.HTML {
return template.HTML(s)
}
template.FuncMap{
...
"unsafe": RenderUnsafe,
}
_content.tmpl
...
<div class="detail">
{{ .RenderedDesc | unsafe }}
</div>
...
Is there a better way to achieve this without having to use a filter at the template level? Short of re-writing marshalling logic from my DB driver (not on the cards) it looks like this is the simplest way to "store" strings but render raw HTML.
IMHO, the right way to do this is using a filter, like you are already doing. There are more ways to achieve the same, one of them is using tags and converting the struct in to a map[string]Interface{}. Because map fields can be reached in the same way that structs, your templates will remain unmodified.
Show me the code (playground):
package main
import (
"html/template"
"os"
"reflect"
)
var templates = template.Must(template.New("tmp").Parse(`
<html>
<head>
</head>
<body>
<h1>Hello</h1>
<div class="content">
Usafe Content = {{.Content}}
Safe Content = {{.Safe}}
Bool = {{.Bool}}
Num = {{.Num}}
Nested.Num = {{.Nested.Num}}
Nested.Bool = {{.Nested.Bool}}
</div>
</body>
</html>
`))
func asUnsafeMap(any interface{}) map[string]interface{} {
v := reflect.ValueOf(any)
if v.Kind() != reflect.Struct {
panic("asUnsafeMap invoked with a non struct parameter")
}
m := map[string]interface{}{}
for i := 0; i < v.NumField(); i++ {
value := v.Field(i)
if !value.CanInterface() {
continue
}
ftype := v.Type().Field(i)
if ftype.Tag.Get("unsafe") == "html" {
m[ftype.Name] = template.HTML(value.String())
} else {
m[ftype.Name] = value.Interface()
}
}
return m
}
func main() {
templates.ExecuteTemplate(os.Stdout, "tmp", asUnsafeMap(struct {
Content string `unsafe:"html"`
Safe string
Bool bool
Num int
Nested struct {
Num int
Bool bool
}
}{
Content: "<h2>Lol</h2>",
Safe: "<h2>Lol</h2>",
Bool: true,
Num: 10,
Nested: struct {
Num int
Bool bool
}{
Num: 9,
Bool: true,
},
}))
}
Output:
<html>
<head>
</head>
<body>
<h1>Hello</h1>
<div class="content">
Usafe Content = <h2>Lol</h2>
Safe Content = <h2>Lol</h2>
Bool = true
Num = 10
Nested.Num = 9
Nested.Bool = true
</div>
</body>
</html>
Note: the previous code doesn't work with nested structures, but it will be easy to add support for them. Also, every field tagged as unsafe will be treated as string.

Resources