Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index out of range exception when creating ColumnModification with identical column/key names #1

Closed
HighQualityWaschbaer opened this issue Aug 29, 2022 · 1 comment

Comments

@HighQualityWaschbaer
Copy link
Collaborator

When creating a column modification where the original column name and the target name (key) are equal, an index out of range exception occurs when trying to read the resulting dbf rows.

2022/08/29 15:32:51 Importing technicians...
2022/08/29 15:32:51 [importer] [0] interruption: runtime error: index out of range [-1]
exit status 0xc000013a
log.Print("Importing technicians...")

dbf, err := dbase.Open(filepath.Join(manager.config.DBPath, "DATABASE.dbf"), &dbase.Win1250Converter{})
	if err != nil {
		log.Panicf(err.Error())
	}
	defer dbf.Close()

	dbf.SetTrimspacesDefault(true)
	dbf.SetColumnModification(dbf.ColumnPos("TECHNR"), true, "ID", nil)
	dbf.SetColumnModification(dbf.ColumnPos("Name"), true, "Name", nil) //crashes because of this line

	for !dbf.EOF() {
		row, err := dbf.Row()
		if err != nil {
			log.Panicf(err.Error())
		}
		dbf.Skip(1)

		if row.Deleted {
			continue
		}
...
}

The original column names in the dbf table:
The original column names

The target struct for reference:

type Technician struct {
	ID        uint64 `gorm:"primaryKey" json:"ID"`
	Name      string `gorm:"not null" json:"Name"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

Reading the rows works perfectly fine when removing the line, but this action should not cause any problems...

@Valentin-Kaiser
Copy link
Owner

The error does not occur because the name is identical, but because there is no column with the name "Name". dbf.ColumnPos("Name") returns -1 and thus the index is out of range. I will, for ease of use, include a check to validate that the index is valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants