<commit_before>package libstns

import (
	"encoding/json"
	"errors"
	"strconv"
	"strings"

	"github.com/STNS/STNS/stns"
	"github.com/STNS/libnss_stns/settings"
)

func convertV1toV3Format(body []byte) (*ResponseFormat, error) {
	var attr stns.Attributes
	err := json.Unmarshal(body, &attr)

	if err != nil {
		return nil, err
	}

	return &ResponseFormat{
		0,
		attr,
	}, nil
}

func convertV2toV3Format(body []byte) (*ResponseFormat, error) {
	var res v2ResponseFormat
	err := json.Unmarshal(body, &res)
	if err != nil {
		return nil, err
	}

	return &ResponseFormat{
		res.MetaData.MinID,
		res.Items,
	}, nil

}

func convertV3Format(b []byte, path string, minID string, config *Config) (*ResponseFormat, error) {
	var err error
	attr := stns.Attributes{}
	sp := strings.Split(strings.TrimLeft(path, "/"), "/")

	if len(sp) < 2 {
		return nil, errors.New("parse error: path specification is insufficient")
	}

	switch sp[0] {
	case "user":
		if sp[1] == "list" {
			users := v3Users{}
			err = json.Unmarshal(b, &users)

			if err != nil {
				return nil, err
			}
			for _, u := range users {
				if u.Name != "" && u.ID+config.UIDShift > settings.MIN_LIMIT_ID {
					attr[u.Name] = &stns.Attribute{
						ID: u.ID + config.UIDShift,
						User: &stns.User{
							Password:  <PASSWORD>,
							GroupID:   u.GroupID + config.GIDShift,
							Directory: u.Directory,
							Shell:     u.Shell,
							Gecos:     u.Gecos,
							Keys:      u.Keys,
						},
					}
				}
			}
		} else {
			user := v3User{}
			err = json.Unmarshal(b, &user)
			if err != nil {
				return nil, err
			}

			if user.Name != "" && user.ID+config.UIDShift > settings.MIN_LIMIT_ID {
				attr[user.Name] = &stns.Attribute{
					ID: user.ID + config.UIDShift,
					User: &stns.User{
						Password:  <PASSWORD>,
						GroupID:   user.GroupID + config.GIDShift,
						Directory: user.Directory,
						Shell:     user.Shell,
						Gecos:     user.Gecos,
						Keys:      user.Keys,
					},
				}
			}
		}
	case "group":
		if sp[1] == "list" {
			groups := v3Groups{}
			err = json.Unmarshal(b, &groups)
			if err != nil {
				return nil, err
			}
			for _, g := range groups {
				if g.ID+config.GIDShift > settings.MIN_LIMIT_ID {
					attr[g.Name] = &stns.Attribute{
						ID: g.ID + config.GIDShift,
						Group: &stns.Group{
							Users: g.Users,
						},
					}
				}
			}
		} else {
			group := v3Group{}
			err = json.Unmarshal(b, &group)
			if err != nil {
				return nil, err
			}

			if group.ID+config.GIDShift > settings.MIN_LIMIT_ID {
				attr[group.Name] = &stns.Attribute{
					ID: group.ID + config.GIDShift,
					Group: &stns.Group{
						Users: group.Users,
					},
				}
			}
		}
	case "sudo":
		user := v3Sudo{}
		err = json.Unmarshal(b, &user)
		if err != nil {
			return nil, err
		}

		if user.Name != "" && user.Password != "" {
			attr[user.Name] = &stns.Attribute{
				ID: 0,
				User: &stns.User{
					Password: user.Password,
				},
			}
		}<commit_msg>fix group id<commit_after>package libstns

import (
	"encoding/json"
	"errors"
	"strconv"
	"strings"

	"github.com/STNS/STNS/stns"
	"github.com/STNS/libnss_stns/settings"
)

func convertV1toV3Format(body []byte) (*ResponseFormat, error) {
	var attr stns.Attributes
	err := json.Unmarshal(body, &attr)

	if err != nil {
		return nil, err
	}

	return &ResponseFormat{
		0,
		attr,
	}, nil
}

func convertV2toV3Format(body []byte) (*ResponseFormat, error) {
	var res v2ResponseFormat
	err := json.Unmarshal(body, &res)
	if err != nil {
		return nil, err
	}

	return &ResponseFormat{
		res.MetaData.MinID,
		res.Items,
	}, nil

}

func convertV3Format(b []byte, path string, minID string, config *Config) (*ResponseFormat, error) {
	var err error
	attr := stns.Attributes{}
	sp := strings.Split(strings.TrimLeft(path, "/"), "/")

	if len(sp) < 2 {
		return nil, errors.New("parse error: path specification is insufficient")
	}

	switch sp[0] {
	case "user":
		if sp[1] == "list" {
			users := v3Users{}
			err = json.Unmarshal(b, &users)

			if err != nil {
				return nil, err
			}
			for _, u := range users {
				if u.Name != "" && u.ID+config.UIDShift > settings.MIN_LIMIT_ID {
					tmpUser := &stns.User{
						Password:  u.Password,
						Directory: u.Directory,
						Shell:     u.Shell,
						Gecos:     u.Gecos,
						Keys:      u.Keys,
					}

					if u.GroupID+config.GIDShift > settings.MIN_LIMIT_ID {
						tmpUser.GroupID = u.GroupID + config.GIDShift
					}

					attr[u.Name] = &stns.Attribute{
						ID:   u.ID + config.UIDShift,
						User: tmpUser,
					}
				}
			}
		} else {
			u := v3User{}
			err = json.Unmarshal(b, &u)
			if err != nil {
				return nil, err
			}

			if u.Name != "" && u.ID+config.UIDShift > settings.MIN_LIMIT_ID {
				tmpUser := &stns.User{
					Password:  <PASSWORD>,
					Directory: u.Directory,
					Shell:     u.Shell,
					Gecos:     u.Gecos,
					Keys:      u.Keys,
				}

				if u.GroupID+config.GIDShift > settings.MIN_LIMIT_ID {
					tmpUser.GroupID = u.GroupID + config.GIDShift
				}

				attr[u.Name] = &stns.Attribute{
					ID:   u.ID + config.UIDShift,
					User: tmpUser,
				}
			}
		}
	case "group":
		if sp[1] == "list" {
			groups := v3Groups{}
			err = json.Unmarshal(b, &groups)
			if err != nil {
				return nil, err
			}
			for _, g := range groups {
				if g.ID+config.GIDShift > settings.MIN_LIMIT_ID {
					attr[g.Name] = &stns.Attribute{
						ID: g.ID + config.GIDShift,
						Group: &stns.Group{
							Users: g.Users,
						},
					}
				}
			}
		} else {
			g := v3Group{}
			err = json.Unmarshal(b, &g)
			if err != nil {
				return nil, err
			}

			if g.ID+config.GIDShift > settings.MIN_LIMIT_ID {
				attr[g.Name] = &stns.Attribute{
					ID: g.ID + config.GIDShift,
					Group: &stns.Group{
						Users: g.Users,
					},
				}
			}
		}
	case "sudo":
		u := v3Sudo{}
		err = json.Unmarshal(b, &u)
		if err != nil {
			return nil, err
		}

		if u.Name != "" && u.Password != "" {
			attr[u.Name] = &stns.Attribute{
				ID: 0,
				User: &stns.User{
					Password: <PASSWORD>,
				},
			}
		}
{'fraction_non_alphanumeric': 0.1518020262414881, 'fraction_numerical': 0.005646902507889055, 'mean_word_length': 2.3604910714285716, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 17, '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': '3919723', 'n_tokens_mistral': 2880, 'n_tokens_neox': 2295, 'n_words': 500}