125 lines
3.0 KiB
Go
125 lines
3.0 KiB
Go
package deesee
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/eslider/superherohub/pkg/people"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// superPower is a type for superpowers.
|
|
type superPower string
|
|
|
|
// List of allowed superpowers.
|
|
const (
|
|
strength superPower = "strength"
|
|
speed superPower = "speed"
|
|
flight superPower = "flight"
|
|
invulnerability superPower = "invulnerability"
|
|
healing superPower = "healing"
|
|
)
|
|
|
|
// To keep the system as simple as possible,
|
|
// DeeSee has agreed to only accept superheroes with the following superpowers.
|
|
var allowedSuperPowers = [5]superPower{strength, speed, flight, invulnerability, healing}
|
|
|
|
// Superhero DeeSee person type.
|
|
type Superhero people.Person
|
|
|
|
// IsAcceptable checks if the superhero is acceptable.
|
|
func (s *Superhero) IsAcceptable() (r bool) {
|
|
// Check if the person has superpowers.
|
|
if *s.SuperPowers == nil {
|
|
return false
|
|
}
|
|
// Check if the person has at least one acceptable superpower.
|
|
for _, p := range *s.SuperPowers {
|
|
if IsAcceptable(p) {
|
|
r = true
|
|
break
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// IsSuperHero checks if the superhero is acceptable.
|
|
func (s *Superhero) IsSuperHero() bool {
|
|
return s.IsAcceptable()
|
|
}
|
|
|
|
// Has checks if the superhero has the given power.
|
|
func (s *Superhero) Has(power string) bool {
|
|
return s.SuperPowers.Contains(power)
|
|
}
|
|
|
|
// IsAcceptable checks if the given power is allowed.
|
|
func IsAcceptable(power string) bool {
|
|
for _, p := range allowedSuperPowers {
|
|
if string(p) == power {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Load JSON file and decode to `[]*Person` list
|
|
func Load(path string) (l []*Superhero, err error) {
|
|
var f *os.File
|
|
if f, err = os.Open(path); err != nil {
|
|
return
|
|
}
|
|
// Parse the request body
|
|
err = json.NewDecoder(f).Decode(&l)
|
|
return
|
|
}
|
|
|
|
// EncryptHerosIdentities encrypts all persons identities with DeeSee encryption algorithm
|
|
func EncryptHerosIdentities(heros []*Superhero, key int) (r []*Superhero) {
|
|
for _, person := range heros {
|
|
p := &Superhero{
|
|
Name: person.Name,
|
|
Identity: EncryptIdentity(person.Identity, key),
|
|
SuperPowers: person.SuperPowers,
|
|
Birthday: person.Birthday,
|
|
}
|
|
r = append(r, p)
|
|
}
|
|
return
|
|
}
|
|
|
|
// EncryptIdentity encrypts person identity with DeeSee encryption algorithm
|
|
func EncryptIdentity(identity *people.Identity, key int) *people.Identity {
|
|
return &people.Identity{
|
|
FirstName: Encrypt(identity.FirstName, key),
|
|
LastName: Encrypt(identity.LastName, key),
|
|
}
|
|
}
|
|
|
|
// SearchByPowers from persons list
|
|
func SearchByPowers(heros []*Superhero, powers []string) (r []*Superhero) {
|
|
for _, hero := range heros {
|
|
for _, power := range powers {
|
|
if hero.IsSuperHero() && hero.Has(power) {
|
|
r = append(r, hero)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// NewSuperhero creates a new superhero
|
|
func NewSuperhero() *Superhero {
|
|
return &Superhero{}
|
|
}
|
|
|
|
// FindByName from superheros list
|
|
func FindByName(heros []*Superhero, name string) *Superhero {
|
|
for _, hero := range heros {
|
|
if strings.EqualFold(name, hero.Name) {
|
|
return hero
|
|
}
|
|
}
|
|
return nil
|
|
}
|