// request - get and load bundle by query func (storage *Record) request(query string) (*mapper.Entry, error) { TLD := detachTLD(query) if TLD == "" { return nil, nil } template := storage.Bundle.EntryByTLD(TLD) if template == nil { return nil, nil } var err error entry := new(mapper.Entry) entry.Fields = make(map[string]mapper.Field) baseField := make(map[string][]string) relatedField := make(map[string]map[string][]string) // Loads fields with constant values for index, field := range template.Fields { if len(field.Value) != 0 && len(field.Related) == 0 && len(field.RelatedBy) == 0 && len(field.RelatedTo) == 0 { entry.Fields[index] = mapper.Field{ Key: field.Key, Value: field.Value, Format: field.Format, Multiple: field.Multiple, Hide: field.Hide, } } } // Loads base fields (non related) for index, field := range template.Fields { // Detect base field if len(field.Value) == 0 && len(field.Related) != 0 && (len(field.RelatedBy) == 0 || len(field.RelatedTo) == 0) { // if baseField not loaded, do it if len(baseField) == 0 { baseField, err = storage.dataStore.Search(field.Related, query) if err != nil { return nil, err } if len(baseField) == 0 { return nil, nil } } value := []string{} // collects all values into value for _, item := range field.Name { if result, ok := baseField[item]; ok { value = append(value, result...) } } entry.Fields[index] = mapper.Field{ Key: field.Key, Value: value, Format: field.Format, Multiple: field.Multiple, Hide: field.Hide, } } } // Loads related records for index, field := range template.Fields { // Detect related fields if len(field.RelatedBy) != 0 && len(field.RelatedTo) != 0 && len(field.Related) != 0 { value := []string{} queryRelated := strings.Join(baseField[field.Related], "") // if non-related field from specified type/table if len(field.Value) != 0 { queryRelated = field.Value[0] } // if field not cached, do it if _, ok := relatedField[field.Related]; ok == false { if field.Multiple { relatedField[field.Related], err = storage.dataStore.SearchMultiple( field.RelatedTo, field.RelatedBy, queryRelated, ) if err != nil { return nil, err } } else { relatedField[field.Related], err = storage.dataStore.SearchRelated( field.RelatedTo, field.RelatedBy, queryRelated, ) if err != nil { return nil, err } } } // collects all values into value for _, item := range field.Name { if result, ok := relatedField[field.Related][item]; ok { value = append(value, result...) } } entry.Fields[index] = mapper.Field{ Key: field.Key, Value: value, Format: field.Format, Multiple: field.Multiple, Hide: field.Hide, } } } return entry, nil }
func TestStorage(t *testing.T) { conf := config.New() flag.Parse() bundle := make(mapper.Bundle, 1) storage := New(conf, bundle) answer, ok := storage.Search("") if ok != false { t.Error("Expected ok is false, got", ok) } if answer != "not found\n" { t.Error("Expected answer is not found, got", answer) } answer, ok = storage.Search("aaa") if ok != false { t.Error("Expected ok is false, got", ok) } if answer != "not found\n" { t.Error("Expected answer is not found, got", answer) } entry := new(mapper.Entry) entry.TLDs = []string{"com"} entry.Fields = make(map[string]mapper.Field) entry.Fields["01"] = mapper.Field{ Key: "Domain Name: ", Name: []string{"name"}, Format: "{idn}", Related: "name", } entry.Fields["02"] = mapper.Field{ Key: "Registry Domain ID: ", Name: []string{"domainId"}, Hide: true, Related: "name", } entry.Fields["03"] = mapper.Field{ Key: "Registrar WHOIS Server: ", Value: []string{"whois.markmonitor.com"}, } entry.Fields["05"] = mapper.Field{ Key: "Updated Date: ", Name: []string{"updatedDate"}, Format: "{date}", Related: "name", } entry.Fields["12"] = mapper.Field{ Key: "Domain Status: ", Name: []string{"domainStatus"}, Multiple: true, Related: "name", } entry.Fields["13"] = mapper.Field{ Key: "Registry Registrant ID: ", Name: []string{"handle"}, Hide: true, Related: "ownerHandle", RelatedBy: "handle", RelatedTo: "customer", } entry.Fields["21"] = mapper.Field{ Key: "Registrant Phone: ", Name: []string{ "phone.countryCode", "phone.areaCode", "phone.subscriberNumber", }, Format: "{string}.{string}{string}", Related: "ownerHandle", RelatedBy: "handle", RelatedTo: "customer", } entry.Fields["52"] = mapper.Field{ Key: "Name Server: ", Name: []string{"name"}, Multiple: true, Related: "nsgroupId", RelatedBy: "nsgroupId", RelatedTo: "nameserver", } entry.Fields["55"] = mapper.Field{ Key: "", Value: []string{"1"}, Name: []string{"updatedDate"}, Format: ">>> Last update of WHOIS database: {date} <<<", Related: "whois", RelatedBy: "id", RelatedTo: "whois", } bundle = append(bundle, *entry) storage = New(conf, bundle) answer, ok = storage.Search("google.com") if ok != true { t.Error("Expected ok is true, got", ok) } expected := `Domain Name: google.com Registrar WHOIS Server: whois.markmonitor.com Updated Date: 2014-05-19T04:00:17Z Domain Status: clientUpdateProhibited Domain Status: clientTransferProhibited Domain Status: clientDeleteProhibited Registry Registrant ID: MMR-2383 Registrant Phone: +1.6502530000 Name Server: ns1.google.com Name Server: ns2.google.com Name Server: ns3.google.com Name Server: ns4.google.com >>> Last update of WHOIS database: 2014-06-01T11:00:07Z <<< ` if answer != expected { t.Error("Expected answer:\n", expected, "\n, got:\n", answer) } conf.Storage.StorageType = "mysql" storage = New(conf, bundle) answer, ok = storage.Search("mmm") if ok != false { t.Error("Expected ok is false, got", ok) } if answer != "not found\n" { t.Error("Expected answer is not found, got", answer) } conf.Storage.StorageType = "elasticsearch" storage = New(conf, bundle) answer, ok = storage.Search("eee") if ok != false { t.Error("Expected ok is false, got", ok) } if answer != "not found\n" { t.Error("Expected answer is not found, got", answer) } }