in coverage/coverage.go [122:217]
func (m *Model) MarkCovered(root interface{}) {
if root == nil || m == nil || m.IsReadOnly {
return
}
m.IsAnyCovered = true
// https://pkg.go.dev/encoding/json#Unmarshal
switch value := root.(type) {
case string:
if m.Enum != nil {
strValue := fmt.Sprintf("%v", value)
if _, ok := (*m.Enum)[strValue]; !ok {
logrus.Warningf("unexpected enum %s in %s", value, m.Identifier)
}
(*m.Enum)[strValue] = true
}
case bool:
if m.Bool == nil {
logrus.Errorf("unexpected bool %v in %v", value, m.Identifier)
}
(*m.Bool)[strconv.FormatBool(value)] = true
case float64:
case []interface{}:
if m.Item == nil {
logrus.Errorf("unexpected array in %s", m.Identifier)
}
for _, item := range value {
m.Item.MarkCovered(item)
}
case map[string]interface{}:
// decide to match property or variant
isMatchProperty := true
if m.Discriminator != nil && m.Variants != nil {
Loop:
for k, v := range value {
if k == *m.Discriminator {
// if model name or variant type is matched, then we match the property
if m.ModelName == v.(string) {
break
}
if m.VariantType != nil && *m.VariantType == v.(string) {
break
}
// either the discriminator value hit the variant model name or variant type, we match the variant
if variant, ok := (*m.Variants)[v.(string)]; ok {
isMatchProperty = true
variant.MarkCovered(value)
break
}
for _, variant := range *m.Variants {
if variant.VariantType != nil && *variant.VariantType == v.(string) {
isMatchProperty = true
variant.MarkCovered(value)
break Loop
}
}
logrus.Errorf("unexpected variant %s in %s", v.(string), m.Identifier)
}
}
}
if isMatchProperty {
for k, v := range value {
if m.Properties == nil {
// some objects has no properties defined
// https://github.com/Azure/azure-rest-api-specs/blob/3519c80fe510a268f6e59a29ccac8a53fdec15b6/specification/monitor/resource-manager/Microsoft.Insights/stable/2023-03-11/dataCollectionRules_API.json#L724
logrus.Warnf("unexpected key %s in %s", k, m.Identifier)
continue
}
if _, ok := (*m.Properties)[k]; !ok {
if !m.HasAdditionalProperties && m.Discriminator == nil {
logrus.Errorf("unexpected key %s in %s", k, m.Identifier)
continue
}
}
(*m.Properties)[k].MarkCovered(v)
}
}
case nil:
default:
logrus.Errorf("unexpect type %T for json unmarshaled value", value)
}
}