func NewPostFromIncomingPost(incomingPost map[string]string) (*Post, error) {
post := &Post{}
if title, exists := incomingPost[`name`]; exists {
post.Title = title
} else if title, exists := incomingPost[`title`]; exists {
post.Title = title
} else {
return nil, errors.New(`title not found`)
}
if text, exists := incomingPost[`text`]; exists {
post.Text = text
} else if text, exists := incomingPost[`description`]; exists {
post.Text = text
} else {
return nil, errors.New(`text not found`)
}
return post, nil
}package main
import (
"encoding/json"
"fmt"
"errors"
)
type Post struct {
Title string
Text string
}
func NewPostFromIncomingPost(incomingPost map[string]string) (*Post, error) {
post := &Post{}
if title, exists := incomingPost[`name`]; exists {
post.Title = title
} else if title, exists := incomingPost[`title`]; exists {
post.Title = title
} else {
return nil, errors.New(`title not found`)
}
if text, exists := incomingPost[`text`]; exists {
post.Text = text
} else if text, exists := incomingPost[`description`]; exists {
post.Text = text
} else {
return nil, errors.New(`text not found`)
}
return post, nil
}
func main() {
jsonSource1 := []byte(`{"title": "Title", "text": "Text"}`)
jsonSource2 := []byte(`{"name": "Title", "description": "Text"}`)
var incomingPost1 map[string]string
if err := json.Unmarshal(jsonSource1, &incomingPost1); err != nil {
panic(err)
}
post1, err := NewPostFromIncomingPost(incomingPost1)
fmt.Printf("post1: %+v\nerr: %+v\n\n", post1, err)
var incomingPost2 map[string]string
if err := json.Unmarshal(jsonSource2, &incomingPost2); err != nil {
panic(err)
}
post2, err := NewPostFromIncomingPost(incomingPost2)
fmt.Printf("post2: %+v\nerr: %+v\n\n", post2, err)
}
$url = 'https://example.com/files/file-название файла (1).docx';
$url = preg_replace_callback(
'/[а-яА-Я\s]+/ui',
static function ($matches) {
return rawurlencode($matches[0]);
},
$url
);
// https://example.com/files/file-%D0%BD%D0%B0%D0%B7%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5%20%D1%84%D0%B0%D0%B9%D0%BB%D0%B0%20(1).docx
created() {
const onClickOutside = e => this.opened = this.$el.contains(e.target) && this.opened;
document.addEventListener('click', onClickOutside);
this.$on('hook:beforeDestroy', () => document.removeEventListener('click', onClickOutside));
},