Skip to content

Commit

Permalink
Merge inputFields
Browse files Browse the repository at this point in the history
This adds the capability to the CMD and Gomobile
to merge InputFields.
  • Loading branch information
AndreasSko committed Mar 21, 2021
1 parent f451ce8 commit 725f620
Show file tree
Hide file tree
Showing 9 changed files with 616 additions and 15 deletions.
3 changes: 3 additions & 0 deletions cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func mergeConflictHelp(name string) string {
"*model.Note": `A note collides if it exists on both sides (so they must have been synced at least once)
and it differers in the title or content. It generally makes sense to choose the note
with the newest date.`,

"*model.InputField": `InputFields are used in interactive publications where you can enter custom notes,
tick boxes, etc. An example would be the "Enjoy Life Forever!" brochure.`,
}

if text, ok := helpTexts[name]; ok {
Expand Down
37 changes: 36 additions & 1 deletion cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ be included in the merged backup. You are able to let the merger
automatically solve conflicts using the 'chooseLeft', 'chooseRight', and
'chooseNewest' resolvers (see Flags).`,
Example: `go-jwlm merge left.jwlibrary right.jwlibrary merged.jwlibrary
go-jwlm merge left.jwlibrary right.jwlibrary merged.jwlibrary --bookmarks chooseLeft --markings chooseRight --notes chooseNewest`,
go-jwlm merge left.jwlibrary right.jwlibrary merged.jwlibrary --bookmarks chooseLeft --markings chooseRight --notes chooseNewest --inputFields chooseRight`,
Run: func(cmd *cobra.Command, args []string) {
leftFilename := args[0]
rightFilename := args[1]
Expand All @@ -46,6 +46,9 @@ var MarkingResolver string
// NoteResolver represents a resolver that should be used for conflicting Notes
var NoteResolver string

// InputFieldResolver represents a resolver that should be used for conflicting InputFields
var InputFieldResolver string

func merge(leftFilename string, rightFilename string, mergedFilename string, stdio terminal.Stdio) {
fmt.Fprintln(stdio.Out, "Importing left backup")
left := model.Database{}
Expand All @@ -65,9 +68,13 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std

fmt.Fprintln(stdio.Out, "🧭 Merging Locations")
mergedLocations, locationIDChanges, err := merger.MergeLocations(left.Location, right.Location)
if err != nil {
log.Fatal(err)
}
merged.Location = mergedLocations
merger.UpdateLRIDs(left.Bookmark, right.Bookmark, "LocationID", locationIDChanges)
merger.UpdateLRIDs(left.Bookmark, right.Bookmark, "PublicationLocationID", locationIDChanges)
merger.UpdateLRIDs(left.InputField, right.InputField, "LocationID", locationIDChanges)
merger.UpdateLRIDs(left.Note, right.Note, "LocationID", locationIDChanges)
merger.UpdateLRIDs(left.TagMap, right.TagMap, "LocationID", locationIDChanges)
merger.UpdateLRIDs(left.UserMark, right.UserMark, "LocationID", locationIDChanges)
Expand Down Expand Up @@ -100,6 +107,33 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std
}
fmt.Fprintln(stdio.Out, "Done.")

fmt.Fprintln(stdio.Out, "✍️ Merging InputFields")
inputFieldConflictSolution := map[string]merger.MergeSolution{}
for {
mergedInputFields, _, err := merger.MergeInputFields(left.InputField, right.InputField, inputFieldConflictSolution)
if err == nil {
merged.InputField = mergedInputFields
break
}
switch err := err.(type) {
case merger.MergeConflictError:
if InputFieldResolver != "" {
var resErr error
newSolutions, resErr := merger.AutoResolveConflicts(err.Conflicts, InputFieldResolver)
if resErr != nil {
log.Fatal(resErr)
}
addToSolutions(inputFieldConflictSolution, newSolutions)
} else {
newSolutions := handleMergeConflict(err.Conflicts, &merged, stdio)
addToSolutions(inputFieldConflictSolution, newSolutions)
}
default:
log.Fatal(err)
}
}
fmt.Fprintln(stdio.Out, "Done.")

fmt.Fprintln(stdio.Out, "🏷 Merging Tags")
var tagsConflictSolution map[string]merger.MergeSolution
for {
Expand Down Expand Up @@ -277,4 +311,5 @@ func init() {
mergeCmd.Flags().StringVar(&BookmarkResolver, "bookmarks", "", "Resolve conflicting bookmarks with resolver (can be 'chooseLeft' or 'chooseRight')")
mergeCmd.Flags().StringVar(&MarkingResolver, "markings", "", "Resolve conflicting markings with resolver (can be 'chooseLeft' or 'chooseRight')")
mergeCmd.Flags().StringVar(&NoteResolver, "notes", "", "Resolve conflicting notes with resolver (can be 'chooseNewest', 'chooseLeft', or 'chooseRight')")
mergeCmd.Flags().StringVar(&InputFieldResolver, "inputFields", "", "Resolve conflicting inputFields with resolver (can be 'chooseLeft', or 'chooseRight')")
}
149 changes: 139 additions & 10 deletions cmd/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func Test_merge(t *testing.T) {
c.ExpectString("📑 Merging Bookmarks")
c.SendLine(string(terminal.KeyArrowDown))

c.ExpectString("✍️ Merging InputFields")
c.SendLine(string(terminal.KeyArrowDown))

c.ExpectString("🖍 Merging Markings")
c.SendLine(string(terminal.KeyArrowDown))

Expand All @@ -81,6 +84,9 @@ func Test_merge(t *testing.T) {
c.ExpectString("📑 Merging Bookmarks")
c.SendLine("")

c.ExpectString("✍️ Merging InputFields")
c.SendLine("")

c.ExpectString("🖍 Merging Markings")
c.SendLine("")

Expand Down Expand Up @@ -108,6 +114,7 @@ func Test_merge(t *testing.T) {
BookmarkResolver = "chooseRight"
MarkingResolver = "chooseRight"
NoteResolver = "chooseNewest"
InputFieldResolver = "chooseRight"
merge(leftFilename, rightFilename, mergedFilename,
terminal.Stdio{In: c.Tty(), Out: c.Tty(), Err: c.Tty()})
merged := &model.Database{}
Expand Down Expand Up @@ -196,8 +203,20 @@ var leftMultiCollision = &model.Database{
UserMarkID: 4,
},
},
Bookmark: []*model.Bookmark{nil},
InputField: []*model.InputField{nil},
Bookmark: []*model.Bookmark{nil},
InputField: []*model.InputField{
nil,
{
LocationID: 1,
TextTag: "a1",
Value: "a1",
},
{
LocationID: 1,
TextTag: "a2",
Value: "a2",
},
},
Location: []*model.Location{
nil,
{
Expand Down Expand Up @@ -258,8 +277,25 @@ var rightMultiCollision = &model.Database{
UserMarkID: 1,
},
},
Bookmark: []*model.Bookmark{nil},
InputField: []*model.InputField{nil},
Bookmark: []*model.Bookmark{nil},
InputField: []*model.InputField{
nil,
{
LocationID: 1,
TextTag: "a1",
Value: "different",
},
{
LocationID: 1,
TextTag: "a2",
Value: "a2",
},
{
LocationID: 1,
TextTag: "b1",
Value: "b1",
},
},
Location: []*model.Location{
nil,
{
Expand Down Expand Up @@ -313,7 +349,19 @@ var leftDB = &model.Database{
BlockIdentifier: sql.NullInt32{1, true},
},
},
InputField: []*model.InputField{nil},
InputField: []*model.InputField{
nil,
{
LocationID: 5,
TextTag: "a1",
Value: "a1",
},
{
LocationID: 5,
TextTag: "a2",
Value: "a2",
},
},
Location: []*model.Location{
nil,
{
Expand All @@ -331,6 +379,15 @@ var leftDB = &model.Database{
MepsLanguage: 2,
LocationType: 1,
},
nil,
nil,
{
LocationID: 5,
DocumentID: sql.NullInt32{1102021811, true},
KeySymbol: sql.NullString{"lffi", true},
MepsLanguage: 2,
LocationType: 0,
},
},
Note: []*model.Note{
nil,
Expand Down Expand Up @@ -448,7 +505,24 @@ var rightDB = &model.Database{
BlockIdentifier: sql.NullInt32{1, true},
},
},
InputField: []*model.InputField{nil},
InputField: []*model.InputField{
nil,
{
LocationID: 4,
TextTag: "a1",
Value: "different",
},
{
LocationID: 4,
TextTag: "a2",
Value: "a2",
},
{
LocationID: 4,
TextTag: "b1",
Value: "b1",
},
},
Location: []*model.Location{
nil,
{
Expand All @@ -475,6 +549,13 @@ var rightDB = &model.Database{
LocationType: 0,
Title: sql.NullString{"1. Mose 1", true},
},
{
LocationID: 4,
DocumentID: sql.NullInt32{1102021811, true},
KeySymbol: sql.NullString{"lffi", true},
MepsLanguage: 2,
LocationType: 0,
},
},
Note: []*model.Note{
nil,
Expand Down Expand Up @@ -562,7 +643,7 @@ var rightDB = &model.Database{
},
}

var mergedAllLeftDB = model.Database{
var mergedAllLeftDB = &model.Database{
BlockRange: []*model.BlockRange{
nil,
{
Expand Down Expand Up @@ -602,7 +683,24 @@ var mergedAllLeftDB = model.Database{
BlockIdentifier: sql.NullInt32{1, true},
},
},
InputField: []*model.InputField{nil},
InputField: []*model.InputField{
nil,
{
LocationID: 4,
TextTag: "a1",
Value: "a1",
},
{
LocationID: 4,
TextTag: "a2",
Value: "a2",
},
{
LocationID: 4,
TextTag: "b1",
Value: "b1",
},
},
Location: []*model.Location{
nil,
{
Expand All @@ -629,6 +727,13 @@ var mergedAllLeftDB = model.Database{
LocationType: 0,
Title: sql.NullString{"1. Mose 2", true},
},
{
LocationID: 4,
DocumentID: sql.NullInt32{1102021811, true},
KeySymbol: sql.NullString{"lffi", true},
MepsLanguage: 2,
LocationType: 0,
},
},
Note: []*model.Note{
nil,
Expand Down Expand Up @@ -736,7 +841,7 @@ var mergedAllLeftDB = model.Database{
},
}

var mergedAllRightDB = model.Database{
var mergedAllRightDB = &model.Database{
BlockRange: []*model.BlockRange{
nil,
{
Expand Down Expand Up @@ -784,7 +889,24 @@ var mergedAllRightDB = model.Database{
BlockIdentifier: sql.NullInt32{1, true},
},
},
InputField: []*model.InputField{nil},
InputField: []*model.InputField{
nil,
{
LocationID: 4,
TextTag: "a1",
Value: "different",
},
{
LocationID: 4,
TextTag: "a2",
Value: "a2",
},
{
LocationID: 4,
TextTag: "b1",
Value: "b1",
},
},
Location: []*model.Location{
nil,
{
Expand All @@ -811,6 +933,13 @@ var mergedAllRightDB = model.Database{
LocationType: 0,
Title: sql.NullString{"1. Mose 2", true},
},
{
LocationID: 4,
DocumentID: sql.NullInt32{1102021811, true},
KeySymbol: sql.NullString{"lffi", true},
MepsLanguage: 2,
LocationType: 0,
},
},
Note: []*model.Note{
nil,
Expand Down
33 changes: 33 additions & 0 deletions gomobile/Merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func (dbw *DatabaseWrapper) MergeLocations() error {
dbw.merged.Location = mergedLocations
merger.UpdateLRIDs(dbw.leftTmp.Bookmark, dbw.rightTmp.Bookmark, "LocationID", locationIDChanges)
merger.UpdateLRIDs(dbw.leftTmp.Bookmark, dbw.rightTmp.Bookmark, "PublicationLocationID", locationIDChanges)
merger.UpdateLRIDs(dbw.leftTmp.InputField, dbw.rightTmp.InputField, "LocationID", locationIDChanges)
merger.UpdateLRIDs(dbw.leftTmp.Note, dbw.rightTmp.Note, "LocationID", locationIDChanges)
merger.UpdateLRIDs(dbw.leftTmp.TagMap, dbw.rightTmp.TagMap, "LocationID", locationIDChanges)
merger.UpdateLRIDs(dbw.leftTmp.UserMark, dbw.rightTmp.UserMark, "LocationID", locationIDChanges)
Expand Down Expand Up @@ -53,6 +54,38 @@ func (dbw *DatabaseWrapper) MergeBookmarks(conflictSolver string, mcw *MergeConf
return nil
}

// MergeInputField merges inputFields
func (dbw *DatabaseWrapper) MergeInputFields(conflictSolver string, mcw *MergeConflictsWrapper) error {
var conflictSolution = mcw.solutions
if conflictSolution == nil {
conflictSolution = map[string]merger.MergeSolution{}
}
for {
merged, _, err := merger.MergeInputFields(dbw.leftTmp.InputField, dbw.rightTmp.InputField, conflictSolution)
if err == nil {
dbw.merged.InputField = merged
break
}
switch err := err.(type) {
case merger.MergeConflictError:
if conflictSolver == "" {
mcw.addConflicts(err.Conflicts)
return MergeConflictError{}
}
var resErr error
newSolutions, resErr := merger.AutoResolveConflicts(err.Conflicts, conflictSolver)
if resErr != nil {
return errors.Wrap(err, "Could not automatically solve conflicts for inputFields")
}
addToSolutions(conflictSolution, newSolutions)
default:
return errors.Wrap(err, "Could not merge inputFields")
}
}

return nil
}

// MergeTags merges tags
func (dbw *DatabaseWrapper) MergeTags() error {
var conflictSolution map[string]merger.MergeSolution
Expand Down
Loading

0 comments on commit 725f620

Please sign in to comment.