| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | package core | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 	"log" | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	"regexp" | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	"github.com/pocketbase/dbx" | 
					
						
							| 
									
										
										
										
											2023-03-22 03:38:28 +08:00
										 |  |  | 	"github.com/pocketbase/pocketbase/tools/dbutils" | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	"github.com/pocketbase/pocketbase/tools/list" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ExpandFetchFunc defines the function that is used to fetch the expanded relation records.
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | type ExpandFetchFunc func(relCollection *Collection, relIds []string) ([]*Record, error) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // ExpandRecord expands the relations of a single Record model.
 | 
					
						
							| 
									
										
										
										
											2022-07-19 18:09:54 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2023-07-24 21:59:01 +08:00
										 |  |  | // If optFetchFunc is not set, then a default function will be used
 | 
					
						
							|  |  |  | // that returns all relation records.
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:36:01 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-07-19 18:09:54 +08:00
										 |  |  | // Returns a map with the failed expand parameters and their errors.
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | func (app *BaseApp) ExpandRecord(record *Record, expands []string, optFetchFunc ExpandFetchFunc) map[string]error { | 
					
						
							|  |  |  | 	return app.ExpandRecords([]*Record{record}, expands, optFetchFunc) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ExpandRecords expands the relations of the provided Record models list.
 | 
					
						
							| 
									
										
										
										
											2022-07-19 18:09:54 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2023-07-24 21:59:01 +08:00
										 |  |  | // If optFetchFunc is not set, then a default function will be used
 | 
					
						
							|  |  |  | // that returns all relation records.
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:36:01 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-07-19 18:09:54 +08:00
										 |  |  | // Returns a map with the failed expand parameters and their errors.
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | func (app *BaseApp) ExpandRecords(records []*Record, expands []string, optFetchFunc ExpandFetchFunc) map[string]error { | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	normalized := normalizeExpands(expands) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 18:09:54 +08:00
										 |  |  | 	failed := map[string]error{} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	for _, expand := range normalized { | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		if err := app.expandRecords(records, expand, optFetchFunc, 1); err != nil { | 
					
						
							| 
									
										
										
										
											2022-07-19 18:09:54 +08:00
										 |  |  | 			failed[expand] = err | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 18:09:54 +08:00
										 |  |  | 	return failed | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | // Deprecated
 | 
					
						
							|  |  |  | var indirectExpandRegexOld = regexp.MustCompile(`^(\w+)\((\w+)\)$`) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var indirectExpandRegex = regexp.MustCompile(`^(\w+)_via_(\w+)$`) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | // notes:
 | 
					
						
							| 
									
										
										
										
											2023-07-14 13:36:01 +08:00
										 |  |  | // - if fetchFunc is nil, dao.FindRecordsByIds will be used
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | // - all records are expected to be from the same collection
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | // - if maxNestedRels(6) is reached, the function returns nil ignoring the remaining expand path
 | 
					
						
							|  |  |  | func (app *BaseApp) expandRecords(records []*Record, expandPath string, fetchFunc ExpandFetchFunc, recursionLevel int) error { | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	if fetchFunc == nil { | 
					
						
							| 
									
										
										
										
											2023-07-14 13:36:01 +08:00
										 |  |  | 		// load a default fetchFunc
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		fetchFunc = func(relCollection *Collection, relIds []string) ([]*Record, error) { | 
					
						
							|  |  |  | 			return app.FindRecordsByIds(relCollection.Id, relIds) | 
					
						
							| 
									
										
										
										
											2023-07-14 13:36:01 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 	if expandPath == "" || recursionLevel > maxNestedRels || len(records) == 0 { | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	mainCollection := records[0].Collection() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 	var relField *RelationField | 
					
						
							|  |  |  | 	var relCollection *Collection | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	parts := strings.SplitN(expandPath, ".", 2) | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 	var matches []string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// @todo remove the old syntax support
 | 
					
						
							|  |  |  | 	if strings.Contains(parts[0], "(") { | 
					
						
							|  |  |  | 		matches = indirectExpandRegexOld.FindStringSubmatch(parts[0]) | 
					
						
							|  |  |  | 		if len(matches) == 3 { | 
					
						
							|  |  |  | 			log.Printf( | 
					
						
							|  |  |  | 				"%s expand format is deprecated and will be removed in the future. Consider replacing it with %s_via_%s.\n", | 
					
						
							|  |  |  | 				matches[0], | 
					
						
							|  |  |  | 				matches[1], | 
					
						
							|  |  |  | 				matches[2], | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		matches = indirectExpandRegex.FindStringSubmatch(parts[0]) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	if len(matches) == 3 { | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		indirectRel, _ := getCollectionByModelOrIdentifier(app, matches[1]) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		if indirectRel == nil { | 
					
						
							| 
									
										
										
										
											2024-02-26 03:06:43 +08:00
										 |  |  | 			return fmt.Errorf("couldn't find back-related collection %q", matches[1]) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		indirectRelField, _ := indirectRel.Fields.GetByName(matches[2]).(*RelationField) | 
					
						
							|  |  |  | 		if indirectRelField == nil || indirectRelField.CollectionId != mainCollection.Id { | 
					
						
							| 
									
										
										
										
											2024-02-26 03:06:43 +08:00
										 |  |  | 			return fmt.Errorf("couldn't find back-relation field %q in collection %q", matches[2], indirectRel.Name) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 		// add the related id(s) as a dynamic relation field value to
 | 
					
						
							|  |  |  | 		// allow further expand checks at later stage in a more unified manner
 | 
					
						
							|  |  |  | 		prepErr := func() error { | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 			q := app.DB().Select("id"). | 
					
						
							| 
									
										
										
										
											2024-02-26 03:06:43 +08:00
										 |  |  | 				From(indirectRel.Name). | 
					
						
							|  |  |  | 				Limit(1000) // the limit is arbitrary chosen and may change in the future
 | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 			if indirectRelField.IsMultiple() { | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 				q.AndWhere(dbx.Exists(dbx.NewExp(fmt.Sprintf( | 
					
						
							|  |  |  | 					"SELECT 1 FROM %s je WHERE je.value = {:id}", | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 					dbutils.JSONEach(indirectRelField.Name), | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 				)))) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				q.AndWhere(dbx.NewExp("[[" + indirectRelField.Name + "]] = {:id}")) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 			pq := q.Build().Prepare() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for _, record := range records { | 
					
						
							|  |  |  | 				var relIds []string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				err := pq.Bind(dbx.Params{"id": record.Id}).Column(&relIds) | 
					
						
							|  |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					return errors.Join(err, pq.Close()) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if len(relIds) > 0 { | 
					
						
							|  |  |  | 					record.Set(parts[0], relIds) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-02-19 22:55:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			return pq.Close() | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 		if prepErr != nil { | 
					
						
							|  |  |  | 			return prepErr | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		// indirect/back relation
 | 
					
						
							|  |  |  | 		relField = &RelationField{ | 
					
						
							|  |  |  | 			Name:         parts[0], | 
					
						
							|  |  |  | 			MaxSelect:    2147483647, | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			CollectionId: indirectRel.Id, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		if dbutils.HasSingleColumnUniqueIndex(indirectRelField.GetName(), indirectRel.Indexes) { | 
					
						
							|  |  |  | 			relField.MaxSelect = 1 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		relCollection = indirectRel | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// direct relation
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		relField, _ = mainCollection.Fields.GetByName(parts[0]).(*RelationField) | 
					
						
							|  |  |  | 		if relField == nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("couldn't find relation field %q in collection %q", parts[0], mainCollection.Name) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		relCollection, _ = getCollectionByModelOrIdentifier(app, relField.CollectionId) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		if relCollection == nil { | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 			return fmt.Errorf("couldn't find related collection %q", relField.CollectionId) | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	// ---------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	// extract the id of the relations to expand
 | 
					
						
							| 
									
										
										
										
											2022-07-15 00:26:08 +08:00
										 |  |  | 	relIds := make([]string, 0, len(records)) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	for _, record := range records { | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		relIds = append(relIds, record.GetStringSlice(relField.Name)...) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// fetch rels
 | 
					
						
							|  |  |  | 	rels, relsErr := fetchFunc(relCollection, relIds) | 
					
						
							|  |  |  | 	if relsErr != nil { | 
					
						
							|  |  |  | 		return relsErr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// expand nested fields
 | 
					
						
							|  |  |  | 	if len(parts) > 1 { | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		err := app.expandRecords(rels, parts[1], fetchFunc, recursionLevel+1) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// reindex with the rel id
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 	indexedRels := make(map[string]*Record, len(rels)) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	for _, rel := range rels { | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		indexedRels[rel.Id] = rel | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, model := range records { | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		// init expand if not already
 | 
					
						
							|  |  |  | 		// (this is done to ensure that the "expand" key will be returned in the response even if empty)
 | 
					
						
							|  |  |  | 		if model.expand == nil { | 
					
						
							|  |  |  | 			model.SetExpand(nil) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		relIds := model.GetStringSlice(relField.Name) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		validRels := make([]*Record, 0, len(relIds)) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		for _, id := range relIds { | 
					
						
							|  |  |  | 			if rel, ok := indexedRels[id]; ok { | 
					
						
							|  |  |  | 				validRels = append(validRels, rel) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(validRels) == 0 { | 
					
						
							|  |  |  | 			continue // no valid relations
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		expandData := model.Expand() | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 18:34:34 +08:00
										 |  |  | 		// normalize access to the previously expanded rel records (if any)
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		var oldExpandedRels []*Record | 
					
						
							| 
									
										
										
										
											2022-09-21 18:34:34 +08:00
										 |  |  | 		switch v := expandData[relField.Name].(type) { | 
					
						
							|  |  |  | 		case nil: | 
					
						
							|  |  |  | 			// no old expands
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		case *Record: | 
					
						
							|  |  |  | 			oldExpandedRels = []*Record{v} | 
					
						
							|  |  |  | 		case []*Record: | 
					
						
							| 
									
										
										
										
											2022-09-21 18:34:34 +08:00
										 |  |  | 			oldExpandedRels = v | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// merge expands
 | 
					
						
							|  |  |  | 		for _, oldExpandedRel := range oldExpandedRels { | 
					
						
							|  |  |  | 			// find a matching rel record
 | 
					
						
							|  |  |  | 			for _, rel := range validRels { | 
					
						
							|  |  |  | 				if rel.Id != oldExpandedRel.Id { | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 17:07:16 +08:00
										 |  |  | 				rel.MergeExpand(oldExpandedRel.Expand()) | 
					
						
							| 
									
										
										
										
											2022-09-21 18:34:34 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// update the expanded data
 | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		if relField.IsMultiple() { | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 			expandData[relField.Name] = validRels | 
					
						
							| 
									
										
										
										
											2024-09-30 00:23:19 +08:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			expandData[relField.Name] = validRels[0] | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-09-21 18:34:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		model.SetExpand(expandData) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // normalizeExpands normalizes expand strings and merges self containing paths
 | 
					
						
							|  |  |  | // (eg. ["a.b.c", "a.b", "   test  ", "  ", "test"] -> ["a.b.c", "test"]).
 | 
					
						
							|  |  |  | func normalizeExpands(paths []string) []string { | 
					
						
							|  |  |  | 	// normalize paths
 | 
					
						
							| 
									
										
										
										
											2022-07-15 00:26:08 +08:00
										 |  |  | 	normalized := make([]string, 0, len(paths)) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	for _, p := range paths { | 
					
						
							| 
									
										
										
										
											2022-07-15 00:26:08 +08:00
										 |  |  | 		p = strings.ReplaceAll(p, " ", "") // replace spaces
 | 
					
						
							|  |  |  | 		p = strings.Trim(p, ".")           // trim incomplete paths
 | 
					
						
							|  |  |  | 		if p != "" { | 
					
						
							|  |  |  | 			normalized = append(normalized, p) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// merge containing paths
 | 
					
						
							| 
									
										
										
										
											2022-07-15 00:26:08 +08:00
										 |  |  | 	result := make([]string, 0, len(normalized)) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	for i, p1 := range normalized { | 
					
						
							|  |  |  | 		var skip bool | 
					
						
							|  |  |  | 		for j, p2 := range normalized { | 
					
						
							|  |  |  | 			if i == j { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if strings.HasPrefix(p2, p1+".") { | 
					
						
							|  |  |  | 				// skip because there is more detailed expand path
 | 
					
						
							|  |  |  | 				skip = true | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !skip { | 
					
						
							|  |  |  | 			result = append(result, p1) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return list.ToUniqueStringSlice(result) | 
					
						
							|  |  |  | } |