surveygo

package module
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2023 License: GPL-3.0 Imports: 11 Imported by: 0

README

Survey Processing Library Documentation

Table of Contents

  1. Overview
  2. Base Structures
  3. Question Structures
  4. Functions

Overview

surveygo facilitates the creation and management of surveys. It provides data structures and methods for creating surveys, questions, and groups of questions, as well as handling validations.

Definitions and Rules within a survey
  • All survey structures will have a unique identifier called nameId.
  • Every reference to a nameId must be unique, or in other words:
    • A question can only be associated with one group.
    • A group can only be associated with one question or the initial set of groups groupsOrder.

Base Structures

Survey

Structure representing a complete survey.

Fields

  • title: Survey title. (Required)
  • version: Survey version. (Required)
  • description: Survey description. (Optional)
  • questions: Map of questions. (Required)
  • groups: Map of groups. (Required)
  • groupsOrder: Order of the groups. (Required)
Question

Structure representing a question within a survey.

Fields

  • nameId: Question identifier. (Required)
  • visible: Indicates if the question is visible. (Required)
  • type: Type of the question. (Required)
  • label: Question label. (Required)
  • required: Indicates if the question is mandatory to answer. (Required)
  • Value: Object representing the value of the question. Varies depending on the type of question. (Required)
Group

Structure representing a group of questions in a survey.

Fields

  • nameId: Group identifier. (Required)
  • title: Group title. (Optional)
  • description: Group description. (Optional)
  • visible: Indicates if the group is visible. (Required)
  • isExternalSurvey: Indicates if the group is an external survey. (Optional)
  • questionsIds: Identifiers of the questions that belong to the group. (Required)
    If the group is an external survey, this field will indicate the identifier of the external survey.

Question Structures

Types of Questions
Choice:
  • single_select: Single select
  • multi_select: Multiple select
  • radio: Single select
  • checkbox: Multiple select
Text:
  • email: Email
  • telephone: Telephone
  • text_area: Free text
  • input_text: Free text
  • information: Information field, not editable
External Questions:
  • external_question: External question
Choice

Structure for all questions in the Choice group.

  • placeholder: Placeholder text for the question. (Optional)
  • defaults: List of default values for the question. Each value must be a valid nameId of an option. (Optional)
  • options: Question options. (Required)
    • nameId: Option identifier. (Required)
    • label: Option label. (Required)
    • groupsIds: Identifiers of the groups to be displayed when the option is selected. (Optional)
Text

Email (email)

  • placeholder: Placeholder text for the question. (Optional)
  • allowedDomains: Allowed domains for the email. (Optional)

Telephone (telephone)

  • placeholder: Placeholder text for the question. (Optional)
  • allowedCountryCodes: List of allowed country codes. (Optional)

FreeText (input_text and text_area)

  • placeholder: Placeholder text for the question. (Optional)
  • min: Minimum length of the text. (Optional)
  • max: Maximum length of the text. (Optional)

Information (information)

  • text: Text to be displayed. (Required)
External Question

Used to create an external questions.

External Question (external_question)

  • placeholder: Placeholder for the question. (Optional)
  • defaults: List of default values for the question. (Optional)
  • questionType: Type of the question. Refer to Types of Questions. (Required)
  • externalType: Type of the external question. (Required)
  • description: Description of the external question. (Optional)
  • src: Source of the external question. (Optional)

Functions

For the complete list of available functions and methods, please refer to the files:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SurveyValidator = newSurveyValidator(validatorTranslator)

Functions

func TranslateValidationErrors

func TranslateValidationErrors(err error) []error

Types

type Answers

type Answers map[string][]any

Answers is a map with the answers provided by the user. The key is the question NameId (Question.NameId).

type InvalidAnswerError

type InvalidAnswerError struct {
	QuestionNameId string `json:"questionNameId,omitempty" bson:"questionNameId,omitempty"`
	Answer         any    `json:"answer,omitempty" bson:"answer,omitempty"`
	Error          string `json:"error,omitempty" bson:"error,omitempty"`
}

type Survey

type Survey struct {
	// Title is the title of the survey.
	// Validations:
	//	- required
	//	- min length: 1
	Title string `json:"title,omitempty" bson:"title,omitempty" validate:"required,min=1"`

	// Version is the version of the survey.
	// Validations:
	//	- required
	//	- min length: 1
	Version string `json:"version,omitempty" bson:"version,omitempty" validate:"required,min=1"`

	// Description is the description of the survey.
	// Validations:
	//	- optional
	//	- min length: 1
	Description *string `json:"description,omitempty" bson:"description,omitempty" validate:"omitempty"`

	// Questions is a map with all the questions in the survey.
	// The key is the question NameId (Question.NameId).
	// Validations:
	//	- required
	//	- min length: 1
	//	- each question must be valid
	Questions map[string]*question.Question `json:"questions,omitempty" bson:"questions,omitempty" validate:"required,dive"`

	// Groups is a map with all the groups in the survey.
	// The key is the group NameId (Group.NameId).
	// Validations:
	//	- required
	//	- min length: 1
	//	- each group must be valid
	Groups map[string]*question.Group `json:"groups,omitempty" bson:"groups,omitempty" validate:"required,dive"`

	// GroupsOrder is a list of group name ids that defines the order of the groups in the survey.
	// Validations:
	//	- required
	//	- min length: 1
	GroupsOrder []string `json:"groupsOrder,omitempty" bson:"groupsOrder,omitempty" validate:"required"`
}

