Skip to content

Commit

Permalink
🔧 chore(option.go): remove duplicated comment and reorder struct fields
Browse files Browse the repository at this point in the history
✨ feat(option.go): add QuestionID field to Option struct to improve semantics
🔧 chore(option.go): add GetQuestionID and SetQuestionID methods to Option struct
✨ feat(params.go): add WithQuestionID function to set QuestionID field of Options struct
🔧 chore(question/params.go): set QuestionID of options when creating a question
✨ feat(question/question.go): add ID field to Meta struct to improve semantics
  • Loading branch information
thalesfsp committed May 24, 2023
1 parent e338f48 commit 664af6e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
28 changes: 22 additions & 6 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type Option[T shared.N] struct {
// Label is the label of the option.
Label string `json:"label" bson:"label"`

// QuestionID is the ID of the question.
QuestionID string `json:"questionID" bson:"questionID"`

// State sets the State of the option.
State status.Status `json:"state" bson:"state"`

// Value is the value of the option.
Value T `json:"value" bson:"value"`

Expand All @@ -26,9 +32,6 @@ type Option[T shared.N] struct {

// NextQuestion is the next question ID.
nextQuestionID string `json:"-" bson:"-"`

// State sets the State of the option.
State status.Status `json:"state" bson:"state"`
}

//////
Expand All @@ -45,6 +48,18 @@ func (o Option[T]) GetLabel() string {
return o.Label
}

// GetQuestionID returns the question ID of the option.
func (o Option[T]) GetQuestionID() string {
return o.QuestionID
}

// SetQuestionID returns the question ID of the option.
func (o Option[T]) SetQuestionID(id string) Option[T] {
o.QuestionID = id

return o
}

// GetValue returns the value of the option.
func (o Option[T]) GetValue() T {
return o.Value
Expand Down Expand Up @@ -92,9 +107,10 @@ func New[T shared.N](value T, params ...Func) (Option[T], error) {
}

o := Option[T]{
Label: p.Label,
Value: value,
Weight: p.Weight,
Label: p.Label,
QuestionID: p.QuestionID,
Value: value,
Weight: p.Weight,

nextQuestionID: p.NextQuestionID,
State: p.State,
Expand Down
12 changes: 12 additions & 0 deletions option/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ type Options struct {
// NextQuestion is the next question ID.
NextQuestionID string `json:"nextQuestionID"`

// QuestionID is the ID of the question.
QuestionID string `json:"questionID" bson:"questionID"`

// state sets the state of the option.
State status.Status `json:"state"`

Expand Down Expand Up @@ -132,3 +135,12 @@ func WithID(id string) Func {
return nil
}
}

// WithQuestionID sets the weight of the option.
func WithQuestionID(id string) Func {
return func(o *Options) error {
o.QuestionID = id

return nil
}
}
4 changes: 4 additions & 0 deletions question/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func WithOption[T shared.N](opts ...option.Option[T]) Func {
}

for _, opt := range opts {
if m.ID != "" {
opt.QuestionID = m.ID
}

m.options = append(m.options, opt)
}

Expand Down
1 change: 1 addition & 0 deletions question/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func New[T shared.N](
params ...Func,
) (Question, error) {
m := Meta{
ID: id,
ImageURL: "",
Required: false,
options: []any{},
Expand Down

0 comments on commit 664af6e

Please sign in to comment.