<commit_before>// Copyright 2018 Unknwon
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package models

import (
	"errors"
)

var ErrJSFileNotFound = errors.New("JS file does not exist")

type JSFileStatus int

const (
	JSFileStatusNone JSFileStatus = iota
	JSFileStatusGenerated
	JSFileStatusDistributed
	JSFileStatusRecycled
)

type JSFile struct {
	ID            int64
	PkgID         int64 `xorm:"INDEX"`
	Etag          string
	Status        JSFileStatus
	IsStale       bool // Indicates whether should be recycled
	NumExtraFiles int  // Indicates the number of extra JS files generated
}

func GetJSFile(pkgID int64, etag string) (*JSFile, error) {
	jsFile := new(JSFile)
	has, err := x.Where("pkg_id = ? AND etag = ?", pkgID, etag).Get(jsFile)
	if err != nil {
		return nil, err
	} else if !has {
		return nil, ErrJSFileNotFound
	}

	return jsFile, nil
}

func SaveJSFile(jsFile *JSFile) error {
	oldJSFile, err := GetJSFile(jsFile.PkgID, jsFile.Etag)
	if err != nil && err != ErrJSFileNotFound {
		return err
	}

	if err == ErrJSFileNotFound {
		_, err = x.InsertOne(jsFile)
		return err
	}

	jsFile.ID = oldJSFile.ID
	_, err = x.ID(jsFile.ID).AllCols().Update(jsFile)
	return err
}
<commit_msg>models: return error when JS file pkg_id is not set<commit_after>// Copyright 2018 Unknwon
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package models

import (
	"errors"
)

var ErrJSFileNotFound = errors.New("JS file does not exist")

type JSFileStatus int

const (
	JSFileStatusNone JSFileStatus = iota
	JSFileStatusGenerated
	JSFileStatusDistributed
	JSFileStatusRecycled
)

type JSFile struct {
	ID            int64
	PkgID         int64 `xorm:"INDEX"`
	Etag          string
	Status        JSFileStatus
	IsStale       bool // Indicates whether should be recycled
	NumExtraFiles int  // Indicates the number of extra JS files generated
}

func GetJSFile(pkgID int64, etag string) (*JSFile, error) {
	jsFile := new(JSFile)
	has, err := x.Where("pkg_id = ? AND etag = ?", pkgID, etag).Get(jsFile)
	if err != nil {
		return nil, err
	} else if !has {
		return nil, ErrJSFileNotFound
	}

	return jsFile, nil
}

func SaveJSFile(jsFile *JSFile) error {
	if jsFile.PkgID == 0 {
		return errors.New("PkgID is zero")
	}

	oldJSFile, err := GetJSFile(jsFile.PkgID, jsFile.Etag)
	if err != nil && err != ErrJSFileNotFound {
		return err
	}

	if err == ErrJSFileNotFound {
		_, err = x.InsertOne(jsFile)
		return err
	}

	jsFile.ID = oldJSFile.ID
	_, err = x.ID(jsFile.ID).AllCols().Update(jsFile)
	return err
}

{'fraction_non_alphanumeric': 0.09492774156984982, 'fraction_numerical': 0.00821762538962879, 'mean_word_length': 3.3960149439601492, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6672565', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1116, 'n_words': 398}