Survey is a struct representation of a survey.

func NewSurvey

func NewSurvey(title, version string, description *string) (*Survey, error)

NewSurvey creates a new Survey instance with the given title, version, and description. Args:

  • nameId: the name id of the survey (required)
  • title: the title of the survey (required)
  • version: the version of the survey (required)
  • description: the description of the survey (optional)

Returns:

  • *Survey: the new survey instance
  • error: if an error occurred

func ParseFromBytes

func ParseFromBytes(b []byte) (*Survey, error)

ParseFromBytes converts the given json byte slice into a Survey instance.

func ParseFromJsonStr

func ParseFromJsonStr(jsonSurvey string) (*Survey, error)

ParseFromJsonStr converts the given json string into a Survey instance.

func (*Survey) AddGroup

func (s *Survey) AddGroup(g *question.Group) error

AddGroup adds a group to the survey. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) AddGroupBytes

func (s *Survey) AddGroupBytes(g []byte) error

AddGroupBytes adds a group to the survey given its representation as a byte array It also validates the group and checks if the group is consistent with the survey.

func (*Survey) AddGroupJson

func (s *Survey) AddGroupJson(g string) error

AddGroupJson adds a group to the survey given its representation as a JSON string It also validates the group and checks if the group is consistent with the survey.

func (*Survey) AddGroupMap

func (s *Survey) AddGroupMap(g map[string]any) error

AddGroupMap adds a group to the survey given its representation as a map[string]any It also validates the group and checks if the group is consistent with the survey.

func (*Survey) AddOrUpdateQuestion

func (s *Survey) AddOrUpdateQuestion(q *question.Question) error

AddOrUpdateQuestion adds a question to the survey if it does not exist, or updates it if it does. It also validates the question and checks if the question is consistent with the survey.

func (*Survey) AddOrUpdateQuestionBytes

func (s *Survey) AddOrUpdateQuestionBytes(qb []byte) error

AddOrUpdateQuestionBytes adds a question to the survey if it does not exist, or updates it if it does. It also validates the question and checks if the question is consistent with the survey.

func (*Survey) AddOrUpdateQuestionJson

func (s *Survey) AddOrUpdateQuestionJson(qs string) error

AddOrUpdateQuestionJson adds a question to the survey if it does not exist, or updates it if it does. It also validates the question and checks if the question is consistent with the survey.

func (*Survey) AddOrUpdateQuestionMap

func (s *Survey) AddOrUpdateQuestionMap(qm map[string]any) error

AddOrUpdateQuestionMap adds a question to the survey if it does not exist, or updates it if it does. It also validates the question and checks if the question is consistent with the survey.

func (*Survey) AddQuestion

func (s *Survey) AddQuestion(q *question.Question) error

AddQuestion adds a question to the survey. It also validates the question and checks if the question is consistent with the survey.

func (*Survey) AddQuestionBytes

func (s *Survey) AddQuestionBytes(qb []byte) error

AddQuestionBytes adds a question to the survey given its representation as a byte array It also validates the question and checks if the question is consistent with the survey.

func (*Survey) AddQuestionJson

func (s *Survey) AddQuestionJson(qs string) error

AddQuestionJson adds a question to the survey given its representation as a JSON string It also validates the question and checks if the question is consistent with the survey.

func (*Survey) AddQuestionMap

func (s *Survey) AddQuestionMap(qm map[string]any) error

AddQuestionMap adds a question to the survey given its representation as a map[string]any It also validates the question and checks if the question is consistent with the survey.

func (*Survey) AddQuestionToGroup

func (s *Survey) AddQuestionToGroup(questionNameId, groupNameId string, position int) error

AddQuestionToGroup adds a question to a group in the survey. Args: * questionNameId: the nameId of the question to add. * groupNameId: the nameId of the group to add the question to. * position: the position of the question in the group. If position is -1, the question will be added at the end of the group. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) GetQuestionsAssignments

func (s *Survey) GetQuestionsAssignments() map[string]string

GetQuestionsAssignments returns a map with the question nameId as key and the group nameId as value. If a question is not assigned to any group, value will be empty.

func (*Survey) RemoveGroup

func (s *Survey) RemoveGroup(groupNameId string) error

RemoveGroup removes a group from the survey given its nameId. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) RemoveQuestion

func (s *Survey) RemoveQuestion(questionNameId string) error

RemoveQuestion removes a question from the survey given its nameId. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) RemoveQuestionFromGroup

func (s *Survey) RemoveQuestionFromGroup(questionNameId, groupNameId string) error

