<commit_before>// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package dep

import (
	"bytes"
	"fmt"
	"io"
	"reflect"
	"sort"

	"github.com/golang/dep/gps"
	"github.com/golang/dep/internal"
	"github.com/pelletier/go-toml"
	"github.com/pkg/errors"
)

const ManifestName = "Gopkg.toml"

type Manifest struct {
	Dependencies gps.ProjectConstraints
	Ovr          gps.ProjectConstraints
	Ignored      []string
	Required     []string
}

type rawManifest struct {
	Dependencies []rawProject `toml:"dependencies,omitempty"`
	Overrides    []rawProject `toml:"overrides,omitempty"`
	Ignored      []string     `toml:"ignored,omitempty"`
	Required     []string     `toml:"required,omitempty"`
}

type rawProject struct {
	Name     string `toml:"name"`
	Branch   string `toml:"branch,omitempty"`
	Revision string `toml:"revision,omitempty"`
	Version  string `toml:"version,omitempty"`
	Source   string `toml:"source,omitempty"`
}

func validateManifest(s string) ([]error, error) {
	var errs []error
	// Load the TomlTree from string
	tree, err := toml.Load(s)
	if err != nil {
		return errs, errors.Wrap(err, "Unable to load TomlTree from string")
	}
	// Convert tree to a map
	manifest := tree.ToMap()

	// Look for unknown fields and collect errors
	for prop, val := range manifest {
		switch prop {
		case "metadata":
			// Check if metadata is of Map type
			if reflect.TypeOf(val).Kind() != reflect.Map {
				errs = append(errs, errors.New("metadata should be a TOML table"))
			}
		case "dependencies", "overrides":
			// Invalid if type assertion fails. Not a TOML array of tables.
			if rawProj, ok := val.([]interface{}); ok {
				// Iterate through each array of tables
				for _, v := range rawProj {
					// Check the individual field's key to be valid
					for key, value := range v.(map[string]interface{}) {
						// Check if the key is valid
						switch key {
						case "name", "branch", "revision", "version", "source":
							// valid key
						case "metadata":
							// Check if metadata is of Map type
							if reflect.TypeOf(value).Kind() != reflect.Map {
								errs = append(errs, fmt.Errorf("metadata in %q should be a TOML table", prop))
							}
						default:
							// unknown/invalid key
							errs = append(errs, fmt.Errorf("Invalid key %q in %q", key, prop))
						}
					}
				}
			} else {
				errs = append(errs, fmt.Errorf("%v should be a TOML array of tables", prop))
			}
		case "ignored", "required":
		default:
			errs = append(errs, fmt.Errorf("Unknown field in manifest: %v", prop))
		}
	}

	return errs, nil
}

func readManifest(r io.Reader) (*Manifest, error) {
	buf := &bytes.Buffer{}
	_, err := buf.ReadFrom(r)
	if err != nil {
		return nil, errors.Wrap(err, "Unable to read byte stream")
	}

	// Validate manifest and log warnings
	errs, err := validateManifest(buf.String())
	if err != nil {
		return nil, errors.Wrap(err, "Manifest validation failed")
	}
	for _, e := range errs {
		internal.Logf("WARNING: %v", e)
	}

	raw := rawManifest{}
	err = toml.Unmarshal(buf.Bytes(), &raw)
	if err != nil {
		return nil, errors.Wrap(err, "Unable to parse the manifest as TOML")
	}

	return fromRawManifest(raw)
}

func fromRawManifest(raw rawManifest) (*Manifest, error) {
	m := &Manifest{
		Dependencies: make(gps.ProjectConstraints, len(raw.Dependencies)),<commit_msg>Comment out call to global internal logger<commit_after>// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package dep

import (
	"bytes"
	"fmt"
	"io"
	"reflect"
	"sort"

	"github.com/golang/dep/gps"
	"github.com/pelletier/go-toml"
	"github.com/pkg/errors"
)

const ManifestName = "Gopkg.toml"

type Manifest struct {
	Dependencies gps.ProjectConstraints
	Ovr          gps.ProjectConstraints
	Ignored      []string
	Required     []string
}

type rawManifest struct {
	Dependencies []rawProject `toml:"dependencies,omitempty"`
	Overrides    []rawProject `toml:"overrides,omitempty"`
	Ignored      []string     `toml:"ignored,omitempty"`
	Required     []string     `toml:"required,omitempty"`
}

type rawProject struct {
	Name     string `toml:"name"`
	Branch   string `toml:"branch,omitempty"`
	Revision string `toml:"revision,omitempty"`
	Version  string `toml:"version,omitempty"`
	Source   string `toml:"source,omitempty"`
}

func validateManifest(s string) ([]error, error) {
	var errs []error
	// Load the TomlTree from string
	tree, err := toml.Load(s)
	if err != nil {
		return errs, errors.Wrap(err, "Unable to load TomlTree from string")
	}
	// Convert tree to a map
	manifest := tree.ToMap()

	// Look for unknown fields and collect errors
	for prop, val := range manifest {
		switch prop {
		case "metadata":
			// Check if metadata is of Map type
			if reflect.TypeOf(val).Kind() != reflect.Map {
				errs = append(errs, errors.New("metadata should be a TOML table"))
			}
		case "dependencies", "overrides":
			// Invalid if type assertion fails. Not a TOML array of tables.
			if rawProj, ok := val.([]interface{}); ok {
				// Iterate through each array of tables
				for _, v := range rawProj {
					// Check the individual field's key to be valid
					for key, value := range v.(map[string]interface{}) {
						// Check if the key is valid
						switch key {
						case "name", "branch", "revision", "version", "source":
							// valid key
						case "metadata":
							// Check if metadata is of Map type
							if reflect.TypeOf(value).Kind() != reflect.Map {
								errs = append(errs, fmt.Errorf("metadata in %q should be a TOML table", prop))
							}
						default:
							// unknown/invalid key
							errs = append(errs, fmt.Errorf("Invalid key %q in %q", key, prop))
						}
					}
				}
			} else {
				errs = append(errs, fmt.Errorf("%v should be a TOML array of tables", prop))
			}
		case "ignored", "required":
		default:
			errs = append(errs, fmt.Errorf("Unknown field in manifest: %v", prop))
		}
	}

	return errs, nil
}

func readManifest(r io.Reader) (*Manifest, error) {
	buf := &bytes.Buffer{}
	_, err := buf.ReadFrom(r)
	if err != nil {
		return nil, errors.Wrap(err, "Unable to read byte stream")
	}

	// Validate manifest and log warnings
	errs, err := validateManifest(buf.String())
	if err != nil {
		return nil, errors.Wrap(err, "Manifest validation failed")
	}
	//for _, e := range errs {
	// FIXME(sdboyer) need to adapt this to use an injected *Loggers
	//internal.Logf("WARNING: %v", e)
	//}

	raw := rawManifest{}
	err = toml.Unmarshal(buf.Bytes(), &raw)
	if err != nil {
		return nil, errors.Wrap(err, "Unable to parse the manifest as TOML")
	}

	return fromRawManifest(raw)
}

func fromRawManifest(raw rawManifest) (*Manifest, error) {
	m := &Manifest{
		Dependencies: make(gps.ProjectConstraints, len(raw.Dependencies)),
{'fraction_non_alphanumeric': 0.1314873876485938, 'fraction_numerical': 0.0011597564511452595, 'mean_word_length': 3.2850931677018633, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, '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': '5846095', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2235, 'n_words': 714}