Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/asset: Save/Load functinality for assets into a state file
1. Calls out on the Save function to persist the state file of all assets in a constant state file 2. Calls out on the Load function to partially load the contents of the state file into a new field in StoreImpl 3. In Save, marshaling as done as a map of type.String() to asset bytes: i.e. stateMap[reflect.TypeOf(assetObject).String()] = marshalledBytes(assetObject) reflect.TypeOf().String() is used as against reflect.Type.Name() function because Name returns the type name only, without scoping the package path. See the implementation of type.Name() function where type.String() is used within: https://golang.org/src/reflect/type.go?#L874 4. Support for 'deferred unmarshal' for assets from state file: Before a target is worked upon, the state file is loaded into the memory as partial asset state map. The key of the map is the string representation of the asset type and the value is raw bytes that are left as is. The idea is that 'fetch' will finally get the asset from the state file, only when needed. A utility function GetStateAsset has been provided to allow for deferred unmarshaling. See example code to use the util function (as in the store's fetch function): ``` func (s *StoreImpl) fetch(asset Asset, indent string) error { ... ... ok, err := s.GetStateAsset(asset) if err != nil { return errors.Wrapf("failed to unmarshal asset from state file: %v. Remove the state file and continue..", err) } if ok { logrus.Debugf("%sAsset found in state file %v", indent, asset) if s.assets == nil { s.assets = make(map[reflect.Type]Asset) } s.assets[reflect.TypeOf(asset)] = asset return nil } ... ... ``` Alternatively, instead of passing the empty asset object, one can make a copy of the asset object and render it with contents from the state file: ``` newAsset := reflect.ValueOf(reflect.New(reflect.TypeOf(asset))).Elem().Interface() ok, err := s.GetStateAsset(newAsset) // now compare newAsset with asset itself ... // and set the contents of asset from newAsset if needed: reflect.ValueOf(asset).Elem().Set(reflect.ValueOf(newAsset).Elem()) ``` Other notes: The utility function GetStateAsset used in this commit such that if an asset is found in the state file, then its used directly. Further work will need to modify this behaviour so that a three way merge can happen between an asset found in the state file, found on disk, rendered by the Generate function.
- Loading branch information
Just
return errors.Wrap