chain.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  2. package convert
  3. import (
  4. "fmt"
  5. "reflect"
  6. converter "github.com/anchore/go-struct-converter"
  7. "github.com/spdx/tools-golang/spdx/common"
  8. "github.com/spdx/tools-golang/spdx/v2/v2_1"
  9. "github.com/spdx/tools-golang/spdx/v2/v2_2"
  10. "github.com/spdx/tools-golang/spdx/v2/v2_3"
  11. )
  12. var DocumentChain = converter.NewChain(
  13. v2_1.Document{},
  14. v2_2.Document{},
  15. v2_3.Document{},
  16. )
  17. // Document converts from one document to another document
  18. // For example, converting a document to the latest version could be done like:
  19. //
  20. // sourceDoc := // e.g. a v2_2.Document from somewhere
  21. // var targetDoc spdx.Document // this can be any document version
  22. // err := convert.Document(sourceDoc, &targetDoc) // the target must be passed as a pointer
  23. func Document(from common.AnyDocument, to common.AnyDocument) error {
  24. if !IsPtr(to) {
  25. return fmt.Errorf("struct to convert to must be a pointer")
  26. }
  27. from = FromPtr(from)
  28. if reflect.TypeOf(from) == reflect.TypeOf(FromPtr(to)) {
  29. reflect.ValueOf(to).Elem().Set(reflect.ValueOf(from))
  30. return nil
  31. }
  32. return DocumentChain.Convert(from, to)
  33. }