Skip to content

Commit

Permalink
search: share empty interface for generic Entity collection
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 3, 2025
1 parent 69cdfdf commit d10a44e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
10 changes: 7 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ func main() {
addSearchRoutes(logger, router, searcher)
addValuesRoutes(logger, router, searcher)

var genericEntities []pubsearch.Entity[pubsearch.Value]

genericSDNs := generalizeOFACSDNs(searcher.SDNs, searcher.Addresses)
v2SearchService := searchv2.NewService[ofac.SDN](logger, genericSDNs)
genericEntities = append(genericEntities, genericSDNs...)

v2SearchService := searchv2.NewService(logger, genericEntities)
addSearchV2Routes(logger, router, v2SearchService)

// Setup our web UI to be served as well
Expand Down Expand Up @@ -271,8 +275,8 @@ func handleDownloadStats(updates chan *DownloadStats, handle func(stats *Downloa
}
}

func generalizeOFACSDNs(input []*SDN, ofacAddresses []*Address) []pubsearch.Entity[ofac.SDN] {
var out []pubsearch.Entity[ofac.SDN]
func generalizeOFACSDNs(input []*SDN, ofacAddresses []*Address) []pubsearch.Entity[pubsearch.Value] {
var out []pubsearch.Entity[pubsearch.Value]
for _, sdn := range input {
if sdn.SDN == nil {
continue
Expand Down
20 changes: 12 additions & 8 deletions internal/search/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,39 @@ import (
)

type Service interface {
Search(ctx context.Context)
Search(ctx context.Context, query search.Entity[search.Value]) ([]SearchedEntity[search.Value], error)
}

func NewService[T any](logger log.Logger, entities []search.Entity[T]) Service {
func NewService(logger log.Logger, entities []search.Entity[search.Value]) Service {

fmt.Printf("v2search NewService(%d entities)\n", len(entities)) //nolint:forbidigo
fmt.Printf("v2search NewService(%d entity types)\n", len(entities)) //nolint:forbidigo

return &service[T]{
return &service{
logger: logger,
entities: entities,
}
}

type service[T any] struct {
type service struct {
logger log.Logger
entities []search.Entity[T]
entities []search.Entity[search.Value]
}

func (s *service[T]) Search(ctx context.Context) {
func (s *service) Search(ctx context.Context, query search.Entity[search.Value]) ([]SearchedEntity[search.Value], error) {
for _, entity := range s.entities {
if len(entity.Addresses) > 0 {
bs, _ := json.Marshal(entity)
fmt.Printf("\n\n %s \n", string(bs)) //nolint:forbidigo
return
return nil, nil
}
}

// TODO(adam): use SearchedEntity
// type SearchedEntity[T any] struct {
// search.Entity[T]
// Match float64 `json:"match"`

var out []SearchedEntity[search.Value]

return out, nil
}
4 changes: 2 additions & 2 deletions pkg/ofac/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func extractCountry(remark string) string {
return ""
}

func ToEntity(sdn SDN, addresses []Address, comments []SDNComments) search.Entity[SDN] {
out := search.Entity[SDN]{
func ToEntity(sdn SDN, addresses []Address, comments []SDNComments) search.Entity[search.Value] {
out := search.Entity[search.Value]{
Name: sdn.SDNName,
Source: search.SourceUSOFAC,
SourceData: sdn,
Expand Down
4 changes: 3 additions & 1 deletion pkg/ofac/mapper_person_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func TestMapper__Person(t *testing.T) {
require.Nil(t, e.Aircraft)
require.Nil(t, e.Vessel)

require.Equal(t, "15102", e.SourceData.EntityID)
sourceData, ok := e.SourceData.(SDN)
require.True(t, ok)
require.Equal(t, "15102", sourceData.EntityID)
}

func TestMapper__CompletePerson(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions pkg/ofac/mapper_vehicles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func TestMapper__Vessel(t *testing.T) {
require.Equal(t, "9187629", e.Vessel.IMONumber)
require.Equal(t, "572469210", e.Vessel.MMSI)

require.Equal(t, "15036", e.SourceData.EntityID)
sourceData, ok := e.SourceData.(SDN)
require.True(t, ok)
require.Equal(t, "15036", sourceData.EntityID)
}

func TestMapper__Aircraft(t *testing.T) {
Expand Down Expand Up @@ -69,7 +71,9 @@ func TestMapper__Aircraft(t *testing.T) {
require.Equal(t, "Airbus A321-131", e.Aircraft.Model)
require.Equal(t, "550", e.Aircraft.SerialNumber)

require.Equal(t, "18158", e.SourceData.EntityID)
sourceData, ok := e.SourceData.(SDN)
require.True(t, ok)
require.Equal(t, "18158", sourceData.EntityID)
}

func TestMapper__CompleteVessel(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/search/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"time"
)

type Entity[T any] struct {
type Value interface{}

type Entity[T Value] struct {
Name string `json:"name"`
Type EntityType `json:"entityType"`
Source SourceList `json:"sourceList"`
Expand Down

0 comments on commit d10a44e

Please sign in to comment.