func putRoutingProviders(ds datastore.Datastore, k key.Key, newRecords []*dhtpb.Message_Peer) error { log.Event(context.Background(), "putRoutingProviders", &k) oldRecords, err := getRoutingProviders(ds, k) if err != nil { return err } mergedRecords := make(map[string]*dhtpb.Message_Peer) for _, provider := range oldRecords { mergedRecords[provider.GetId()] = provider // add original records } for _, provider := range newRecords { mergedRecords[provider.GetId()] = provider // overwrite old record if new exists } var protomsg dhtpb.Message protomsg.ProviderPeers = make([]*dhtpb.Message_Peer, 0, len(mergedRecords)) for _, provider := range mergedRecords { protomsg.ProviderPeers = append(protomsg.ProviderPeers, provider) } data, err := proto.Marshal(&protomsg) if err != nil { return err } return ds.Put(providerKey(k), data) }