RemoveQuestionFromGroup removes a question from a group in the survey. Args: * questionNameId: the nameId of the question to remove. * groupNameId: the nameId of the group to remove the question from. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) ReviewAnswers

func (s *Survey) ReviewAnswers(ans Answers) (*SurveyResume, error)

ReviewAnswers verifies if the answers provided are valid for this survey. Args: * ans: the answers to check. Returns: * map[string]bool:

  • key: the name id of the missing question
  • value: if the question is required or not
  • error: if an error occurred

func (*Survey) ToJson

func (s *Survey) ToJson() (string, error)

ToJson returns a JSON string representation of the survey.

func (*Survey) ToMap

func (s *Survey) ToMap() (map[string]any, error)

ToMap returns a map representation of the survey.

func (*Survey) UpdateGroupBytes

func (s *Survey) UpdateGroupBytes(ug []byte) error

UpdateGroupBytes updates a group in the survey given its representation as a byte array It also validates the group and checks if the group is consistent with the survey.

func (*Survey) UpdateGroupJson

func (s *Survey) UpdateGroupJson(ug string) error

UpdateGroupJson updates an existing group in the survey with the data provided as a JSON string. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) UpdateGroupMap

func (s *Survey) UpdateGroupMap(ug map[string]any) error

UpdateGroupMap updates an existing group in the survey with the data provided as a map. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) UpdateGroupQuestions

func (s *Survey) UpdateGroupQuestions(groupNameId string, questionsIds []string) error

UpdateGroupQuestions updates the questions of a group in the survey. Args: * groupNameId: the nameId of the group to update. * questionsIds: the list of questions ids to update the group with. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) UpdateGroupsOrder

func (s *Survey) UpdateGroupsOrder(order []string) error

UpdateGroupsOrder updates the groups order in the survey. It also validates the group and checks if the group is consistent with the survey.

func (*Survey) UpdateQuestion

func (s *Survey) UpdateQuestion(uq *question.Question) error

UpdateQuestion updates an existing question in the survey. It also validates the question and checks if the question is consistent with the survey.

func (*Survey) UpdateQuestionBytes

func (s *Survey) UpdateQuestionBytes(uq []byte) error

UpdateQuestionBytes updates a question in the survey given its representation as a byte array It also validates the question and checks if the question is consistent with the survey.

func (*Survey) UpdateQuestionJson

func (s *Survey) UpdateQuestionJson(uq string) error

UpdateQuestionJson updates an existing question in the survey with the data provided as a JSON string. It also validates the question and checks if the question is consistent with the survey.

func (*Survey) UpdateQuestionMap

func (s *Survey) UpdateQuestionMap(uq map[string]any) error

UpdateQuestionMap updates an existing question in the survey with the data provided as a map. It also validates the question and checks if the question is consistent with the survey.

func (*Survey) ValidateSurvey

func (s *Survey) ValidateSurvey() error

ValidateSurvey validates the survey.

type SurveyResume

type SurveyResume struct {
	TotalsResume `json:",inline" bson:",inline"`

	//----- Others Totals -----//
	// ExternalSurveyIds map of external survey ids. Key: GroupNameId, Value: ExternalSurveyId
	ExternalSurveyIds map[string]string `json:"externalSurveyIds,omitempty" bson:"externalSurveyIds,omitempty"`

	//----- Groups -----//
	// GroupsResume map of groups resume. Key: GroupNameId, Value: GroupResume
	GroupsResume map[string]*TotalsResume `json:"groupsResume,omitempty" bson:"groupsResume,omitempty"`

	//----- Errors -----//
	// InvalidAnswers list of invalid answers
	InvalidAnswers []InvalidAnswerError `json:"invalidAnswers,omitempty" bson:"invalidAnswers,omitempty"`
}

SurveyResume contains the resume of a survey based on the answers provided. All values are calculated based on the answers provided and over the visible components of the survey.

type TotalsResume

type TotalsResume struct {
	//----- Questions Totals -----//
	// TotalQuestions number of questions in the group
	TotalQuestions int `json:"totalQuestions,omitempty" bson:"totalQuestions,omitempty"`
	// TotalRequiredQuestions number of required questions in the group
	TotalRequiredQuestions int `json:"totalRequiredQuestions,omitempty" bson:"totalRequiredQuestions,omitempty"`

	//----- Answers Totals  -----//
	// TotalQuestionsAnswered number of answered questions in the group
	TotalQuestionsAnswered int `json:"totalQuestionsAnswered,omitempty" bson:"totalQuestionsAnswered,omitempty"`
	// TotalRequiredQuestionsAnswered number of required questions answered in the group
	TotalRequiredQuestionsAnswered int `json:"totalRequiredQuestionsAnswered,omitempty" bson:"totalRequiredQuestionsAnswered,omitempty"`
	// UnansweredQuestions map of unanswered questions, key is the nameId of the question, value is true if the question is required
	UnansweredQuestions map[string]bool `json:"unansweredQuestions,omitempty" bson:"unansweredQuestions,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL