Beispiel #1
0
func (t *Translate) Record(dr *DataRecord) error {
	if t.Session == nil {
		return nil
	}
	var (
		tm session.Template
		tr TemplateRecord
		ok bool
	)
	if tm, ok = t.Session.GetTemplate(dr.TemplateID); !ok {
		if debug {
			debugLog.Printf("no template for id=%d, can't translate field\n", dr.TemplateID)
		}
		return nil
	}
	if tr, ok = tm.(TemplateRecord); !ok {
		return nil
	}
	if tr.Fields == nil {
		if debug {
			debugLog.Printf("no fields in template id=%d, can't translate\n", dr.TemplateID)
		}
		return nil
	}

	if debug {
		debugLog.Printf("translating %d/%d fields\n", len(dr.Fields), len(tr.Fields))
	}

	for i, field := range tr.Fields {
		if i > len(dr.Fields) {
			break
		}
		f := &dr.Fields[i]
		f.Translated = &TranslatedField{}
		f.Translated.EnterpriseNumber = field.EnterpriseNumber
		f.Translated.InformationElementID = field.InformationElementID

		if element, ok := t.Translate.Key(translate.Key{field.EnterpriseNumber, field.InformationElementID}); ok {
			f.Translated.Name = element.Name
			f.Translated.Value = translate.Bytes(dr.Fields[i].Bytes, element.Type)
			if debug {
				debugLog.Printf("translated {%d, %d} to %s, %v\n", field.EnterpriseNumber, field.InformationElementID, f.Translated.Name, f.Translated.Value)
			}
		} else if debug {
			debugLog.Printf("no translator element for {%d, %d}\n", field.EnterpriseNumber, field.InformationElementID)
		}
	}

	return nil
}
Beispiel #2
0
func (t *Translate) Record(dr *DataRecord) error {
	if t.Session == nil {
		if debug {
			debugLog.Println("no session, can't translate field")
		}
		return nil
	}
	var (
		tm session.Template
		tr TemplateRecord
		ok bool
	)
	if tm, ok = t.Session.GetTemplate(dr.TemplateID); !ok {
		if debug {
			debugLog.Printf("no template for id=%d, can't translate field\n", dr.TemplateID)
		}
		return nil
	}
	if tr, ok = tm.(TemplateRecord); !ok {
		return nil
	}
	if tr.Fields == nil {
		if debug {
			debugLog.Printf("no fields in template id=%d, can't translate\n", dr.TemplateID)
		}
		return nil
	}

	if debug {
		debugLog.Printf("translating %d/%d fields\n", len(dr.Fields), len(tr.Fields))
	}

	for i, field := range tr.Fields {
		if i >= len(dr.Fields) {
			break
		}
		f := &dr.Fields[i]
		f.Translated = &TranslatedField{}
		f.Translated.Type = field.Type

		if element, ok := t.Translate.Key(translate.Key{0, field.Type}); ok {
			f.Translated.Name = element.Name
			f.Translated.Value = translate.Bytes(dr.Fields[i].Bytes, element.Type)
		} else if debug {
			debugLog.Printf("no translator element for {0, %d}\n", field.Type)
		}
	}

	return nil
}