You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2018/11/01 18:40:05 UTC

[mynewt-newt] 04/09: val: Generic setting value structure

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit 9b06effbba704378fe174dda01d9c1c8cc79bfa3
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Wed Oct 24 17:00:59 2018 -0700

    val: Generic setting value structure
---
 newt/val/valsetting.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/newt/val/valsetting.go b/newt/val/valsetting.go
new file mode 100644
index 0000000..a57d1d1
--- /dev/null
+++ b/newt/val/valsetting.go
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package val
+
+import (
+	"mynewt.apache.org/newt/newt/syscfg"
+	"mynewt.apache.org/newt/util"
+)
+
+// Value setting: represents a setting value as read from a YAML configuration
+// file.  The setting may reference a syscfg setting via the `MYNEWT_VAL(...)`
+// notation.
+type ValSetting struct {
+	// The exact text specified as the YAML map key.
+	Text string
+
+	// If this setting refers to a syscfg setting via the `MYNEWT_VAL(...)`
+	// notation, this contains the name of the setting.  Otherwise, "".
+	RefName string
+
+	// The setting value, after setting references are resolved.
+	Value string
+}
+
+// IntVal Extracts a setting's integer value.
+func (vs *ValSetting) IntVal() (int, error) {
+	iv, err := util.AtoiNoOct(vs.Value)
+	if err != nil {
+		return 0, util.ChildNewtError(err)
+	}
+
+	return iv, nil
+}
+
+// Constructs a setting from a YAML string.
+func ResolveValSetting(s string, cfg *syscfg.Cfg) (ValSetting, error) {
+	refName, val, err := cfg.ExpandRef(s)
+	if err != nil {
+		return ValSetting{},
+			util.FmtNewtError("value \"%s\" references undefined setting", s)
+	}
+
+	return ValSetting{
+		Text:    s,
+		RefName: refName,
+		Value:   val,
+	}, nil
+}