MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / GenFieldsFromProperties

Function GenFieldsFromProperties

pkg/codegen/schema.go:762–865  ·  view source on GitHub ↗

GenFieldsFromProperties produce corresponding field names with JSON annotations, given a list of schema descriptors

(props []Property)

Source from the content-addressed store, hash-verified

760// GenFieldsFromProperties produce corresponding field names with JSON annotations,
761// given a list of schema descriptors
762func GenFieldsFromProperties(props []Property) []string {
763 var fields []string
764 for i, p := range props {
765 field := ""
766
767 goFieldName := p.GoFieldName()
768
769 // Add a comment to a field in case we have one, otherwise skip.
770 if p.Description != "" {
771 // Separate the comment from a previous-defined, unrelated field.
772 // Make sure the actual field is separated by a newline.
773 if i != 0 {
774 field += "\n"
775 }
776 field += fmt.Sprintf("%s\n", StringWithTypeNameToGoComment(p.Description, p.GoFieldName()))
777 }
778
779 if p.Deprecated {
780 // This comment has to be on its own line for godoc & IDEs to pick up
781 var deprecationReason string
782 if extension, ok := p.Extensions[extDeprecationReason]; ok {
783 if extDeprecationReason, err := extParseDeprecationReason(extension); err == nil {
784 deprecationReason = extDeprecationReason
785 }
786 }
787
788 field += fmt.Sprintf("%s\n", DeprecationComment(deprecationReason))
789 }
790
791 // Check x-go-type-skip-optional-pointer, which will override if the type
792 // should be a pointer or not when the field is optional.
793 if extension, ok := p.Extensions[extPropGoTypeSkipOptionalPointer]; ok {
794 if skipOptionalPointer, err := extParsePropGoTypeSkipOptionalPointer(extension); err == nil {
795 p.Schema.SkipOptionalPointer = skipOptionalPointer
796 }
797 }
798
799 field += fmt.Sprintf(" %s %s", goFieldName, p.GoTypeDef())
800
801 shouldOmitEmpty := (!p.Required || p.ReadOnly || p.WriteOnly) &&
802 (!p.Required || !p.ReadOnly || !globalState.options.Compatibility.DisableRequiredReadOnlyAsPointer)
803
804 omitEmpty := shouldOmitEmpty
805
806 omitZero := false
807
808 // default, but allow turning of
809 if shouldOmitEmpty && p.Schema.SkipOptionalPointer && globalState.options.OutputOptions.PreferSkipOptionalPointerWithOmitzero {
810 omitZero = true
811 }
812
813 // Support x-omitempty and x-omitzero
814 if extOmitEmptyValue, ok := p.Extensions[extPropOmitEmpty]; ok {
815 if xValue, err := extParseOmitEmpty(extOmitEmptyValue); err == nil {
816 omitEmpty = xValue
817 }
818 }
819

Callers 2

GenStructFromSchemaFunction · 0.85
GenStructFromAllOfFunction · 0.85

Calls 12

DeprecationCommentFunction · 0.85
extParseOmitEmptyFunction · 0.85
extParseOmitZeroFunction · 0.85
stringOrEmptyFunction · 0.85
extParseGoJsonIgnoreFunction · 0.85
extExtraTagsFunction · 0.85
SortedMapKeysFunction · 0.85
GoFieldNameMethod · 0.80
GoTypeDefMethod · 0.45

Tested by

no test coverage detected