示例#1
0
文件: datasets.go 项目: js-arias/jdh
// Add adds a new dataset to the database.
func (d *datasets) add(set *jdh.Dataset) (string, error) {
	id := strconv.FormatUint(d.next, 10)
	set.Id = id
	if err := d.validate(set); err != nil {
		return "", err
	}
	d.addSet(set)
	d.next++
	d.changed = true
	return id, nil
}
示例#2
0
文件: datasets.go 项目: js-arias/jdh
// Validates that a dataset is valid in the database. When posible canonical
// values will be set. If the dataset is invalid it will return an error.
func (d *datasets) validate(set *jdh.Dataset) error {
	set.Id = strings.TrimSpace(set.Id)
	set.Title = strings.Join(strings.Fields(set.Title), " ")
	if (len(set.Id) == 0) || (len(set.Title) == 0) {
		return errors.New("dataset without identification")
	}
	if _, ok := d.ids[set.Id]; ok {
		return fmt.Errorf("dataset id %s already in use", set.Id)
	}
	set.Citation = strings.Join(strings.Fields(set.Citation), " ")
	set.License = strings.Join(strings.Fields(set.License), " ")
	if u, err := url.Parse(set.Url); err != nil {
		set.Url = ""
	} else {
		set.Url = u.String()
	}
	ext := set.Extern
	set.Extern = nil
	for _, e := range ext {
		serv, id, err := jdh.ParseExtern(e)
		if err != nil {
			continue
		}
		if len(id) == 0 {
			continue
		}
		add := true
		for _, ex := range set.Extern {
			if strings.HasPrefix(ex, serv) {
				add = false
				break
			}
		}
		if !add {
			continue
		}
		if _, ok := d.ids[e]; !ok {
			set.Extern = append(set.Extern, e)
		}
	}
	return nil
}