<commit_before>package wysteria_client

import (
	wyc "github.com/voidshard/wysteria/common"
)

// Wrapper around wysteria/common Item object
type Item struct {
	conn *Client
	data *wyc.Item
}

// Get which ever Version object is considered the "published" version of this Item
//  Note that this may not be the latest, or even be set, even if child versions exist.
func (i *Item) PublishedVersion() (*Version, error) {
	ver, err := i.conn.middleware.PublishedVersion(i.data.Id)
	if err != nil {
		return nil, err
	}
	return &Version{
		conn: i.conn,
		data: ver,
	}, nil
}

// Return the ItemType of this item
func (i *Item) Type() string {
	return i.data.ItemType
}

// Delete this Item. Note all child Versions & their children will be deleted too.
func (i *Item) Delete() error {
	return i.conn.middleware.DeleteItem(i.data.Id)
}

// Link this item with a link described by 'name' to some other item.
func (i *Item) LinkTo(name string, other *Item) error {
	if i.Id() == other.Id() { // Prevent linking to oneself
		return nil
	}

	lnk := &wyc.Link{
		Name: name,
		Src:  i.data.Id,
		Dst:  other.data.Id,
	}
	_, err := i.conn.middleware.CreateLink(lnk)
	return err
}

// Find and return all linked items for which links exist that name this as the source.
// That is, this first finds all links for which the source Id is this Item's Id, then
// gets all matching Items.
// Since this would cause us to lose the link 'name' we return a map of link name -> []*Item
func (i *Item) Linked(opts ...SearchParam) (map[string][]*Item, error) {
	opts = append(opts, ChildOf(i.Id()))
	links, err := i.conn.Search(opts...).FindLinks()
	if err != nil {
		return nil, err
	}

	item_id_to_link := map[string]*Link{}
	ids := []*wyc.QueryDesc{}
	for _, link := range links {
		id := link.SourceId()

		if link.SourceId() == i.data.Id {
			id = link.DestinationId()
		}

		item_id_to_link[id] = link
		ids = append(ids, &wyc.QueryDesc{Id: id})
	}

	// Apply limit to query just to be safe
	items, err := i.conn.middleware.FindItems(int32(len(ids)), 0, ids)
	if err != nil {
		return nil, err
	}

	result := map[string][]*Item{}
	for _, ver := range items {
		lnk, ok := item_id_to_link[ver.Id]
		if !ok {
			continue
		}

		result_list, ok := result[lnk.Name()]
		if result_list == nil {
			result_list = []*Item{}
		}

		wrapped_item := &Item{
			conn: i.conn,
			data: ver,
		}

		result_list = append(result_list, wrapped_item)
		result[lnk.Name()] = result_list
	}
	return result, nil
}

// Return the variant string associated with this Item
func (i *Item) Variant() string {
	return i.data.Variant
}

// Get the facet value and a bool indicating if the value exists for the given key.
func (i *Item) Facet(key string) (string, bool) {
	val, ok := i.data.Facets[key]
	return val, ok
}

// Get the Id for this Item
func (i *Item) Id() string {
	return i.data.Id
}

// Set all the key:value pairs given on this Item's facets.
// Note that the server will ignore the setting of reserved facets.
func (i *Item) SetFacets(in map[string]string) error {<commit_msg>Add facets() call to item<commit_after>package wysteria_client

import (
	wyc "github.com/voidshard/wysteria/common"
)

// Wrapper around wysteria/common Item object
type Item struct {
	conn *Client
	data *wyc.Item
}

// Get which ever Version object is considered the "published" version of this Item
//  Note that this may not be the latest, or even be set, even if child versions exist.
func (i *Item) PublishedVersion() (*Version, error) {
	ver, err := i.conn.middleware.PublishedVersion(i.data.Id)
	if err != nil {
		return nil, err
	}
	return &Version{
		conn: i.conn,
		data: ver,
	}, nil
}

// Return the ItemType of this item
func (i *Item) Type() string {
	return i.data.ItemType
}

// Delete this Item. Note all child Versions & their children will be deleted too.
func (i *Item) Delete() error {
	return i.conn.middleware.DeleteItem(i.data.Id)
}

// Link this item with a link described by 'name' to some other item.
func (i *Item) LinkTo(name string, other *Item) error {
	if i.Id() == other.Id() { // Prevent linking to oneself
		return nil
	}

	lnk := &wyc.Link{
		Name: name,
		Src:  i.data.Id,
		Dst:  other.data.Id,
	}
	_, err := i.conn.middleware.CreateLink(lnk)
	return err
}

// Find and return all linked items for which links exist that name this as the source.
// That is, this first finds all links for which the source Id is this Item's Id, then
// gets all matching Items.
// Since this would cause us to lose the link 'name' we return a map of link name -> []*Item
func (i *Item) Linked(opts ...SearchParam) (map[string][]*Item, error) {
	opts = append(opts, ChildOf(i.Id()))
	links, err := i.conn.Search(opts...).FindLinks()
	if err != nil {
		return nil, err
	}

	item_id_to_link := map[string]*Link{}
	ids := []*wyc.QueryDesc{}
	for _, link := range links {
		id := link.SourceId()

		if link.SourceId() == i.data.Id {
			id = link.DestinationId()
		}

		item_id_to_link[id] = link
		ids = append(ids, &wyc.QueryDesc{Id: id})
	}

	// Apply limit to query just to be safe
	items, err := i.conn.middleware.FindItems(int32(len(ids)), 0, ids)
	if err != nil {
		return nil, err
	}

	result := map[string][]*Item{}
	for _, ver := range items {
		lnk, ok := item_id_to_link[ver.Id]
		if !ok {
			continue
		}

		result_list, ok := result[lnk.Name()]
		if result_list == nil {
			result_list = []*Item{}
		}

		wrapped_item := &Item{
			conn: i.conn,
			data: ver,
		}

		result_list = append(result_list, wrapped_item)
		result[lnk.Name()] = result_list
	}
	return result, nil
}

// Return the variant string associated with this Item
func (i *Item) Variant() string {
	return i.data.Variant
}

// Get the facet value and a bool indicating if the value exists for the given key.
func (i *Item) Facet(key string) (string, bool) {
	val, ok := i.data.Facets[key]
	return val, ok
}

// Get all facets
func (i *Item) Facets() map[string]string {
	return i.data.Facets
}

// Get the Id for this Item
func (i *Item) Id() string {
	return i.data.Id
}

// Set all the key:value pairs given on this Item's facets.
// Note that the server will ignore the setting of reserved facets.
func (i *Item) SetFacets(in map[string]string) error {
{'fraction_non_alphanumeric': 0.13138803804610671, 'fraction_numerical': 0.0009672738997259391, 'mean_word_length': 3.40625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6459888', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2146, 'n_words': 766}