Add function transformation data on JSON

This commit is contained in:
Archie Fox
2025-03-06 23:58:58 +03:00
parent b5941ea148
commit 26e125577d
7 changed files with 94 additions and 25 deletions

View File

@@ -1,34 +1,38 @@
package account
import (
"encoding/json"
"errors"
"fmt"
"math/rand/v2"
"net/url"
"time"
"github.com/fatih/color"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGIJKLMNOPQRSTUVWXYZ1234567890-*!?()#$%&")
type Account struct {
login string
password string
url string
}
type AccountWithTimeStamp struct {
createdAt time.Time
updatedAt time.Time
Account
Login string `json:"login"`
Password string `json:"password"`
Url string `json:"url"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// метод вывода пароля
func (acc Account) outputPassword() {
fmt.Println(acc.login, acc.password, acc.url)
func (acc *Account) OutputPassword() {
c := color.New(color.FgRed, color.Italic, color.Bold)
c.Printf("password: %v\n", acc.Password)
}
func (acc AccountWithTimeStamp) OutputPassword() {
fmt.Println(acc.login, acc.password, acc.url)
func (acc *Account) ToBytes() ([]byte, error) {
file, err := json.Marshal(acc)
if err != nil {
return nil, err
}
return file, nil
}
// метод генерации пароля
@@ -37,11 +41,11 @@ func (acc *Account) generatePassword(n int) {
for i := range res {
res[i] = letterRunes[rand.IntN(len(letterRunes))]
}
acc.password = string(res)
acc.Password = string(res)
}
// Создание аккаунта с таймстамп
func NewAccountWithTimeStamp(login, password, urlString string) (*AccountWithTimeStamp, error) {
func NewAccount(login, password, urlString string) (*Account, error) {
_, err := url.ParseRequestURI(urlString)
if err != nil {
return nil, errors.New("INVALID_URL")
@@ -49,14 +53,12 @@ func NewAccountWithTimeStamp(login, password, urlString string) (*AccountWithTim
if login == "" {
return nil, errors.New("INVALID_LOGIN")
}
newAcc := &AccountWithTimeStamp{
createdAt: time.Now(),
updatedAt: time.Now(),
Account: Account{
login: login,
password: password,
url: urlString,
},
newAcc := &Account{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Login: login,
Password: password,
Url: urlString,
}
if password == "" {
newAcc.generatePassword(18)