github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/cache.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import ( 18 "errors" 19 ) 20 21 type Cache struct { 22 Paths Stringorslice `yaml:"paths,omitempty"` 23 Key *CacheKey `yaml:"key,omitempty"` 24 Untracked bool `yaml:"untracked,omitempty"` 25 Unprotect bool `yaml:"unprotect,omitempty"` 26 When string `yaml:"when,omitempty"` // on_success, on_failure, always 27 Policy string `yaml:"policy,omitempty"` // pull, push, pull-push 28 FallbackKeys Stringorslice `yaml:"fallback_keys,omitempty"` 29 } 30 31 type CacheKey struct { 32 Value string `yaml:"-"` 33 Files Stringorslice `yaml:"files,omitempty"` 34 Prefix string `yaml:"prefix,omitempty"` 35 } 36 37 func (v *CacheKey) UnmarshalYAML(unmarshal func(interface{}) error) error { 38 var out1 string 39 var out2 = struct { 40 Files Stringorslice `yaml:"files"` 41 Prefix string `yaml:"prefix"` 42 }{} 43 44 if err := unmarshal(&out1); err == nil { 45 v.Value = out1 46 return nil 47 } 48 49 if err := unmarshal(&out2); err == nil { 50 v.Files = out2.Files 51 v.Prefix = out2.Prefix 52 return nil 53 } 54 55 return errors.New("failed to unmarshal cache key") 56 } 57 58 func (v *CacheKey) MarshalYAML() (interface{}, error) { 59 if v.Files != nil || v.Prefix != "" { 60 return struct { 61 Files Stringorslice `yaml:"files,omitempty"` 62 Prefix string `yaml:"prefix,omitempty"` 63 }{ 64 Files: v.Files, 65 Prefix: v.Prefix, 66 }, nil 67 } 68 69 return v.Value, nil 70 }