<commit_before>// Copyright (C) 2016 Space Monkey, Inc.
//
// 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 ir

import "gopkg.in/spacemonkeygo/dbx.v1/ast"

type linker struct {
	models map[string]*modelLink
}

func newLinker() *linker {
	return &linker{
		models: make(map[string]*modelLink),
	}
}

func (l *linker) AddModel(ast_model *ast.Model) (link *modelLink, err error) {
	if existing, ok := l.models[ast_model.Name]; ok {
		return nil, Error.New("%s: model %q already defined at %s",
			ast_model.Pos, ast_model.Name, existing.ast.Pos)
	}

	link = newModelLink(ast_model)
	l.models[ast_model.Name] = link
	return link, nil
}

func (l *linker) GetModel(name string) *modelLink {
	return l.models[name]
}

func (l *linker) FindModel(ref *ast.FieldRef) (*Model, error) {
	link := l.models[ref.Model]
	if link != nil {
		return link.model, nil
	}
	return nil, Error.New("%s: no model %q defined",
		ref.Pos, ref.Model)
}

func (l *linker) FindField(ref *ast.FieldRef) (*Field, error) {
	model_link := l.models[ref.Model]
	if model_link == nil {
		return nil, Error.New("%s: no model %q defined",
			ref.Pos, ref.Model)
	}
	return model_link.FindField(ref.Relative())
}

type modelLink struct {
	model  *Model
	ast    *ast.Model
	fields map[string]*fieldLink
}

func newModelLink(ast_model *ast.Model) *modelLink {
	return &modelLink{
		model:  &Model{},
		ast:    ast_model,
		fields: make(map[string]*fieldLink),
	}
}

func (m *modelLink) newFieldLink(ast_field *ast.Field) *fieldLink {
	field := &Field{
		Model: m.model,
	}
	m.model.Fields = append(m.model.Fields, field)

	return &fieldLink{
		field: field,
		ast:   ast_field,
	}
}

func (m *modelLink) AddField(ast_field *ast.Field) (err error) {
	if existing, ok := m.fields[ast_field.Name]; ok {
		return Error.New("%s: field %q already defined at %s",
			ast_field.Pos, ast_field.Name, existing.ast.Pos)
	}
	m.fields[ast_field.Name] = m.newFieldLink(ast_field)
	return nil
}

func (m *modelLink) GetField(name string) *fieldLink {
	return m.fields[name]<commit_msg>add name and type to the linked models<commit_after>// Copyright (C) 2016 Space Monkey, Inc.
//
// 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 ir

import "gopkg.in/spacemonkeygo/dbx.v1/ast"

type linker struct {
	models map[string]*modelLink
}

func newLinker() *linker {
	return &linker{
		models: make(map[string]*modelLink),
	}
}

func (l *linker) AddModel(ast_model *ast.Model) (link *modelLink, err error) {
	if existing, ok := l.models[ast_model.Name]; ok {
		return nil, Error.New("%s: model %q already defined at %s",
			ast_model.Pos, ast_model.Name, existing.ast.Pos)
	}

	link = newModelLink(ast_model)
	l.models[ast_model.Name] = link
	return link, nil
}

func (l *linker) GetModel(name string) *modelLink {
	return l.models[name]
}

func (l *linker) FindModel(ref *ast.FieldRef) (*Model, error) {
	link := l.models[ref.Model]
	if link != nil {
		return link.model, nil
	}
	return nil, Error.New("%s: no model %q defined",
		ref.Pos, ref.Model)
}

func (l *linker) FindField(ref *ast.FieldRef) (*Field, error) {
	model_link := l.models[ref.Model]
	if model_link == nil {
		return nil, Error.New("%s: no model %q defined",
			ref.Pos, ref.Model)
	}
	return model_link.FindField(ref.Relative())
}

type modelLink struct {
	model  *Model
	ast    *ast.Model
	fields map[string]*fieldLink
}

func newModelLink(ast_model *ast.Model) *modelLink {
	return &modelLink{
		model: &Model{
			Name: ast_model.Name,
		},
		ast:    ast_model,
		fields: make(map[string]*fieldLink),
	}
}

func (m *modelLink) newFieldLink(ast_field *ast.Field) *fieldLink {
	field := &Field{
		Name:  ast_field.Name,
		Type:  ast_field.Type,
		Model: m.model,
	}
	m.model.Fields = append(m.model.Fields, field)

	return &fieldLink{
		field: field,
		ast:   ast_field,
	}
}

func (m *modelLink) AddField(ast_field *ast.Field) (err error) {
	if existing, ok := m.fields[ast_field.Name]; ok {
		return Error.New("%s: field %q already defined at %s",
			ast_field.Pos, ast_field.Name, existing.ast.Pos)
	}
	m.fields[ast_field.Name] = m.newFieldLink(ast_field)
	return nil
}

func (m *modelLink) GetField(name string) *fieldLink {
	return m.fields[name]
{'fraction_non_alphanumeric': 0.13450405248938635, 'fraction_numerical': 0.0034735623311462757, 'mean_word_length': 4.032038834951456, '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': '7166696', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1818, 'n_words': 540}